Laravel - POST 方法重定向到表单,刷新它并显示 "required error"

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

我正在尝试重命名我的路线中的 URI。我实际上可以显示视图,但是当我点击按钮保存数据时,表单只是重新加载、重置并且表单中出现以下错误:

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.

这是我的 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');

这是我的 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'],
        ]);
    }
}

这是我的 auth/register.blade.php 观点:

@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

对于这方面的任何帮助,我将不胜感激。这令人沮丧,也许这只是我遗漏的一件显而易见的事情。

您应该向所有 input 元素添加 name 属性。例如:

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

另外,你需要pass CSRF token。所以,改变这个:

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

收件人:

{{ csrf_field() }}

所以我不必总是在所有元素中写 id 和 name 我 运行 这个函数在加载页面时加载了具有相同名称的 id

function namesToId(element) {
    if(element===undefined)element='body';
    $(element).find('[name]').each(function(index, element){
        var elementId = $(this).attr('id');
        var elementName = $(this).attr('name');
        if(elementId===undefined){
            if(elementName.search(/\[]/)===-1){
                $(this).attr('id', elementName);
            }else{
                $(this).attr('id', elementName.replace(/\[]/, '_'+name="'+elementName+'"][id] ').length));
            }
        }
    });
};