Search code examples
phplaravelroutescontrollers

Laravel - POST method redirects to form, refresh it and show the "required error"


I'm trying to rename URIs in my routes. I can actually display the view but when I hit the button to save the data, the form just reloads, resets and the following errors appear within the form:

The password field is required.
The nombre field is required.
The apellido field is required.
The calle field is required.
The numero field is required.
The barrio field is required.
The localidad field is required.
The provincia field is required.
The codigo postal field is required.
The telefono field is required.
The email field is required.
The nivel acceso field is required.

This is my routes/web.php:

<?php

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

/* Routes Websites */
Route::get('/', 'WebsiteController@index');
Route::view('/sobreitc', 'website.about');
Route::view('/despachantes', 'website.traders');
Route::get('/cursos', 'WebsiteController@courses');
Route::view('/contacto', 'website.contact');

/* Routes Webapp */

// Auth::routes();
// Route::auth();

/* Routes Authentication */

// Authentication Routes...
Route::get('/ingresar', 'Auth\LoginController@showLoginForm');
Route::post('/ingresar', 'Auth\LoginController@login');
Route::get('/salir', 'Auth\LoginController@logout');

// Registration Routes...
Route::get('/nuevousuario', 'Auth\RegisterController@showRegistrationForm');
Route::post('/nuevousuario', 'Auth\RegisterController@register');



Route::resource('panel', 'WebappController');

This is my RegisterController.php:

<?php

namespace App\Http\Controllers\Auth;

use App\User;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;

class RegisterController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Register Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles the registration of new users as well as their
    | validation and creation. By default this controller uses a trait to
    | provide this functionality without requiring any additional code.
    |
    */

    use RegistersUsers;

    /**
     * Where to redirect users after registration.
     *
     * @var string
     */
    protected $redirectTo = '/panel';

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest');
    }

    /**
     * Get a validator for an incoming registration request.
     *
     * @param  array  $data
     * @return \Illuminate\Contracts\Validation\Validator
     */
    protected function validator(array $data)
    {
        return Validator::make($data, [
            'usuario' => 'string|max:255|unique:usuarios',
            'password' => 'required|string|min:8|confirmed',
            'nombre' => 'required|string|max:255',
            'apellido' => 'required|string|max:255',
            'calle' => 'required|string|max:255',
            'numero' => 'required|integer|max:11',
            'barrio' => 'required|string|max:255',
            'localidad' => 'required|string|max:255',
            'provincia' => 'required|string|max:255',
            'codigoPostal' => 'required|integer|max:11',
            'telefono' => 'required|integer|max:11',
            'email' => 'required|string|max:255',
            'nivelAcceso' => 'required|string|max:255',
        ]);
    }

    /**
     * Create a new user instance after a valid registration.
     *
     * @param  array  $data
     * @return \App\User
     */
    protected function create(array $data)
    {
        return User::create([
            'usuario' => $data['usuario'],
            'password' => bcrypt($data['password']),
            'nombre' => $data['nombre'],
            'apellido' => $data['apellido'],
            'calle' => $data['calle'],
            'numero' => $data['numero'],
            'barrio' => $data['barrio'],
            'localidad' => $data['localidad'],
            'provincia' => $data['provincia'],
            'codigoPostal' => $data['codigoPostal'],
            'telefono' => $data['telefono'],
            'email' => $data['email'],
            'nivelAcceso' => $data['nivelAcceso'],
        ]);
    }
}

And this is my auth/register.blade.php view:

@extends('layouts.webapp')

