Validation of a form

Asked

Viewed 335 times

1

Hello, I searched and did not find well what I was looking for for my Laravel project so I need help! I have a simple form that receives the name, email and a message from the user, but I would like to go through the database and check if the email is already in use and if it is in use display a message or alert.

So what logica should I use? how could I treat this? should I use Laravel’s own Validation?

I know I could assign in the database as UNIQUE in the desired field but I didn’t want to go this way.

BS: I’m learning Laravel

follows below the form code.

<form action="/project/public/enviar" method="POST">
  <input type="hidden" name="_token" value="{{ csrf_token() }}">

  <div class="form-group">
    <label for="nome">Nome</label>
    <input type="text" id="nome" name="nome" class="form-control" placeholder="Nome">
  </div>

  <div class="form-group">
    <label for="email">E-Mail</label>
    <input type="text" id="email" name="email" class="form-control" placeholder="E-Mail">
  </div>

  <div class="form-group">
    <textarea id="mensagem" name="mensagem" class="form-control" placeholder="Digite sua mensagem"></textarea>
  </div>

  <button type="submit" class="btn btn-default">Enviar</button>

</form>

Here is the View:

Route::post('/enviar', 'ContatoController@enviar')->middleware('auth');

Here is the Controller:

public function enviar(Request $request){
    $contato = new Contato();

    $contato->nome = $request->get('nome');
    $contato->email = $request->get('email');
    $contato->mensagem = $request->get('mensagem');
    $contato->save();      

As I said very simple but the more I need is making my hair stand on end, I thank you for your help.

2 answers

2


I find it easier to use the validation of the Laravel. In this case there is already a validation ready:

$request->validate(["email" => "unique:contato"]);

Then the error messages will be injected into the view and can be recovered like this:

@if ($errors->any())
    <div class="alert alert-danger">
        <ul>
            @foreach ($errors->all() as $error)
                <li>{{ $error }}</li>
            @endforeach
        </ul>
    </div>
@endif

If you are using the Laravel as a Rest api, for example, the form of the validation changes a little:

//criando validação
$validator = \Validator::make($request->all(), [
    "email" => "unique:contato"
]);
//recuperando mensagem de erro:
$validator->errors()->first()

https://laravel.com/docs/5.8/validation

-1

You can use: Illuminate Support Facades Validator, just import it like this:

    use Illuminate\Support\Facades\Validator;

In the controller function put this:

    $validator = Validator::make($request->all(), [
        'nomeDoCampo' => 'nomeDaValidação',
    ]);

    if ($validator->fails()){
        return view('nameOfYourView')
            ->withErrors($validator->errors());
    }

And to show the error message in the view:

    @if ($errors->any())
        @foreach ($errors->all() as $error)
            {{$error}}
        @endforeach
    @endif

Link to all types of validations: https://laravel.com/docs/5.8/validation

Browser other questions tagged

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