OctoberCMS image(file) validation not working properly

久未见 提交于 2019-12-11 05:57:27

问题


I am trying to put image(file) validations in my model file but it seems to be not working as I want.Here below is my scenario.

fields.yaml

fields:
    slider_image:
        label: 'Slider Image (jpg,png,gif) (1920 X 600)'
        mode: image
        fileTypes: 'jpeg,jpg,png,gif'
        useCaption: true
        thumbOptions:
            mode: crop
            extension: auto
        span: auto
        required: 1
        type: fileupload

Model.php

public $rules = [    
'slider_image' => 'required|mimes:jpeg,jpg,png,gif',
];

public $customMessages = [                
            'slider_image.required' => 'Please select slider image',                
            'slider_image.mimes' => 'Please select valid slider image',                
];

As you can clearly see here I have a file upload option called as slider_image and in my .yaml file I have put validations to upload only jpeg,jpg,png,gif

But the issue is with my validation rules.

Even if I upload any other extension file (i.e. .zip) I m always having an error message saying

Please select slider image

but it should rather display below error as I have already invalid extension file.

Please select valid slider image

If I do not upload any image then it should display first error and if I upload an invalid image then it should display second error.

Can someone tell me what is wrong in my current scenario here ?

Additionally, I wanted to know if there is any validation rule available through which we can check height width of the image and set rule of minimum height width to upload and put validation message based on it in our model file.

Thanks


回答1:


There are two levels of validation at play here. One is the built-in validation functionality of the file upload widget - that is what is configured in your fields.yaml in the widget options, i.e.

    mode: image
    fileTypes: 'jpeg,jpg,png,gif'

and that is happening on the client side. The fileuplod widget will then already point out to you (on the client side) that this type is not allowed if you upload for example a zip file, because: 1. You have specified mode: image. 2. You have specified filetypes: jpeg,jpg,png,gif. Your file upload will be simply not accepted on the client side, and so when submitting the form the file input will just be empty.

And so your second level of validation (specified by your $rules in Model.php), which is taking place after the form is submitted on the server side will then get an empty file input (because the upload widget has not even forwarded the type of file that you tried to uploaded), and will therefore answer with 'Please select slider image'.

So the validator is working correctly. Everything is working correctly. But if you want the mime validation in your Model.php to respond to unwanted file types you would have to remove the

fileTypes: 'jpeg,jpg,png,gif'

from your fields.yaml, because otherwise they won't even get through to your model validation.

To your second question: No, there is no such inbuilt validation for proper height and width of an image. But you can specify "imageHeight" and "imageWidth" in your fields.yaml to resize the uploaded image automatically.



来源:https://stackoverflow.com/questions/40926676/octobercms-imagefile-validation-not-working-properly

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!