Question

I have a field "CompanyName", and i need to have validation.
this is what i have :

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


generated JS :

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

What i want ( includes trim in js side ) :

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

how can i do this ?

Was it helpful?

Solution 2

this is the solution i used , if anyone will face the same problem : * its a custom validator which implements only the client side

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;
}

}


from the rules , use it in the following:

array('fieldToCheck', 'ext.validators.EspecialValidator', 'compareValue' => 'TextToCompare', 'message' => 'failure message')

OTHER TIPS

Check out CFilterValidator. I think you're looking for something like this:

array('CompanyName', 'filter', 'filter'=>'trim')
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top