1

In my Laravel 5.6.33 application I need to check dimension of an uploaded file with rule

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

But I got error:

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

In declarations of rules I added a line:

use Illuminate\Validation\Rule;

But I have the error anyway...

What did I miss?

MODIFIED 2 : I see point in your comments: I have file input with js function on file selection

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

and when I selecte a file I keep it in this.user_avatar_url var and show on the form

    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
    },

the user_avatar_url has comntent like

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

and setting the rule :

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

I have the same error. Looks like that is wrong way... Which is the correct way?

2
  • You didn't show us your full validation code...
    – Akbar Soft
    Aug 26, 2018 at 5:49
  • what input you was validating? is it a string input?
    – Bagus Tesa
    Aug 26, 2018 at 6:24

1 Answer 1

3

You should write it like that:

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

4
  • your answer is good, though it left me wondering... is it a string.. or is it an image..
    – Bagus Tesa
    Aug 26, 2018 at 6:26
  • my answer is about syntax, i also wonder about validating file as a string Aug 26, 2018 at 6:36
  • Please, look at MODIFIED 2 block of my first post.
    – user2054381
    Aug 27, 2018 at 11:45
  • change 'avatar_url' for 'user_avatar_url' in validation rules Aug 28, 2018 at 10:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.