Laravel 5 WithInput error

2

I am trying to have an error in the login form return me to the record view keeping the data in the form. with a return back () -> withInput and it makes the return correctly but it does not show me my data here is the code:
RegistryController:

public function showRegistro()
{
    return view('registro');
}

public function register(Request $request)
{
    if ($request->password1 != $request->password2) {
        //return back()->withInput();
        return back()->withInput($request->input());


    } else {
        $usuario = new User();
        $usuario->nombre = $request->nombre;
        $usuario->email = $request->input('email');
        $usuario->direccion = $request->input('direccion');
        $usuario->password = $request->input('password1');
        $usuario->id_rol = 2; 
    }



    try {
        $usuario->save();
        }   catch (QueryException $e){
        session(['error' => $e->getMessage()]);
    }   


@extends('layouts.principal')

@section('titulo')
Registro
@endsection


@section('principal')
<div class="container">
<div class="panel panel-primary">
<div class="panel-heading">
  <h3 class="panel-title">Registro de usuario</h3>
  </div>
  <div class="panel-body">
      {{ Form::open(['action' => 'RegistroController@register', 'method' =>        'POST', 'class' => 'form-horizontal']) }}
<div class="form-group">
       {{-- {{ Form::label('nombre', 'Nombre', ['class' => 'col-sm-2    control-label']) }} --}}
</div>
 <div class="form-group">
    <label for="inputEmail" class="col-sm-2 control-label">E-mail</label>
    <div class="col-sm-10">
      <input type="email" class="form-control" id="email" name="email" placeholder="Email">
  </div>
</div>
<div class="form-group">
  <label for="inputName" class="col-sm-2 control-label">Nombre</label>
   <div class="col-sm-10">
     <input type="text" class="form-control" id="nombre" name="nombre" placeholder="Nombre">
       </div>
        </div>
           <div class="form-group">
  <label for="inputDireccion" class="col-sm-2 control-label">Direccion</label>
  <div class="col-sm-10">
    <input type="text" class="form-control" id="direccion" name="direccion" placeholder="Direccion">
  </div>
 </div>
<div class="form-group">
    <label class="control-label col-sm-2" for="pwd">Contraseña</label>
     <div class="col-sm-10">
      <input type="password" class="form-control" id="password1" name="password1" placeholder="Contraseña">
     </div>
        </div>
              <div class="form-group">
            <label class="control-label col-sm-2" for="pwd">Confirmar Contraseña</label>
                <div class="col-sm-10">
              <input type="password" class="form-control" id="password2"    name="password2" placeholder="Confirmar Contraseña">
      </div>
     </div>
        <div class="col-md-3 col-md-offset-2">
                        <button type="submit" class="btn btn-primary">Aceptar</button>
             </div>
                 </div>
     @endsection


Thanks in advance.

    
asked by Jonathan Pėrez 05.03.2018 в 12:34
source

2 answers

0

Try in your view to put your input fields the value attribute in this way:

<input type="text" class="form-control" id="nombre" name="nombre" placeholder="Nombre" value="{{ old("nombre") }}">

In this case, "name" is the same value as the name attribute.

This way you can have the field with the old value, that is, the redirected from your controller with back () - > withInput ($ request-> input ())

    
answered by 05.03.2018 / 14:11
source
0

Considering that you are using the Laravel Collective form builder, it is not necessary to add the helper old() as suggested by the other answer, you can do this:

{!! Form::password('password1', ['class' => 'form-control', 'placeholder' => 'Contraseña']) !!}

However, validating the way you do it is not the recommended method, what you should do is use the Laravel validation methods and rules, in the case of key confirmation you would use the rule confirmed , in which the first field would be called for example password and the second password_confirmation .

Please check the Laravel documentation on how to perform the validations and their respective rules:

link

    
answered by 05.03.2018 в 15:36