I’m trying to do a validation on the Laravel but it’s not going

Asked

Viewed 24 times

2

When I leave the registration form fields without filling in, rule messages are displayed.

This is the customer registration code:

<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use \App\Clientes;
use Validator; // Validação
use App\Http\Requests\ClienteRules;// Regras do formulário

class VendedorController extends Controller
{
    public function cadastrar(ClienteRules $request){
    $cliente = new \App\Clientes();
    $cliente->nome = $request->nome;
    $cliente->descricao = $request->descricao;
    $cliente->vendedor = $request->vendedor;
    $cliente->orcamento = $request->orcamento;
    $cliente->save();
    return redirect()->to('/lista_Cliente');

}

And here the requests

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class ClienteRules extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'nome' => 'required',
            'descricao' => 'required',
            'vendedor' => 'required',
            'orcamento' => 'required'
        ];
    }
    public function messages(){
        return[
            'nome.required' => 'Informe seu nome.',
            'descricao.required' => 'Informe a descrição do produto.',
            'vendedor.required' => 'Informe o nome do vendedor.',
            'orcamento.required' => 'Informe o orçamento.'

        ];
    }
}

1 answer

2

I was able to fix it. I wasn’t really having a problem, I just wasn’t displaying the error messages in create.blade.php .

@if(isset($errors) && count ($errors)>0)
    <div class="alert alert-danger">
        @foreach ($errors->all() as $error)
          <p>{{$error}}</p>                 
        @endforeach
    </div>
  @endif

Browser other questions tagged

You are not signed in. Login or sign up in order to post.