问题
I have an input field that I am adding dynamically with jQuery, it is the input field for video links, that a user can add as many times as he wants to, and I need to check if the url is valid for all the fields if something is written in them, I also need to leave it as an optional field. I was trying to do it by following this example but that is obviously not working for me:
This is my form field:
{!! Form::text('external_media[]', null,['class' => 'form-control col-lg-10 external-media-input']) !!}
And in my controller store function I was trying to validate it like this:
$this->validate($request->all(), [
'external_media.*' => 'present|active_url',
]);
When I am sending data from the create form, $request->all() looks like this:
array:11 [▼
"_token" => "JQXZjFEs3ETgVqh2izcmJx1h3sGryFvDkzGGtVAd"
"external_media" => array:3 [▶]
"category" => "1"
"type" => "0"
"title" => "sdbvsdb"
]
But I get the error:
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
I have also tried with making the validation with rules in a requests file like this:
$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;
But then when I have a blank field for external_media, I get this error:
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)
回答1:
$inputs = request()->all();
$validator = Validator::make($inputs, [
'external_link.*' => 'required|active_url',
]);
dd($validator->messages());
Don't forget to use Validator; in your file.
回答2:
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
That just means what it says. You should be passing the $request object not an array from $request->all()
$this->validate($request, ....);
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)
This is probably because the Html and Form package from LaravelCollective doesn't handle arrays. It expects every field to be a single value.
Update
The fact that you are getting redirected back with errors means the validation is failing. Dump the validation errors if they exist to see if it is working as expected, before you get to the form. This will avoid this error with the FormBuilder you are seeing. Once you can confirm if your validation is working as expected you can then move onto getting around this issue with the FormBuilder.
回答3:
plz try like below from 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...
}
回答4:
as say the laravel 5.2 documentation : https://laravel.com/docs/5.2/validation
If you do not want to use the ValidatesRequests trait's validate method, you may create a validator instance manually using the Validator facade. The make method on the facade generates a new validator instance:
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...
}
}
来源:https://stackoverflow.com/questions/37140956/laravel-5-2-array-validation