A
A
Artur Kosikov2018-06-04 14:39:52
Yii
Artur Kosikov, 2018-06-04 14:39:52

Yii2 custom validation in Activeform - how to display an error message?

There is an Activeform with an offer_maximum field that needs to be checked...

public function rules()
    {
        return [
      ...
            [ 'offer_minimum', 'number', 'min' => 100],
            ['offer_maximum', 'getPossibleMaximum'],
        ];
    }

        public function getPossibleMaximum($attribute){
            $this->addError($attribute, 'Недостаточно средств на балансе!');
        }

I expect that when filling in the offer_maximum field , an error message will be generated, as it happens for the offer_minimum field, but this rule is not added to the Ajax validation script.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Maxim Timofeev, 2018-06-04
@atillus

1. do not call the validator as a getter,
"possibleMaximum" is a good option
"checkPossibleMaximum" is also "
getPossibleMaximum" - not correct
the model is transferred.
So

public function getPossibleMaximum($attribute){
            $this->addError($attribute, 'Недостаточно средств на балансе!');
        }

change to
public function possibleMaximum($modelKakNeNazovi,$attributeHotTiTresni){
            $this->addError($attributeHotTiTresni, 'Недостаточно средств на балансе!');
        }

PS: The example you described should work only on the server, for client-side validation everything is a little more complicated: https://www.yiiframework.com/doc/guide/2.0/ru/inpu...

A
Artem, 2018-06-04
@proudmore

If you are using some script other than the default, you may have forgotten to add the attribute to the validation list

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question