Skip to content

Instantly share code, notes, and snippets.

@steppefox
Created November 4, 2013 08:07
Show Gist options
  • Save steppefox/7299533 to your computer and use it in GitHub Desktop.
Save steppefox/7299533 to your computer and use it in GitHub Desktop.
Yii list of some fast validators

**** Validators ****

array('name','match','pattern' => '/^[a-zA-Z\s]+$/','message' => 'Только английские буквы'),
array('name','filter', 'filter'=>'strtolower'),
array('image','file','allowEmpty'=>true,'maxFiles'=>10,'types'=>'png,jpg,jpeg'),
array('text','filter','filter'=>array($obj=new CHtmlPurifier(),'purify')),

**** Own validator ****

public function passwordStrength($attribute,$params)
{
    if ($params['strength'] === self::WEAK)
        $pattern = '/^(?=.*[a-zA-Z0-9]).{5,}$/';  
    elseif ($params['strength'] === self::STRONG)
        $pattern = '/^(?=.*\d(?=.*\d))(?=.*[a-zA-Z](?=.*[a-zA-Z])).{5,}$/';  
 
    if(!preg_match($pattern, $this->$attribute))
      $this->addError($attribute, 'your password is not strong enough!');
}

**** XSS ****

$p = new CHtmlPurifier();
$p->options = array('URI.AllowedSchemes'=>array(
  'http' => true,
  'https' => true,
));
$text = $p->purify($text);
 
//-------------------
//Валидация как фильтра
array('text','filter','filter'=>array($obj=new CHtmlPurifier(),'purify'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment