简体   繁体   English

Laravel 表单验证仅在点击后退按钮时有效

[英]Laravel form validation only works when hitting back button

I'm using Laravel 4.2.我正在使用 Laravel 4.2。 My issue happens after submitting the form with data that will fail during form validation (short pass, invalid email, etc).我的问题是在提交包含在表单验证期间失败的数据(短通行证、无效电子邮件等)的表单后发生的。

My form is at /register.我的表格在 /register。 When I submit the form with invalid data, the page goes blank (white) and no data is entered into the database.当我提交包含无效数据的表单时,页面变为空白(白色)并且没有数据输入到数据库中。 The validation errors only show up after I hit the back button in the browser.验证错误仅在我点击浏览器中的后退按钮后才会出现。

If I enter valid data, the form submits and adds data to the database correctly, and redirects the user to /account.如果我输入有效数据,表单将正确提交数据并将数据添加到数据库,并将用户重定向到 /account。

Any help would be greatly appreciated.任何帮助将不胜感激。 I feel like it's something small that I'm overlooking, and it's kicking my ass.我觉得这是我忽略的小东西,它踢我的屁股。

Here's my code...这是我的代码...

User.php用户名

use Illuminate\Auth\UserTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;

class User extends BaseModel implements UserInterface, RemindableInterface {

    use UserTrait, RemindableTrait;

    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'users';

    /** Protect from mass-assignment **/
    protected $fillable = array('username', 'email', 'password', 'password_confirmation');

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = array('password', 'remember_token');

    public static $rules = array(
        'username'=>'required|unique:users,username|alpha_dash|min:4',
        'password'=>'required|between:8,32|confirmed',
        'password_confirmation'=>'required|between:8,32',
        'email'=>'required|email|unique:users,email'
    );

}

UsersController.php用户控制器.php

class UsersController extends BaseController {

    public function getNew() {
        return View::make('users.new')
            ->with('title', 'Create An Account');
    }   

    public function postCreate() {
        $validation = User::Validate(Input::all());

        if ($validation->passes()) {
            User::create(array(
                'username'=>Input::get('username'),
                'password'=>Hash::make(Input::get('password')),
                'email'=>Input::get('email')
            ));

            return Redirect::to('account')->with('message', 'Welcome! Your account has been created.');
        } else {
            Redirect::to('register')->withErrors($validation)->withInput();
        }
    }
}

Routes.php路由.php

Route::get('register', array('as'=>'register', 'uses'=>'UsersController@getNew'));

Route::post('register', array('before'=>'csrf', 'uses'=>'UsersController@postCreate'));

new.blade.php新刀片.php

    @if($errors->has())
        <p>The following errors have occured:</p>

        <ul id="form-errors">
            {{ $errors->first('username', '<li>:message</li>') }}
            {{ $errors->first('password', '<li>:message</li>') }}
            {{ $errors->first('password_confirmation', '<li>:message</li>') }}
            {{ $errors->first('email', '<li>:message</li>') }}
        </ul>   
    @endif

    {{ Form::open(array('action'=>'register')) }}

    <p>
        {{ Form::label('username', 'Username') }}<br />
        {{ Form::text('username', Input::old('username')) }}
    </p>

    <p>
        {{ Form::label('email', 'Email Address') }}<br />
        {{ Form::text('email', Input::old('email')) }}
    </p>

    <p>
        {{ Form::label('password', 'Password') }}<br />
        {{ Form::password('password') }}
    </p>

    <p>
        {{ Form::label('password_confirmation', 'Confirm Password') }}<br />
        {{ Form::password('password_confirmation') }}
    </p>

    <p>
        {{ Form::submit('Register') }}
    </p>

    {{ Form::close() }}

basemodel.php基础模型.php

class BaseModel extends Eloquent {

    public static function validate($data) {
        return Validator::make($data, static::$rules);
    }
}

In your UsersController::postCreate() function you're not returning the Redirect::to() in your else{} and so the response isn't being sent back to the user.在您的UsersController::postCreate()函数中,您不会在else{}返回Redirect::to() ,因此不会将响应发送回用户。 The controller is just executing without returning anything and so you're left with a blank page.控制器只是在没有返回任何东西的情况下执行,所以你只剩下一个空白页面。

Redirect::to('register')->withErrors($validation)->withInput();

needs to become需要成为

return Redirect::to('register')->withErrors($validation)->withInput();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM