model validation

my model contains two fields let’s call them A and B.

these are integers.

i need a validator than ensures the total of A and B is always 100.

hope to use two sliders in active form and when slider from A or B is moved the other one is moved to make the total 100

add custom validator here is an example.


/**

     * @return array the validation rules.

     */

    public function rules()

    {

        return [

            [['a', 'b'], 'validateTotal']

        ];

    }


    public function validateTotal($attribute, $params)

    {

        if (((int)$this->a + (int)$this-><img src='http://www.yiiframework.com/forum/public/style_emoticons/default/cool.gif' class='bbc_emoticon' alt='B)' /> !== 100) {

            $this->addError($attribute, 'Total of a + b must be 100');

        }

    }

JS may help you…

PFA