php - 重定向回自定义请求类 : Laravel 5 中的错误和输入

标签 php laravel-5 laravel-5.1

我有一些原始代码,如下所示,其中包含 MySQL 数据库中的表单验证和保存过程。

原始代码

public function store(Request $request)
{
    $v = \Validator::make($request->all(), [
        'Category' => 'required|unique:tblcategory|max:25|min:5'
    ]);

    if ($v->fails()) {
        return \Redirect::back()
                    ->withErrors($v)
                    ->withInput();
    }
    ...
    //code to save the record in database is here....
    ...
}

Then I followed this article 并修改了上面的函数,现在它看起来像下面。

public function store(CategoryRequest $request)
{

    ...
    //code to save the record in database is here....
    ...
}

下面是请求类

class CategoryRequest extends Request
{
    protected $redirect = \Redirect::back()->withErrors($v)->withInput();
    public function authorize()
    {
        return false;
    }
    public function rules()
    {
        return [
            'Category' => 'required|unique:tblcategory|max:25|min:5'
        ];
    }
}

错误详情

syntax error, unexpected '(', expecting ',' or ';'

此错误出现在下一行。

protected $redirect = \Redirect::back()->withErrors($v)->withInput();

我错过了什么吗?

最佳答案

有多种方法可以告诉 Laravel 在验证失败时该怎么做。一种方法是覆盖 response() 方法并设置您自己的响应如下...

class CategoryRequest extends Request
{
    public function response(array $errors){
        return \Redirect::back()->withErrors($errors)->withInput();
    }

    public function authorize()
    {
        return false;
    }

    public function rules()
    {
        return [
            'Category' => 'required|unique:tblcategory|max:25|min:5'
        ];
    }
}

Laravel 的默认响应是将您重定向到包含错误和输入值的上一页,因此您的情况可能不需要上述代码。

关于php - 重定向回自定义请求类 : Laravel 5 中的错误和输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34598209/

相关文章:

php - 哎呀,看起来像出事了。尝试在内置服务器上运行时 Laravel 5 出现错误

foreign-keys - 模型工厂中的 Laravel 5.1 外键

javascript - 文件未在文件阅读器中上传

MYSQL 的 PHP 脚本作用于一个文件,而不作用于另一个文件

php - 为什么我不能更改我的 laravel 项目的生产模式?

php - 在 get 方法路由 laravel 中允许特殊字符

php - 对指定列的 Eloquent 结果进行排序

php - mysql数据存在代码不起作用

php - Composer 更新时, Composer 提示文件被编辑?

php - 如何在 laravel 范围内查询关系