OctoberCMS图像(文件)验证无法正常工作 [英] OctoberCMS image(file) validation not working properly

查看:86
本文介绍了OctoberCMS图像(文件)验证无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将图像(文件)验证放入模型文件中,但它似乎无法按我的意愿工作.这是我的情况.

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',                
];

您可以在这里清楚地看到我有一个名为 slider_image 的文件上传选项,并且在我的 .yaml 文件中,我已经将验证仅上传了 jpeg, jpg,png,gif

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.

即使我上传了其他任何扩展名文件(即 .zip ),我也总是会收到一条错误消息,提示

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.

谢谢

推荐答案

此处有两个验证级别.一种是文件上传小部件的内置验证功能-这是在小部件选项(即

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'

,这是在客户端发生的.然后,fileuplod小部件将已经(在客户端上为 )向您指出,如果您上传例如zip文件,则不允许这种类型,因为:1.您指定了mode: image. 2.您已指定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.

因此,您的 second 验证级别(由您在 Model.php 中的$规则指定)是在表单上的提交之后进行的.服务器端将获得文件输入(因为上传小部件甚至都没有转发您尝试上传的文件类型),因此将回答请选择滑块图像'.

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

因此验证器正常工作.一切正常.但是,如果您希望 Model.php 中的mime验证响应不需要的文件类型,则必须删除

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'

来自您的 fields.yaml ,因为否则它们甚至都无法通过您的模型验证.

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

第二个问题:不,对于适当的图像高度和宽度,没有这样的内置验证.但是,您可以在fields.yaml中指定"imageHeight"和"imageWidth",以自动调整上传图像的大小.

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.

这篇关于OctoberCMS图像(文件)验证无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