我有一个输入字段,我用jQuery动态添加,它是视频链接的输入字段,用户可以添加他想要的次数,我需要检查网址是否对所有人都有效如果字段中写有字段,我还需要将其保留为可选字段。我试图按照this example进行操作,但这显然不适合我:
这是我的表格字段:
{!! Form::text('external_media[]', null,['class' => 'form-control col-lg-10 external-media-input']) !!}
在我的控制器存储功能中,我试图像这样验证它:
$this->validate($request->all(), [
'external_media.*' => 'present|active_url',
]);
当我从创建表单发送数据时,$request->all()
看起来像这样:
array:11 [▼
"_token" => "JQXZjFEs3ETgVqh2izcmJx1h3sGryFvDkzGGtVAd"
"external_media" => array:3 [▶]
"category" => "1"
"type" => "0"
"title" => "sdbvsdb"
]
但我收到错误:
FatalThrowableError in ValidatesRequests.php line 49:
Type error: Argument 1 passed to App\Http\Controllers\Controller::validate() must be an instance of Illuminate\Http\Request, array given, called in /home/vagrant/Projects/myProject/app/Http/Controllers/Admin/Articles/ArticlesController.php on line 66
我还试过在这样的请求文件中使用规则进行验证:
$rules = [
'title' => 'required',
'text' => 'required',
//'image' => 'required|image|max:20000',
];
foreach($this->request->get('external_media') as $val)
{
$rules[$val] = 'present|active_url';
}
return $rules;
但是当我为external_media写了一个空白字段时,我收到了这个错误:
ErrorException in helpers.php line 531:
htmlentities() expects parameter 1 to be string, array given (View: /home/vagrant/Projects/iCoop5.2/resources/views/admin/articles/create.blade.php)
答案 0 :(得分:3)
$inputs = request()->all();
$validator = Validator::make($inputs, [
'external_link.*' => 'required|active_url',
]);
dd($validator->messages());
不要忘记文件中的use Validator;
。
答案 1 :(得分:0)
ValidatesRequests.php第49行中的FatalThrowableError: 输入错误:参数1传递给App \ Http \ Controllers \ Controller :: validate()必须是Illuminate \ Http \ Request的实例,给定数组,在/ home / vagrant / Projects / myProject / app / Http / Controllers /中调用第66行的Admin / Articles / ArticlesController.php
这就意味着它所说的。您应该从$request
$request->all()
对象而不是数组
$this->validate($request, ....);
helpers.php第531行中的ErrorException: htmlentities()期望参数1为字符串,给定数组(查看:/home/vagrant/Projects/iCoop5.2/resources/views/admin/articles/create.blade.php)
这可能是因为LaravelCollective的Html和Form包不处理数组。它希望每个字段都是单个值。
<强>更新强>
您被重定向到错误的事实意味着验证失败。转储验证错误(如果它们存在)以查看它是否按预期工作, 之前到达表单。这将避免您看到的FormBuilder出现此错误。一旦您确认验证是否按预期工作,您就可以继续使用FormBuilder解决此问题。
答案 2 :(得分:0)
请点击laravel docs下面的内容
https://laravel.com/docs/5.2/validation
public function store(Request $request)
{
$this->validate($request, [
'title' => 'required|unique:posts|max:255',
'body' => 'required',
]);
// The blog post is valid, store in database...
}
答案 3 :(得分:-2)
如laravel 5.2文档所述: https://laravel.com/docs/5.2/validation
如果您不想使用ValidatesRequests特征的validate方法,可以使用Validator外观手动创建验证器实例。 Facade上的make方法生成一个新的验证器实例:
namespace App\Http\Controllers;
use Validator;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
class PostController extends Controller
{
/**
* Store a new blog post.
*
* @param Request $request
* @return Response
*/
public function store(Request $request)
{
$validator = Validator::make($request->all(), [
'title' => 'required|unique:posts|max:255',
'body' => 'required',
]);
if ($validator->fails()) {
return redirect('post/create')
->withErrors($validator)
->withInput();
}
// Store the blog post...
}
}