Validate character delimited email strings and return all errors.
Requirements ¶
This extension has been tested with Yii Framework 1.1.5
Usage ¶
Place the extension in your protected\extensions directory and add it to your model like the following:
/**
* ShareReportForm class.
*/
class ShareReportForm extends CFormModel
{
public $emails;
/**
* Declares the validation rules.
*/
public function rules()
{
return array(
array('emails', 'required'),
array('emails', 'ext.MultiEmailValidator', 'delimiter'=>',', 'min'=>1, 'max'=>5),
);
}
/**
* Declares customized attribute labels.
* If not declared here, an attribute would have a label that is
* the same as its name with the first letter in upper case.
*/
public function attributeLabels()
{
return array(
'emails'=>'Email To',
);
}
}
Client side validation
Everything works great except client side validation (which I guess is inherited from parent Class)
This should work for client side vaildation
add the following to the model file
/** * 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 * @since 1.1.7 */ public function clientValidateAttribute($object,$attribute) { $message=$this->message!==null ? $this->message : Yii::t('yii','{attribute} is not a valid email address.'); $message=strtr($message, array( '{attribute}'=>$object->getAttributeLabel($attribute), )); $condition="!result[i].match({$this->pattern})"; if($this->allowName) $condition.=" && !value.match({$this->fullPattern})"; return "var result = value.split('".$this->delimiter."'); var mailsBad = false; for(var i = 0;i < result.length;i++) { if(".($this->allowEmpty ? "$.trim(result[i])!='' && " : '').$condition.") { mailsBad = true; } if(mailsBad){messages.push(".CJSON::encode($message).");}}"; }
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.