Laravel, Form is sent double

3

Good morning, I'm starting with laravel and I've created a login / registration system using Auth but I have a problem, I try to put the login and registration form on the same page and when I give "Logear" they send both the data of the first form as of the second.

When I leave the form blank and click on the "Enter" button, it shows me

  

The name field is required.

     

The password field is required.

     

The name field is required.

     

The password field is required.

            {{ $errors->first('nombre') }}
            {{ $errors->first('password') }}

            {{ $errors->first('nombre') }}
            {{ $errors->first('password') }}
            <section class="wPanelUsuario" id="Login">
                <h3>Bienvenido</h3>
                <form action="{{ route('login') }}" method="POST">
                  {{ csrf_field() }}
                  <input type="text" name="nombre" placeholder="Usuario" value="{{ old('nombre') }}"/>
                  <input type="password" name="password" placeholder="Contraseña" value="{{ old('password') }}"/>
                  <input type="submit" class="botonPrimario" value="Entrar">
                  <a class="botonSecundario" id="suBoton">Registrarme</a>
                </form>
            </section>

            <section class="wPanelUsuario none" id="Registro">
                <h3>Ya casí...</h3>
                <p class="text-center">¡Estás a un paso de obtener funciones exclusivas para usuarios registrados!</p>
                <form action="{{ route('register') }}" method="POST">
                  {{ csrf_field() }}
                  <input type="text" name="nombre" placeholder="Usuario" value="{{ old('nombre') }}" required/>
                  <input type="email" name="email" placeholder="Correo electrónico" value="{{ old('email') }}" required/>
                  <input type="password" name="password" placeholder="Contraseña" value="{{ old('password') }}" required/>
                  <input type="password" name="password2" placeholder="Repite tu contraseña" value="{{ old('password2') }}" required/>
                  <input type="submit" class="botonPrimario" value="Registrarme">
                  <a class="botonSecundario" id="loginBoton">Iniciar sesión</a>
                </form>
            </section>

The problem is that I have two input called with the same name ("name", "password"). I tried to change the name of the input to one of the forms but the problem is that it automatically uses that name to insert the data in the data base and therefore I get an error too.

Controller:

protected function validator(array $data)
{
    return Validator::make($data, [
        'nombre' => 'required|string|unique:Usuarios',
        'email' => 'required|string|email|unique:Usuarios',
        'password' => 'required|string|min:6',
        'password_confirmation' => 'required|string|min:6|same:password',
    ]);
}
protected function create(array $data)
{
    return User::create([
        'nombre' => $data['nombre'],
        'email' => $data['email'],
        'password' => bcrypt($data['password']),
    ]);
}

When I put email1 I already skipped the following error Column not found: 1054 Unknown column 'email1' in 'where clause' (SQL: select count (*) as aggregate from Usuarios where email1 = example @ example. com)

    
asked by 29.07.2017 в 18:22
source

2 answers

1

A simple way to solve this problem is to rename these fields temporarily, but in the controller return them to the way you want Laravel. It's like doing a kind of mapping of them, just for validation:

        <section class="wPanelUsuario" id="Login">
            <h3>Bienvenido</h3>
            <form action="{{ route('login') }}" method="POST">
              {{ csrf_field() }}
              <input type="text" name="nombre" placeholder="Usuario" value="{{ old('nombre') }}"/>
              <input type="password" name="password" placeholder="Contraseña" value="{{ old('password') }}"/>
              <input type="submit" class="botonPrimario" value="Entrar">
              <a class="botonSecundario" id="suBoton">Registrarme</a>
            </form>
        </section>

        <section class="wPanelUsuario none" id="Registro">
            <h3>Ya casí...</h3>
            <p class="text-center">¡Estás a un paso de obtener funciones exclusivas para usuarios registrados!</p>
            <form action="{{ route('register') }}" method="POST">
              {{ csrf_field() }}
              <input type="text" name="nombre2" placeholder="Usuario" value="{{ old('nombre2') }}" required/>
              <input type="email" name="correo" placeholder="Correo electrónico" value="{{ old('correo') }}" required/>
              <input type="password" name="contrasena" placeholder="Contraseña" value="{{ old('contrasena') }}" required/>
              <input type="password" name="contrasena_confirmation" placeholder="Repite tu contraseña" value="{{ old('contrasena_confirmation') }}" required/>
              <input type="submit" class="botonPrimario" value="Registrarme">
              <a class="botonSecundario" id="loginBoton">Iniciar sesión</a>
            </form>
        </section>

I assume you perform the validation in the Request:

Validation of the registration form:

class RequestCrearUsuario extends Request
{

    // ...


    public function messages()
    {
        return [
            'nombre2'       => 'required|max:255',
            'correo'        => 'required|email|max:255|unique:users,email',
            'contrasena'    => 'required|confirmed|min:6',
        ];
     }

In the method of the controller that receives the data from the registry:

public function register(RequestCrearUsuario $request)
{
    $request['nombre'] = $request['nombre2'];
    $request['password'] = $request['contrasena'];
    unset($request['nombre2']);
    unset($request['contrasena']);

    // ....

}

EDITING

Another option may be to add a value to the session at some point in the process that identifies where the request comes from (the login or registration form):

If the request comes from the registration form:

$request->session()->flash('formulario', 'registro');

Or login:

$request->session()->flash('formulario', 'login');

In the view it would be something like this:

    @if(Session::get('formulario') == 'login')
        {{ $errors->first('nombre') }}
        {{ $errors->first('password') }}
    @endif

    @if(Session::get('formulario') == 'registro')
        {{ $errors->first('nombre') }}
        {{ $errors->first('password') }
    @endif
    
answered by 29.07.2017 в 19:03
0

Are you complicating yourself, so that you duplicate the error checking twice more? I would leave it like this, as each form goes to an independent action and has the same "name=", when you submit these checking each form separately. You only need those two lines for both forms.

{{ $errors->first('nombre') }}
{{ $errors->first('password') }}

<section class="wPanelUsuario" id="Login">
    <h3>Bienvenido</h3>
    <form action="{{ route('login') }}" method="POST">
      {{ csrf_field() }}
      <input type="text" name="nombre" placeholder="Usuario" value="{{ old('nombre') }}"/>
      <input type="password" name="password" placeholder="Contraseña" value="{{ old('password') }}"/>
      <input type="submit" class="botonPrimario" value="Entrar">
      <a class="botonSecundario" id="suBoton">Registrarme</a>
    </form>
</section>

<section class="wPanelUsuario none" id="Registro">
    <h3>Ya casí...</h3>
    <p class="text-center">¡Estás a un paso de obtener funciones exclusivas para usuarios registrados!</p>
    <form action="{{ route('register') }}" method="POST">
      {{ csrf_field() }}
      <input type="text" name="nombre" placeholder="Usuario" value="{{ old('nombre') }}" required/>
      <input type="email" name="email" placeholder="Correo electrónico" value="{{ old('email') }}" required/>
      <input type="password" name="password" placeholder="Contraseña" value="{{ old('password') }}" required/>
      <input type="password" name="password2" placeholder="Repite tu contraseña" value="{{ old('password2') }}" required/>
      <input type="submit" class="botonPrimario" value="Registrarme">
      <a class="botonSecundario" id="loginBoton">Iniciar sesión</a>
    </form>
</section>
    
answered by 13.06.2018 в 13:12