Problem when validating image with Request

-1

I am developing in Laravel 5.1 and I am having a small problem when validating a form, I am using Request for this, the rules I have are the following:

ProductoCreateRequest

public function rules()
{
    return ['nombre'        =>   'required|max:50',
            'presentacion'  =>   'required',
            'unidad'        =>   'required',
            'codigo'        =>   'required|numeric|max:4',
            'almacen'       =>   'required',
            'categoria'     =>   'required',
            'pathImg'       =>   'image',
    ];
}

The problem here is that the image is taking it as it is required, what I'm looking for is that it only validates when a file was uploaded, if nothing is uploaded then it does not mark the error that it needs to be a image, as well as in code , my validation is that it is maximum of 4 digits, but it always marks me error when I write less of these, it only allows me 1 digit, when writing 2 or 3 it marks me that error.

I would appreciate your help.

    
asked by Jorge Fernandez 21.03.2017 в 01:53
source

2 answers

0

If you want the field to be validated as an image only if a file was uploaded, then you can use sometimes , that rule only validates if there is something in that field:

'pathImg' => 'sometimes|image',

You can see more information here: link

    
answered by 21.03.2017 в 02:04
0

To validate the code use digits:

//,
'codigo' => 'required|digits:4'
//,

For the validation maybe you should do it in the controller, it could be some error of the version 5.1 and 5.2 since from Laravel 5.3 the rule nullable was included with which you could do something like:

//,
'pathImg' => 'nullable|image',
//,
    
answered by 11.05.2017 в 08:50