Validations with Laravel - Input and Option

Asked

Viewed 257 times

1

I have a form that I receive an email address and what type of email. Filling in the fields should not be mandatory. If the email is completed, please provide the type.

Following example:

  • Email - Optional
  • Email Type - Optional


  • Email - Case Filled

  • Email Type - Required.

Following form I’m using:

<form action="" method="POST" enctype="multipart/form-data">
 <div class="form-group col-md-6">
      <div class="input-group">
          <input type="email" class="form-control" value="" id="email" name="email"
              placeholder="Email">

          <select class="form-control select2" name="tipo_email" style="width: 100%;">
              <option value="" selected="">Selecione</option>
              <option value="1">Pessoal</option>
              <option value="2">Corporativo</option>
          </select>
      </div>
  </div>
  <button type="submit">Enviar</button>
</form>

I tried to use Laravel validation required_unless but I couldn’t. Someone can help me ?

  • mano bom dia, você usar o Rule::requiredIf https://laravel.com/docs/5.8/validation#Rule-required-if

1 answer

2


Good afternoon man, the best way to validate yourself seriously with the Rule::Requiredif, it is of the Rule class of the Laravel for validation in the client part.

//Não esquece os namespaces
use Illuminate\Validation\Rule;
use Illuminate\Support\Facades\Validator;

It would look like this:

$validator  = Validator::make($request->all(), [
            'tipo_email' => Rule::requiredIf(function () use ($request) { //Se o email estiver preenchido o campo tipo_email passa a ser obrigatório.
                return $request->get('email') !== null ? true : false; //Verifica se o email foi preenchido
            }),
        ]);

Capturing error messages and redirecting to display:

if ($validator->fails()) {
     return redirect('home')
       ->withErrors($validator)
       ->withInput();
}

Route post for registration:

Route::post('/cadastro/pessoa', 'UserController@cadastroPessoa')->name('cadastro.pessoa');

Your form would look like this:

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

   <div style="display:flex;justify-content: center;">
     <form action="{{ route('cadastro.pessoa') }}" method="POST">
        @csrf
        <div class="form-group col-md-6">
         <div class="input-group">
           <input type="email" class="form-control" value="" id="email" name="email"placeholder="Email">

          <select class="form-control select2" id="tipo_email" name="tipo_email" style="width: 100%;">
             <option value="" selected="">Selecione</option>
             <option value="1">Pessoal</option>
             <option value="2">Corporativo</option>
         </select>
       </div>
     </div>
        <button type="submit">Enviar</button>
   </form>
 </div>

Browser other questions tagged

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