smeto
(Matej Smeto)
September 7, 2009, 12:44pm
1
Hello,
sorry in advance for stupid question but i just cant find answer in docs:
could anyone post an example on how to set rules in activeRecord class, to achieve Regex pattern validation for some attribute.
i mean something like:
public function rules()
{
return array(
array('attribute','pattern'=>'someRegexPattern'),
);
}
so could you post an example rules array also with some easy sample regex (lets say email regex)?
any help highly appreciated
Smeto
Dave
(Deventer)
September 7, 2009, 1:46pm
2
If you want an email validator you can simply use the email validator
The pattern you be provided as follows: ‘/<your pattern>/’. For example:
public function rules()
{
return array(
array('attribute','pattern'=>'/[a-zA-Z0-9]{2,}/'),
);
}
The given pattern requires at least to alphanumeric characters.
Badger
(Stephen Lowry)
September 8, 2009, 12:38am
4
This is in rules():
array(‘content’, ‘application.extensions.urivalidator.EUriValidator’, ‘mode’=>‘web’)
This is how I am using the URI Validation Extension. The “'application.extensions.urivalidator.EUriValidator” points to a file with a method with something like
if (!preg_match("/^(https?):\/\/(?<img src='http://www.yiiframework.com/forum/public/style_emoticons/default/sad.gif' class='bbc_emoticon' alt=':(' />(?:[a-z0-9\-\._\~!\$\&\'\(\)\*\+\,\;\=\:]|%[0-9A-F]{2})*)\@)?((?:[a-z0-9\.\-]|%[0-9A-F]{2}){3,})(?:<img src='http://www.yiiframework.com/forum/public/style_emoticons/default/sad.gif' class='bbc_emoticon' alt=':(' />\d+))?((?:\/(?:[a-z0-9\-\.\_\~\!\$\&\'\(\)\*\+\,\;\=\:\@]|%[0-9A-F]{2})*)*)(?:\?((?:[a-z0-9\-\.\_\~\!\$\&\'\(\)\*\+\,\;\=\:\/\?\@]|%[0-9A-F]{2})*))?(?:\#((?:[a-z0-9\-\.\_\~\!\$\&\'\(\)\*\+\,\;\=\:\/\?\@]|%[0-9A-F]{2})*))?$/i", $uri, $parts)) {
return false;
}
The ‘mode’=>‘web’ picks which method to use in that file.
Content is the thing to be checked
smeto
(Matej Smeto)
September 8, 2009, 12:39am
5
thanx a lot man, it worked the same way for my problem…