1

I tried googling and saw other questions posted at this forum but could not find any solution for my issue. I am using Jquery ajaxForm method to submit form. My form contains one file field too in the form that can be used to upload a picture. I have defined the validation in my model. But the issue is even i am uploading a correct jpg file, still i am getting error message that

Argument 1 passed to Illuminate\\Validation\\Factory::make() must be of the type array, object given.

Javascript Code

$('#create_form').ajaxForm({
    dataType:'JSON',
    success: function(response){
        alert(response);    
    } 
}).submit();

Controllder Code

if ($file = Input::file('picture')) {
    $validator = Validator::make($file, User::$file_rules);

    if ($validator->fails()) {
        $messages = $validator->messages();
        foreach ($messages->all(':message') as $message) {
            echo $message; exit;
        }
        return Response::json(array('message'=>$response, 'status'=>'failure'));
    } else {
        // do rest 
    }
}

Model Code

public static $file_rules = array(
    'picture' => 'required|max:2048|mimes:jpeg,jpg,bmp,png,gif'
);

POST Request

enter image description here

I know that my validation defined in the model expects an array. But by passing $file in the validator, an object is passed. Then i changed the code like:

$validator = Validator::make(array('picture' => $file->getClientOriginalName()), User::$file_rules);

Now i am getting error:

The picture must be a file of type: jpg, JPEG, png,gif.

2 Answers 2

2
+50

The problem is you pass file object directly to validate. Validator::make() method takes all four parameters as array. Moreover, you need to pass the whole file object as value so that Validator can validate mime type, size, etc. That's why your code should be like that.

$input = array('picture' => Input::file('picture'));
$validator = Validator::make($input, User::$file_rules);

if ($validator->fails()) {
    $messages = $validator->messages();
    foreach ($messages->all(':message') as $message) {
        echo $message; exit;
    }
    return Response::json(array('message'=>$response, 'status'=>'failure'));
} else {
    // do rest 
}

Hope it will be useful for you.

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

4 Comments

After replacing your code, i am getting correct error for file extension but when i upload a bulky file having size greater than allowed size of 200kb then i get the error "The picture field is required." I could not understand why this error is coming.
I have no problem when I am testing with postman. Let me know detail to solve it.
I have updated my code of model in my question. So when i try to upload an image having size greater than 2MB then i get the error message "The picture field is required". Can increasing size of max_upload_size may help?
Yes . This happen because of this issue. github.com/laravel/framework/issues/2433 Please increase post_max_size and upload_max_filesize to fix your problem.
1

Try rule like this.

$rules = array(
 'picture' => 'image|mimes:jpeg,jpg,bmp,png,gif'
);

or try removing 'mimes'

Comments

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.