0

フィールド「CompanyName」があり、検証が必要です。
これは私が持っているものです:

public function rules() {
    return array(
        array('CompanyName', 'compare', 'compareValue' => "google", 'operator' => '!=',    'message' => Yii::t('app', 'NOT Google'), 'on' => 'submit'),
    );
}


生成された JS :

if(value=="google") {
    messages.push("You wrote google!");
}

私が欲しいもの(js側のトリムを含む):

if($.trim(value)=="google") {
    messages.push("You wrote google!");
}

これどうやってするの ?

4

2 に答える 2

3

CFilterValidatorをチェックしてください。私はあなたがこのようなものを探していると思います:

array('CompanyName', 'filter', 'filter'=>'trim')
于 2013-01-29T14:24:55.620 に答える
0

誰かが同じ問題に直面する場合、これは私が使用したソリューションです:*クライアント側のみを実装するカスタムバリデーター

class EspecialValidator extends CValidator {

public $message;
public $compareValue;

/**
 * Validates the attribute of the object.
 * If there is any error, the error message is added to the object.
 * @param CModel the object being validated
 * @param Array the attribute being validated
 */
protected function validateAttribute($object, $attribute) {
    parent::validateAttribute($object, $attribute);
}

/**
 *  Returns the JavaScript needed for performing client-side validation
 * @param type $object
 * @param type $attribute
 */
public function clientValidateAttribute($object, $attribute) {
    $js = '
            if($.trim(value)=="' . $this->compareValue . '") {
                    messages.push("' . $this->message . '");
            }
        ';
    return $js;
}

}


rules から、次のように使用します。

array('fieldToCheck', 'ext.validators.EspecialValidator', 'compareValue' => 'TextToCompare', 'message' => 'failure message')
于 2013-01-30T09:10:46.623 に答える