ActiveRecord\Validations::validates_format_of PHP Method

validates_format_of() public method

class Person extends ActiveRecord\Model { static $validates_format_of = array( array('email', 'with' => '/^.*?@.*$/') ); } Available options:
  • with: a regular expression
  • message: custom error message
  • allow_blank: allow blank strings
  • allow_null: allow null strings
public validates_format_of ( array $attrs )
$attrs array Validation definition
    public function validates_format_of($attrs)
    {
        $configuration = array_merge(self::$DEFAULT_VALIDATION_OPTIONS, array('message' => Errors::$DEFAULT_ERROR_MESSAGES['invalid'], 'on' => 'save', 'with' => null));
        foreach ($attrs as $attr) {
            $options = array_merge($configuration, $attr);
            $attribute = $options[0];
            $var = $this->model->{$attribute};
            if (is_null($options['with']) || !is_string($options['with'])) {
                throw new ValidationsArgumentError('A regular expression must be supplied as the [with] option of the configuration array.');
            } else {
                $expression = $options['with'];
            }
            if ($this->is_null_with_option($var, $options) || $this->is_blank_with_option($var, $options)) {
                continue;
            }
            if (!@preg_match($expression, $var)) {
                $this->record->add($attribute, $options['message']);
            }
        }
    }