با سلام
شما میتوانید validation مخصوص به خود را در Yii بنویسید. در فولدر extension کلاس CodeMelli را ایجاد نمایید که از کلاس CValidator ارث می برد.
کلاس CValidator دارای دو متد می باشد که باید آنها را بازنویسی کنید.
متد validateAttribute برای اعتبار سنجی سمت سرور
متد clientValidateAttribute برای اعتبار سنجی سمت کلاینت
class CodeMelli extends CValidator {
/**
* Validates the attribute of the object.
* If there is any error, the error message is added to the object.
* @param CModel $object the object being validated
* @param string $attribute the attribute being validated
*/
protected function validateAttribute($object,$attribute)
{
// extract the attribute value from it's model object
$value=$object->$attribute;
// add your code for validation
if(!$validate)
{
$this->addError($object,$attribute,'کد ملی معتبر نیست');
}
}
/**
* Returns the JavaScript needed for performing client-side validation.
* @param CModel $object the data object being validated
* @param string $attribute the name of the attribute to be validated.
* @return string the client-side validation script.
* @see CActiveForm::enableClientValidation
*/
public function clientValidateAttribute($object,$attribute)
{
return "
// add your code for client validation (javaScript)
if(!validate) {
messages.push(".CJSON::encode('کد ملی معتبر نیست').");
}
";
}
}
حالا در مدل قسمت rule کد زیر را قرار دهید
/**
* @return array validation rules for model attributes.
*/
public function rules()
{
return array(
array('password', 'ext.CodeMelli', ),
);
}
فقط وقتی یک form ایجاد میکنید مقدار enableClientValidation را برابر true قرار دهید
<?php $form = $this->beginWidget('CActiveForm', array(
'id'=>'form',
'enableAjaxValidation'=>true,
'enableClientValidation'=>true,
)); ?>
با این کار کدهایی را که به صورت javaScript نوشته اید در صفحه ای که فرم ایجاد می کنید برای اعتبار سنجی سمت کلاینت نیز در صفحه بارگذاری می شود