0

I have created a simple function in App\Traits\Validate. This function simply validates image as seen.

public function validate_image($request)
{
    $check = true;
    $validate = $this->validate($request, [
        'image' => 'image|mimes:jpeg,png,jpg|max:2048',
    ]);
    dd($validate);
    if($validate->fails())
    {
        $check = false;
    }

    return $check;
}

Now when I access this function in Controller through

$check_image = $this->validate_image($request); 

I am passing the whole request variable. But still in validate_image function I am getting null. What is that I am missing? I have other functions in Validate trait and those are working fine, but this function returning null value. Please help

And yess image is the name of the file field in form

6
  • 2
    Is dd($validate) in your function as well ? Commented Aug 7, 2017 at 11:10
  • 1
    And why are you using extra function for validation of just one image field ? Commented Aug 7, 2017 at 11:13
  • @SagarGautam Yesss! I am trying to check if it is returning anything or not. Commented Aug 7, 2017 at 11:15
  • There are many images that I have to upload, if I write validation code for all of them, it would make the functions a bit messy. Commented Aug 7, 2017 at 11:16
  • While calling your custom validation function, just remove dd($validate). And you have multiple field from where images are uploaded. Am I right ? Commented Aug 7, 2017 at 11:21

3 Answers 3

4

I don't know why but try this...

first add

use Validator;

Now that you are sending $request I believe you it in array before passing to Validator

This

$data[] = $request->files;
    $validate = Validator::make($data, [
        'image' => 'image|mimes:jpeg,png,jpg|max:2048',
    ]);

Now dd() this. Hope it works.

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

1 Comment

It did work. Don't know what is the problem with validate well nvm, it works...
1

first of all make sure that your form can send files.

<form method="POST" action="" enctype="multipart/form-data">


{!! Form::open([
     'route' => '',
    'method' => 'POST',
    'files' => true
]) !!}

also create custom request. Do you really need validation in controller?

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class CustomRequest extends FormRequest
{

    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'image' => 'image|mimes:jpeg,png,jpg|max:2048',
        ];
    }
}

1 Comment

Didn't work! I have create validate function in trait file. Do you know traits?
0

Ensure that from your form you are able to handle images.
Ensure that in your opening clause you have added:

enctype="multipart/form-data"

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.