Validate password using Laravel

1

I have the following form

Mypasswordfieldslooklikethis:

<divclass="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 this way using 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 returns the way it is in the first image

    
asked by anonymous 27.02.2018 / 16:20

1 answer

1

An example, with data other than 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, you have to have a password field and the other one with the password_confirmation nomenclature, this means that the name should look 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 your rules

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

27.02.2018 / 16:29