E
E
Evgeny Bukharev2015-02-09 14:00:07
Yii
Evgeny Bukharev, 2015-02-09 14:00:07

Is it possible to add client validation when creating custom validation rule in Yii?

Hello,
Is it possible to create a client-side validation rule for a field while creating a custom validation method in the model?
For example:

array('agentReward', 'checkRewardValidation', 'on' => self::SCENARIO_FRONT_CREATE)

public function checkRewardValidation($attribute, $params = array()) {
       /* Здесь какой либо код который добавляет валидацию поля agentReward на клиенте */
    }

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Evgeny Bukharev, 2015-02-09
@evgenybuckharev

For those who are faced with this problem, and do not want to waste time looking for a solution, it is done like this:

array('agentReward', 'checkRewardValidation', 'clientValidate' => 'clientCheckRewardValidation', 'on' => self::SCENARIO_FRONT_CREATE),

    public function clientCheckRewardValidation($attribute, $params = array()) {
        if (!isset($params['message']))
            $params['message'] = 'Value should be 123';
        $js = "if(value != '123') { messages.push('".$params['message']."'); }";
        return $js;
    }

In the "clientValidate" parameter, specify the function that returns the code for field validation on the client

M
Maxim Grechushnikov, 2015-02-09
@maxyc_webber

Hello. Yes it is possible.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question