【问题标题】:Passing Success or Failure For Laravel 4 Validation通过 Laravel 4 验证的成功或失败
【发布时间】:2014-10-02 23:48:41
【问题描述】:

我正在使用我的 restful 用户控制器中创建一个用户表单。当用户在表单上时,我将它当前发布到 store 方法。当表单提交给该方法时,我希望它验证用户输入,然后重定向回 create 方法,以便在数据验证失败时显示成功消息或错误消息数组。有人可以指出需要什么来帮助我根据验证测试通过成功消息或错误消息重定向到 create 方法。

/**
 * Store a newly created user in storage.
 *
 * @return Response
 */
public function store()
{
    $input = Input::all();

    $validation = Validator::make($input, $rules);

    if ($validation->fails())
    {
        $messages = $validator->all();
    }
    else
    {
        User::create([
            'username' => $input['username'],
            'email_address' => $input['email_address'],
            'password' => $input['password'],
            'role_id' => $input['role']

        ]);
    }

    return Redirect::to('users/create');
}

【问题讨论】:

    标签: php laravel


    【解决方案1】:

    你可以使用任何一个

    $messages = $validator->messages();
    

    $failed = $validator->failed();
    

    检索错误消息。如果你想显示'flash'消息,你可以在重定向中使用'with'方法。

    Redirect::to('user/currentroute')->with('message', 'Something wrong'); 
    

    更多详情,请阅读documentation

    【讨论】:

    • 那么 all() 函数是什么?至于我将如何传递成功消息或失败消息而不是实际消息。
    • 你的意思是像'登录失败'这样的消息吗???您可以在重定向中传递 'with' 方法 return Redirect::to('user/login')->with('message', 'Login Failed');
    • 很好,但是,添加 getErrors 和 getInput 的最佳有效方法是什么,因为如果它是成功的帖子,我将不需要它们。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-09
    • 1970-01-01
    • 1970-01-01
    • 2021-06-17
    • 2019-08-07
    • 2015-12-18
    相关资源
    最近更新 更多