javascriptphpvalidationyiiyii-validation

Yii compare rule have no trim


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 ?


Solution

  • 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')