Yii Framework Forum


(Lmartord) #1

I want to perform ClientSideValidation on a certain field in a form, based on a custom validation.
ServerSide is working well for the field, but cannot get the validation functioning properly on Client Side.
Custom Validation is in MemberValidation Class that extends Validator.
ServerSide is working properly:

public function validateAttribute($model, $attribute)

	if(!$this->isValidIdNumber($model->$attribute)) {
        $this->addError($model, $attribute, 'The '.$attribute.'= {value} must be either "{NIF}" or "{CIF}".', ['NIF' => '12345678Z', 'CIF' => 'A87654321']);

Function returns true or false.
But trying to make same method working in ClientSide is something I don’t understand.
I want to pass field value to same function onChange. I get the message and the value, but cannot trigger the function.
Already tried so many paths without success:

public function clientValidateAttribute($model, $attribute, $view)
if(! $this->isValidIdNumber($model->$attribute)) {
        $message = json_encode($this->message .'=>'. $attribute, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
        return <<<JS
}// end if $this->isValidIdNumber($model->$attribute)

I tried to call ‘isValidIdNumber’ in PHP assigned a value, checked if is false inside the return <<<JS, encapsulate everything inside an if, …
I get $message and value properly below field.
How can I call same function inside the ‘return <<<JS’?
Only thing left is rewrite validation in Javascript and trigger it from onChange.
Any clues?
Thanks in advance.

(Lmartord) #2

Silly (stupid) mistake. It should be a Javascript validation. Solved writing validation function in Javascript.