Esempio n. 1
0
 /**
  * Get a rule from the Rules array
  *
  * @param string $name The field to fetch
  * @return array An array of rules
  * @static 
  */
 public static function getRules($name)
 {
     return \Former\Former::getRules($name);
 }
Esempio n. 2
0
 /**
  * Add the corresponding rules to the field's attributes
  */
 private function addRules()
 {
     // Get the different rules assigned to this field
     $rules = Former::getRules($this->name);
     if (!$rules) {
         return false;
     }
     // Iterate through them and add the attributes
     foreach ($rules as $rule => $parameters) {
         switch ($rule) {
             case 'email':
                 $this->type = 'email';
                 break;
             case 'url':
                 $this->type = 'url';
                 break;
             case 'required':
                 $this->required();
                 break;
             case 'after':
             case 'before':
                 $format = 'Y-m-d';
                 if ($this->type == 'datetime' or $this->type == 'datetime-local') {
                     $format .= '\\TH:i:s';
                 }
                 $date = strtotime(array_get($parameters, 0));
                 $attribute = $rule == 'before' ? 'max' : 'min';
                 $this->attributes[$attribute] = date($format, $date);
                 break;
             case 'max':
                 $this->setMax(array_get($parameters, 0));
                 break;
             case 'min':
                 $this->setMin(array_get($parameters, 0));
                 break;
             case 'integer':
                 $this->attributes['pattern'] = '\\d+';
                 break;
             case 'mimes':
             case 'image':
                 if ($this->type == 'file') {
                     $ext = $rule == 'image' ? array('jpg', 'png', 'gif', 'bmp') : $parameters;
                     $mimes = array_map('File::mime', $ext);
                     $this->attributes['accept'] = implode(',', $mimes);
                 }
                 break;
             case 'numeric':
                 if ($this->type == 'number') {
                     $this->attributes['step'] = 'any';
                 } else {
                     $this->attributes['pattern'] = '[+-]?\\d*\\.?\\d+';
                 }
                 break;
             case 'not_numeric':
                 $this->attributes['pattern'] = '\\D+';
                 break;
             case 'alpha':
                 $this->attributes['pattern'] = '[a-zA-Z]+';
                 break;
             case 'alpha_num':
                 $this->attributes['pattern'] = '[a-zA-Z0-9]+';
                 break;
             case 'alpha_dash':
                 $this->attributes['pattern'] = '[a-zA-Z0-9_\\-]+';
                 break;
             case 'between':
                 list($min, $max) = $parameters;
                 $this->setMin($min);
                 $this->setMax($max);
                 break;
             case 'in':
                 $possible = sizeof($parameters) == 1 ? $parameters[0] : '(' . join('|', $parameters) . ')';
                 $this->attributes['pattern'] = '^' . $possible . '$';
                 break;
             case 'not_in':
                 $this->attributes['pattern'] = '(?:(?!^' . join('$|^', $parameters) . '$).)*';
                 break;
             case 'match':
                 $this->attributes['pattern'] = substr($parameters[0], 1, -1);
                 break;
             default:
                 continue;
                 break;
         }
     }
 }