Custom Validation

Hi All

I am trying to do custom validation but the error is not getting displayed.

steps i am following are

  1. defining rule in model array

  2. defining the function used in the model rule.

  3. displaying it in view form.

model.php




public function rules()

	{

		

		return array(

			

array('idAgentApplication, PreviousExperienceCustomerService, WeekHoursContribution, LanguagesToSpeak, AboutYou, ResumeURL, ResumeFileName, FacebookLink, TwitterLink, Users_id, AccountProfile_Id, AgentApplicationStatus_Id, BluePass_id, LinkedIn', 'safe', 'on'=>'search'),

array('WeekHoursContribution','cust'),

		);

}


public function cust($attribute,$error)

  {

       	 $this->addError('$attribute','my error');

	        

  }



controller.php




public function actionConsultantApplication()

	{

      		$this->render('consultantApplication');	

	}



view.php




<?php $form=$this->beginWidget('CActiveForm', array(

				'id'=>'application-form',

			//	'htmlOptions'=>array('class'=>'list_form'),

				'enableAjaxValidation'=>false,

				'enableClientValidation'=>true,

				'clientOptions'=>array('validateOnSubmit'=>true,),

)); ?>



Thanks & Regards

this rule needs to be changed

array(‘WeekHoursContribution’,‘cust’),

as

array(‘consultantApplication’,‘cust’),

??

Hi thanks for reply

but rule should contain the attribute name(WeekHourscontribution) and rule name(cust)

consultantApplication is view file name.

does the controller/action need some code?

but since buildin functions are working without any change in controller/action then why not custom functions?

Hi,

please see this link

http://help.discretelogix.com/php/yii/how-to-create-custom-validation-rule.htm

it may be helpful

Hi prats,

Is it exactly the controller code? I mean, does the method contain only one line?

If so, your custom validation method will never be called.

Note that not all validators support the client validation. Only some built-in validators that can produce a javascript for checking the value (without asking the server) are capable of the client validation.