@section('content')
<section class="register-grid">
  <div class="formulario">

    @if($errors->any())
        <span class="help-block">
          @foreach($errors->all() as $error)
            <strong>{{ $error }}</strong><br/>
          @endforeach
        </span>
    @endif

    <form method="POST" action="{{ url('nuevousuario') }}">
      <!-- {{ csrf_field() }} -->
      <div class="form-group{{ $errors->has('usuario') ? ' has-error' : '' }}">
        <label for="usuario">Usuario</label>
        <input type="text" class="form-control" id="usuario" aria-describedby="usuario" placeholder="Usuario" value="{{ old('usuario') }}" required autofocus>
        <small id="usuario" class="form-text text-muted">Ingrese su usuario</small>
      </div>
      <div class="form-group{{ $errors->has('password') ? ' has-error' : '' }}">
        <label for="password">Contraseña</label>
        <input type="password" class="form-control" id="password" placeholder="Contraseña" required>
      </div>
      <div class="form-group">
          <label for="password-confirm">Confirmar Contraseña</label>
          <input id="password-confirm" type="password" class="form-control" name="password_confirmation" placeholder="Confirmar Contraseña" required>
      </div>
      <div class="form-group{{ $errors->has('nombre') ? ' has-error' : '' }}">
        <label for="nombre">Nombre</label>
        <input type="text" class="form-control" id="nombre" aria-describedby="nombre" placeholder="Nombre" value="{{ old('nombre') }}" required>
      </div>
      <div class="form-group{{ $errors->has('apellido') ? ' has-error' : '' }}">
        <label for="apellido">Apellido</label>
        <input type="text" class="form-control" id="apellido" aria-describedby="apellido" placeholder="Apellido" value="{{ old('apellido') }}" required>
      </div>
      <div class="form-group{{ $errors->has('calle') ? ' has-error' : '' }}">
        <label for="calle">Calle</label>
        <input type="text" class="form-control" id="calle" aria-describedby="calle" placeholder="Calle" value="{{ old('calle') }}" required>
      </div>
      <div class="form-group{{ $errors->has('numero') ? ' has-error' : '' }}">
        <label for="numero">Numero</label>
        <input type="text" class="form-control" id="numero" aria-describedby="numero" placeholder="Numero" value="{{ old('numero') }}" required>
      </div>
      <div class="form-group{{ $errors->has('barrio') ? ' has-error' : '' }}">
        <label for="barrio">Barrio</label>
        <input type="text" class="form-control" id="barrio" aria-describedby="barrio" placeholder="Barrio" value="{{ old('barrio') }}" required>
      </div>
      <div class="form-group{{ $errors->has('localidad') ? ' has-error' : '' }}">
        <label for="localidad">Localidad</label>
        <input type="text" class="form-control" id="localidad" aria-describedby="localidad" placeholder="Localidad" value="{{ old('localidad') }}" required>
      </div>
      <div class="form-group{{ $errors->has('provincia') ? ' has-error' : '' }}">
        <label for="provincia">Provincia</label>
        <input type="text" class="form-control" id="provincia" aria-describedby="provincia" placeholder="Provincia" value="{{ old('provincia') }}" required>
      </div>
      <div class="form-group{{ $errors->has('codigoPostal') ? ' has-error' : '' }}">
        <label for="codigoPostal">Codigo Postal</label>
        <input type="text" class="form-control" id="codigoPostal" aria-describedby="codigoPostal" placeholder="Codigo Postal" value="{{ old('codigoPostal') }}" required>
      </div>
      <div class="form-group{{ $errors->has('telefono') ? ' has-error' : '' }}">
        <label for="telefono">Teléfono</label>
        <input type="tel" class="form-control" id="telefono" aria-describedby="telefono" placeholder="Teléfono" value="{{ old('telefono') }}" required>
      </div>
      <div class="form-group{{ $errors->has('email') ? ' has-error' : '' }}">
        <label for="email">E-mail</label>
        <input type="email" class="form-control" id="email" aria-describedby="email" placeholder="E-mail" value="{{ old('email') }}" required>
      </div>
      <div class="form-group{{ $errors->has('nivelAcceso') ? ' has-error' : '' }}">
        <label for="nivelAcceso">Nivel de Acceso</label>
        <input type="text" class="form-control" id="nivelAcceso" aria-describedby="nivelAcceso" placeholder="Nivel de Acceso" value="{{ old('nivelAcceso') }}" required>
      </div>

      <button type="submit" class="btn btn-primary">Crear</button>
      <!-- <a class="btn btn-link" href="{{ url('password.request') }}">¿Olvido su contraseña?</a> -->
    </form>
  </div>
</section>
@endsection

I will appreciate any help on this. It's frustrating and maybe it's just an obvious thing I'm missing.


Solution

  • You should add name attribute to all input elements. For example:

    <input name="password" type="password" class="form-control" id="password" placeholder="Contraseña" required>
    

    Also, you need to pass CSRF token. So, change this:

    <!-- {{ csrf_field() }} -->
    

    To:

    {{ csrf_field() }}