[[+content_image]]
I
I
Ivan Komarov2017-06-07 10:22:32
Yii
Ivan Komarov, 2017-06-07 10:22:32

YII2 - How to set custom message for "Image" validation rule?

Can't set custom message for 'image' validation rule.
No matter what I do, the message "The file "test.jpg" is not an image."
Code example:

public function rules()
    {
        return [
            [['photoFacet', 'photoProfile'], 'required', 'message' => 'This field can not be blank'],
            [['photoFacet', 'photoProfile'], 'image', 'message' => 'My own message']
        ];
    }

At the same time, my message is displayed for the required rule, but not for image.
Does anyone know how to overcome this?

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
M
Maxim Timofeev, 2017-06-07
@FreeTibet

As evgenybuckharev said, you set the wrong one. We read the docs
:
www.yiiframework.com/doc-2.0/yii-validators-imagev... we see the $notImage
property in the Public Properties , we read the docs to it: www.yiiframework.com/doc-2.0/ yii-validators-imagev... we understand, we excite the neural activity of the brain for 2 minutes:

public function rules()
    {
        return [
            [['photoFacet', 'photoProfile'], 'required', 'message' => 'This field can not be blank'],
            [
                ['photoFacet', 'photoProfile'], 
                'image',
                'message' => 'My own message',
                'notImage' => 'My not image message'
            ]
        ];
    }

E
Evgeny Bukharev, 2017-06-07
@evgenybuckharev

\yii\validators\ImageValidator
We look at this file and see that the given text of the message changes in the public property notImage

K
k2lhu, 2017-06-07
@k2lhu

If I understand correctly, you need to add this rule
[['image'], 'file', 'extensions' => 'png,jpg'],

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question