Laravel Validator :: validateString | max不存在错误

时间:2018-08-26 05:33:48

标签: validation laravel-5 dimension

在Laravel 5.6.33应用程序中,我需要使用规则检查上传文件的尺寸

    'avatar' => [
        'string|max:50',
        Rule::dimensions()->maxWidth(96)->maxHeight(64)->ratio(3 / 2),
    ],

但是我得到了错误:

Method Illuminate\Validation\Validator::validateString|max does not exist.

在规则声明中,我添加了一行:

use Illuminate\Validation\Rule;

但是我还是有错误...

我想念什么?

已修改2: 我在您的评论中看到了重点: 我在选择文件时使用js函数输入文件

<input type="file" style="display: none" ref="file_input" accept="avatar/*" @change="onImageSelected">

,当我选择一个文件时,将其保存在this.user_avatar_url var中并显示在表单上

    onImageSelected(event) {
        this.is_page_updating = true
        const files = event.target.files
        let filename = files[0].name
        this.user_avatar = filename;
        if (filename.lastIndexOf('.') <= 0) {
            return alert('Select valid avatar !')
        }
        const fileReader = new FileReader()
        fileReader.addEventListener('load', () => {
            this.user_avatar_url = fileReader.result
        })
        fileReader.readAsDataURL(files[0]);
        this.is_page_updating = false
    },

user_avatar_url的内容类似

data:image/png;base64,iVBORw0KGgoAAAAN
...
LSOiCLN/wIWijWu5oCk7wAAAABJRU5ErkJggg==

并设置规则:

'avatar' => [
    'string',
    'max:100',
],
'avatar_url' => [
    Rule::dimensions()->maxWidth(2896)->maxHeight(2864),
],

我有同样的错误。看起来这是错误的方式... 哪种方法正确?

1 个答案:

答案 0 :(得分:2)

您应该这样写:

'avatar' => [ 'string', 'max:50', Rule::dimensions()->maxWidth(96)->maxHeight(64)->ratio(3 / 2), ],