首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Laravel表单验证只在按回按钮时起作用。

Laravel表单验证只在按回按钮时起作用。
EN

Stack Overflow用户
提问于 2015-07-29 14:19:57
回答 1查看 917关注 0票数 2

我用的是拉拉维尔4.2。我的问题发生在提交表单后,在表单验证过程中将失败的数据(短通、无效电子邮件等)。

我的表格在/register。当我提交包含无效数据的表单时,页面变为空白(白色),数据库中不输入任何数据。只有在我按下浏览器中的后退按钮后,才会出现验证错误。

如果输入有效数据,表单将正确提交数据并将数据添加到数据库,并将用户重定向到/account。

任何帮助都将不胜感激。我觉得这是一件我忽略的小东西,它在踢我的屁股。

这是我的密码。

User.php

代码语言:javascript
复制
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

代码语言:javascript
复制
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

代码语言:javascript
复制
Route::get('register', array('as'=>'register', 'uses'=>'UsersController@getNew'));

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

new.blade.php

代码语言:javascript
复制
    @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

代码语言:javascript
复制
class BaseModel extends Eloquent {

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

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-07-29 15:02:43

在您的UsersController::postCreate()函数中,您不会在else{}中返回Redirect::to(),因此不会将响应发送回用户。控制器只是在执行而不返回任何内容,因此只剩下一个空白页。

代码语言:javascript
复制
Redirect::to('register')->withErrors($validation)->withInput();

需要成为

代码语言:javascript
复制
return Redirect::to('register')->withErrors($validation)->withInput();
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31703205

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档