How To Properly Use Clientvalidation Attribute

What is correct way of using $htmlOptions[‘clientValidation’] attribute for CActiveForm.error() function?

Citing documentation:

[size=2]OK. So I’m using a code like this:[/size]


<?php 

    $validationArray = array

    (

        'hideErrorMessage'=>TRUE,

        'clientValidation'=>'js:afterValidateAttribute'

    );

?>


<div class="row">

    <?php echo($form->labelEx($model, 'name')); ?>

    <?php echo($form->textField($model, 'name')); ?>

    <?php echo($form->error($model, 'name', $validationArray)); ?>

</div>

[size=2]And a very strange things happens.[/size]

Not only, my Javascript function [size="2"]afterValidateAttribute() is not being called / executed, but also, when I take a look into source code, I see some garbage.[/size]

[size="2"]Error fields looks like this:[/size]

[size="2"]


<div clientValidation="js:afterValidateAttribute" class="errorMessage" id="Contact_subject_em_" style="display:none"></div>

[/size]

[size="2"]so Yii is rendering [/size][size="2"]clientValidation as a normal attribute. Also it does not stop serving default (built-in) client-side validator (it should supply my own, if I understand these things correctly), but in adds a strange string inside. So particular client-side validator looks like this:[/size]

[size="2"]


'clientValidation':function(value, messages, attribute)

{

	js:afterValidateAttribute


	if($.trim(value)=='')

	{

		messages.push("Field can't be empty!");

	}


	if($.trim(value)!='')

	{

		if(value.length>70)

		{

			messages.push("Field's content is too long");

		}

	}

}

[/size][size="2"]Is this some kind of bug?[/size]

[size=“2”]If I’m not missing something obvious or doing something very wrong, [/size][size=“2”]using[/size] $htmlOptions[‘clientValidation’] [size=“2”]attribute is completely useless for me. I managed to apply my own, custom client-side validation only by turning build-in client validation off ([/size]hideErrorMessage) and using afterValidateAttribute. I failed completely doing this easier way, i.e. using clientValidation[size=“2”].[/size]

Dear Friend

Thanks for unearthing the property clientValidation in htmlOptions of CActive::form() method.

I was not aware of its existance.

I found that the following is the successful way of utilizing that option.




echo $form->error($model,'location',

	array('clientValidation'=>'js:

        if($.trim(value)=="")

        {

                messages.push("Field can\'t be empty!");

        }


        if($.trim(value)!="")

        {

                if(value.length>7)

                {

                        messages.push("Field\'s content is too long");

                }

        }

'),false,true); 



We have to set enableClientValidation as true.

clientValidation is set as attribute.The attribute value is inserted into the body of the function

with following signature.




function(value, messages, attribute) 

{


    htmlOptions['clientValidation']; //piece of javascript.


    //This is followed by javascript renderd by custom validators in which client side validation is available. 

    

    $validator1->clientValidation->js;

    $validator2->clientValidation->js;

    ..................................

}



Thanks again.

Thank you for your answer. It has cleared out some things for me.

First of all, my private client-side validation function wasn’t called-up, because I made an error of using functionName instead of functionName().

Second of all, there is unclear information in documentation. I assumed that using clientValidation code replaces build-in one, while (as you showed) it actually is added to original one.

Finally, the fact that Yii renders <span clientValidation is a confirmed bug and it will be fixed soon.

Thanks again!