Laravel 表单验证仅在点击后退按钮时有效
Laravel form validation only works when hitting back button
我正在使用 Laravel 4.2。我的问题发生在提交表单后,该表单包含在表单验证期间将失败的数据(短通、无效电子邮件等)。
我的表格在/register。当我提交包含无效数据的表单时,页面变为空白(白色)并且没有数据输入到数据库中。验证错误只有在我点击浏览器中的后退按钮后才会出现。
如果我输入有效数据,表单会正确提交数据并将数据添加到数据库,并将用户重定向到 /account。
如有任何帮助,我们将不胜感激。我觉得这是我忽略的小东西,它正在踢我的屁股。
这是我的代码...
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
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
Route::get('register', array('as'=>'register', 'uses'=>'UsersController@getNew'));
Route::post('register', array('before'=>'csrf', 'uses'=>'UsersController@postCreate'));
new.blade.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
class BaseModel extends Eloquent {
public static function validate($data) {
return Validator::make($data, static::$rules);
}
}
在您的 UsersController::postCreate()
函数中,您不会在 else{}
中返回 Redirect::to()
,因此不会将响应发送回用户。控制器只是执行而没有返回任何东西,所以你留下了一个空白页面。
Redirect::to('register')->withErrors($validation)->withInput();
需要成为
return Redirect::to('register')->withErrors($validation)->withInput();
我正在使用 Laravel 4.2。我的问题发生在提交表单后,该表单包含在表单验证期间将失败的数据(短通、无效电子邮件等)。
我的表格在/register。当我提交包含无效数据的表单时,页面变为空白(白色)并且没有数据输入到数据库中。验证错误只有在我点击浏览器中的后退按钮后才会出现。
如果我输入有效数据,表单会正确提交数据并将数据添加到数据库,并将用户重定向到 /account。
如有任何帮助,我们将不胜感激。我觉得这是我忽略的小东西,它正在踢我的屁股。
这是我的代码...
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
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
Route::get('register', array('as'=>'register', 'uses'=>'UsersController@getNew'));
Route::post('register', array('before'=>'csrf', 'uses'=>'UsersController@postCreate'));
new.blade.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
class BaseModel extends Eloquent {
public static function validate($data) {
return Validator::make($data, static::$rules);
}
}
在您的 UsersController::postCreate()
函数中,您不会在 else{}
中返回 Redirect::to()
,因此不会将响应发送回用户。控制器只是执行而没有返回任何东西,所以你留下了一个空白页面。
Redirect::to('register')->withErrors($validation)->withInput();
需要成为
return Redirect::to('register')->withErrors($validation)->withInput();