jformvalidate + captcha = got some problem

Hi Guys,

Need some help.

Got some problem, I have a page now called it User Registration, what I did was, I use dthe JFormValidate extension to be able to validate the users input in local machine rather that server side. This could avoid to used the server processor, since we can used the client side processor using the extension. I got it correctly to validate the users input data, but got some problem when I try to get a new code for captcha, it doesn’t work at all!

regards,

Pinoy Coderz

Hi,

could you please provide more detail on the error that you had ? When you say “it doesn’t work at all” do you mean that client -side validation is not triggered ? is there some ajavscript error ? Maybe you could post the code of your form and model so I can have a look at it ?

bye

8)

Hi Raoul,

Sorry for this late reply, got busy with other project that I have.


Now this is the scenario.





in my controller I put this function





/**


* Declares class-based actions.


*/      


public function actions()


{


	return array(


		// captcha action renders the CAPTCHA image displayed on the contact page


		'captcha'=>array(


			'class'=>'CCaptchaAction',


			'backColor'=>0xFFFFFF,


		),


		// page action renders "static" pages stored under 'protected/views/site/pages'


		// They can be accessed via: index.php?r=site/page&view=FileName


		'page'=>array(


			'class'=>'CViewAction',


		),


	);


}





in my model, I call it User.





class User extends CActiveRecord


{


	public $verifyCode;





	.....





	/**


     * @return array validation rules for model attributes.


     */


	public function rules()


	{


		$CS=Yii::app()->jformvalidate;





		return array(


			.........


			


			array('verifyCode', 'captcha', 'allowEmpty'=>!CCaptcha::checkRequirements()),


			


			........


		);


	}


	


	........


}	





now in the view file.








<?php if(CCaptcha::checkRequirements()): ?>			


<table>


	<tr>


		<td colspan="2"><?php echo Yii::t('general', 'VERIFY_CODE'); ?></td>


	</tr>


	<tr>


		<td colspan="2"><?php $this->widget('CCaptcha'); ?></td>


	</tr>


	<tr>


		<td><?php echo EHtml::activeTextField($model,'verifyCode',array('class'=>'inputbox')); ?></td>											


		<td><label for="User_verifyCode" generated="true" class="invalid"><?php echo $err;?></label></td>


	</tr>


</table>				


<div class="row">


	<div class="hint"><?php echo Yii::t('general', 'CAPTCHA_MESS'); ?></div>


</div>


<?php endif; ?>





<?php if(CCaptcha::checkRequirements()): ?>


	<div class="row">


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


		<div>


		<?php $this->widget('CCaptcha'); ?>


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


		</div>


		<div class="hint">Please enter the letters as they are shown in the image above.


		<br/>Letters are not case-sensitive.</div>


	</div>


<?php endif; ?>





in my config/main.php





// autoloading model and component classes


'import'=>array(


	'application.models.*',


	'application.components.*',


	'application.extensions.jformvalidate.EHtml',


),





// application components


'components'=>array(


		.........


		


		'jformvalidate' => array (


			'class' => 'application.extensions.jformvalidate.EJFValidate',


			'enable' => true


		),		


		


		......


),








The thing is, there is no java script error or any error occure. The only problem is, yes the client side don't trigger


when going to ask a new code for the captcha. When I try not to make it in client side validation, it's work perfect, but


as what I've wanted to do, is to validate in client side machine.





Thank you with your help.

Kind regards,

Pinoy Coderz

Hi Pinoy Coderz,

the problem you have is that the CCaptcha widget creates input elements using the CHtml helper provided by Yii, and not the EHtml helper that should be used to enable client side validation … therefore, no JS validation occurs.

To make it simple, let’s say that each time you create a form field using EHtml, the jformvalidate extension retrieve its associated validation rule, and at the end of the form, it uses all rules collected to initialize the Jquery Validate plugin who is in charge of client side validation. In this case, CCaptcha create the input text using CHtml, so the jformvalidate extension is not aware that this field should be client side validated…

On the other hand, I don’t really understand why you need client-side validation for captcha, when by design, captcha validation must occurs on server side… I guess the only rule you want to test is ‘required’ right ? In this case I’m sorry, but there is no way currently to make it work like you want …(unles you overload the CCaptcha Widget and replace CHtml with EHtml … but this needs testing ;) )

ciao

8)

Hi Raoul,

Thank you with your clear explanation.

Probably I need to change something in the code.

This really help a lot of my confusions.

ciao ;)

kind regards,

Pinoy Coderz