验证后在控制器中获取表单输入(Getting form inputs in the controller after validation)

编程入门 行业动态 更新时间:2024-10-20 05:33:59
验证后在控制器中获取表单输入(Getting form inputs in the controller after validation)

我是Laravel世界的新手,我有一个到目前为止我无法解决的问题。

我的问题是:如何在验证失败后访问控制器的create()函数中提交的值?

这是我有的:

PostsController,在routes.php中定义为资源 在控制器中我有通常的方法create()和store()等。

这是一些代码:

//PostsController public function create() { //Here I need to get the inputs return view('posts.create'); } public function store(Requests\PostsRequest $request) { //store actions }

我创建了PostsRequest类,它有一些规则,如:

public function rules(){ $rules = [ 'category_id'=>'required', 'name'=>'required|min:3', 'description'=>'required' ]; return $rules; }

如果一切正常,则条目已存储到DB,但是当验证失败时,它会加载带有一些验证消息的create() 。 我需要在表单中加载所选类别的祖先,这应该通过知道category_id元素的提交值来实现。

到目前为止我尝试了什么:

试图在创建中添加参数,如下所示:

public function create(Requests\PostsRequest $request){...}

但它进入了无限循环。 我理解为什么因为控制器的资源定义而且它“认为”它是存储方法。

试图打印外观Input::all()但它是空的

试图做:

public function create(){ $request = new \Request(); //print $request }

但它不起作用,我认为这是正确的方式。

因此,任何想法如何在控制器中获得这些值都将受到高度赞赏。

I am new to Laravel world and I have a problem which I can't solve so far.

My question is: How to access the submitted values in the controller's create() function after the validation fails?

Here is what I have:

PostsController, defined as resource in the routes.php In the controller I have usual methods create() and store() etc.

Here is some code:

//PostsController public function create() { //Here I need to get the inputs return view('posts.create'); } public function store(Requests\PostsRequest $request) { //store actions }

I have created PostsRequest class which has some rules like:

public function rules(){ $rules = [ 'category_id'=>'required', 'name'=>'required|min:3', 'description'=>'required' ]; return $rules; }

If everything is fine the entry has been stored to the DB, but when the validation fails, it's loaded the create() with some validation messages. I need to load ancestors of the selected category in the form and this should happen by knowing the submitted value of the category_id element.

What I've tried so far:

Tried to add parameter in the create like so:

public function create(Requests\PostsRequest $request){...}

But it went to infinite loop. I understand why because of the resource definition of the controller and it "think" that it's store method.

Tried to print facade Input::all() but it's empty

Tried to make:

public function create(){ $request = new \Request(); //print $request }

But it doesn't work, neither I think it's the right way.

So any idea how to get these values in the controller would be highly appreciated.

最满意答案

如果你使用{!! Form::open() !!} {!! Form::open() !!} (或Form::model )将再次加载所有内容(重定向后)。 Laravel自动捕获“Input :: old”值。

来自:FormBuilder.php(照亮/ html包 - 版本5.0)

/** * Get the value that should be assigned to the field. * * @param string $name * @param string $value * @return string */ public function getValueAttribute($name, $value = null) { if (is_null($name)) return $value; if ( ! is_null($this->old($name))) { return $this->old($name); } if ( ! is_null($value)) return $value; if (isset($this->model)) { return $this->getModelValueAttribute($name); } }

如果您不想使用illuminate / html,只需添加您的value属性:

Input::old('input_name')

如果您想获得请求。 只需添加到您的create方法即可。

public function create(Request $request){ $data = $request->all(); //or whatever you want.. }

If you use {!! Form::open() !!} (or Form::model) everything will be loaded again (after redirect back). Laravel automatically capture the "Input::old" values.

From source: FormBuilder.php (illuminate/html package - Version 5.0)

/** * Get the value that should be assigned to the field. * * @param string $name * @param string $value * @return string */ public function getValueAttribute($name, $value = null) { if (is_null($name)) return $value; if ( ! is_null($this->old($name))) { return $this->old($name); } if ( ! is_null($value)) return $value; if (isset($this->model)) { return $this->getModelValueAttribute($name); } }

If you don't want to use illuminate/html, just add at your value attribute:

Input::old('input_name')

If you want to get the Request. Just add to your create method.

public function create(Request $request){ $data = $request->all(); //or whatever you want.. }

更多推荐

本文发布于:2023-04-29 03:01:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1334529.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:表单   器中   form   validation   controller

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!