1

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)
5
  • Why are you not using active_url validation for validating urls? laravel.com/docs/5.2/validation#rule-active-url Commented May 10, 2016 at 15:16
  • Or this laravel.com/docs/5.2/validation#rule-url Commented May 10, 2016 at 15:16
  • I have changed that, but that is obviously not a problem, I get that error because there is some issue with array, it doesn't even go into validation part. Maybe the title doesn't reflect what the real problem is, I will edit the question. Commented May 10, 2016 at 15:20
  • Ok. Tell me at which line and which file you get this error Commented May 10, 2016 at 15:21
  • I get it in the form where I create the article and the error is for the input field that I posted in the question. It happens when I try to submit that form with the blank field that I mentioned. Commented May 10, 2016 at 19:54

4 Answers 4

3
+50
$inputs = request()->all();
$validator = Validator::make($inputs, [
  'external_link.*' => 'required|active_url',
]);

dd($validator->messages());

Don't forget to use Validator; in your file.

Sign up to request clarification or add additional context in comments.

1 Comment

how can one display customize message for each array fields
0

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.

2 Comments

Yes, but in this case I need to pass as an array, so that is why I am wondering how to validate an array.
Validating the array and the Form Helper trying to deal with the field are two different things. Im just trying to point out why you are getting the errors you are getting so you can get back to figuring out the actual validation part :).
0

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...
}

Comments

-2

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...
    }
}

1 Comment

Yes, but how to do an array validation then in the controller, if you could show the code?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.