Validate password using Laravel

Asked

Viewed 1,531 times

1

I have the following form

inserir a descrição da imagem aqui

My password fields are like this:

 <div class="form-group {{ $errors->has('senha') ? ' has-error' : '' }}">
   <label for="senha">Senha</label>
    <input type="password" class="form-control" id="senha" name="senha" placeholder="Minino 8 dígitos" required value="{{ old('senha') }}">
    @if ($errors->has('senha'))
       <span class="help-block">
          <strong>{{ $errors->first('senha') }}</strong>
       </span>
    @endif
</div>

 <div class="form-group {{ $errors->has('senha1') ? ' has-error' : '' }}">
    <label for="senha1">Repita a Senha</label>
    <input type="password" class="form-control" id="senha1" name="senha1" placeholder="Minino 8 dígitos" required value="{{ old('senha1') }}">
    @if ($errors->has('senha1'))
       <span class="help-block">
          <strong>{{ $errors->first('senha1') }}</strong>
       </span>
    @endif
 </div>

I’m trying to validate like this using the confirmed:

        $senha      = $request->get( 'senha' );
        $senha1     = $request->get( 'senha1' );
        $nivel      = $request->get( 'nivel' );

        $field = array(
            'senha' => $senha,
            'password_confirmation'  => $senha1
        );

        $rules = array(
            'senha'   => 'required|confirmed|min:8',
        );

        $traducao = array(
            'required'  => 'Este campo é obrigatório',
            'confirmed' => 'A confirmação de :attribute não confere.',
            'min'       => 'A tem que ter no mínimo 8 dígitos.',
        );

        $validator = Validator::make(
            $field,
            $rules,
            $traducao
        );

But it only comes back the way it is in the first picture

1 answer

1


An example, with different data from yours is the following:

$rules = array(
    'username' => 'required|alpha_num|min:3|max:32',
    'email' => 'required|email',
    'password' => 'required|min:3|confirmed',
    'password_confirmation' => 'required|min:3'
);

that is, it has to have a field password and the other with the nomenclature password_confirmation, that means the name should be like this:

<input type="password" name="password" />

and

<input type="password" name="password_confirmation" />

in your case then the names should be:

<div class="form-group {{ $errors->has('senha') ? ' has-error' : '' }}">
   <label for="senha">Senha</label>
    <input type="password" class="form-control" id="senha" name="senha" 
        placeholder="Minino 8 dígitos" required value="{{ old('senha') }}">
    @if ($errors->has('senha'))
       <span class="help-block">
          <strong>{{ $errors->first('senha') }}</strong>
       </span>
    @endif
</div>

<div class="form-group {{ $errors->has('senha_confirmation') ? ' has-error' : '' }}">
    <label for="senha1">Repita a Senha</label>
    <input type="password" class="form-control" 
        id="senha_confirmation" name="senha_confirmation" 
        placeholder="Minino 8 dígitos" required value="{{ old('senha_confirmation') }}">
    @if ($errors->has('senha1'))
       <span class="help-block">
          <strong>{{ $errors->first('senha_confirmation') }}</strong>
       </span>
    @endif
</div>

and its rules

$rules = array(
    ....
    'senha' => 'required|min:8|confirmed',
    'senha_confirmation' => 'required|min:8'
);

Reference:

  • 1

    Thank you very much, it worked!

Browser other questions tagged

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