|
Phpr_ValidationRules class
Represents a set of validation rules.
Objects of this class are usually created by Phpr_Validation::add() and Db_ColumnDefinition::validation() methods.
Almost all methods of this class return the updated object. It allows to define rules as a chain:
$this->define_column('name', 'Name')->order('asc')->validation()->fn('trim')->required("Please specify the theme name."); Public methods
Method details¶ alpha() methodpublic Phpr_ValidationRules alpha(string $custom_message=NULL)
Checks whether the value contains only Latin characters.
¶ alphanum() methodpublic Phpr_ValidationRules alphanum(string $custom_message=NULL)
Checks whether the value contains only Latin characters and digits.
¶ email() methodpublic Phpr_ValidationRules email(boolean $allow_empty=false, string $custom_message=NULL)
Checks whether the value is a valid email address.
¶ float() methodpublic Phpr_ValidationRules float(string $custom_message=NULL)
Checks whether the value is a valid floating point number.
Correct numeric values: 10, 10.0, -10.0.
¶ fn() methodpublic Phpr_ValidationRules fn(string $name)
Adds a rule that processes a value using a PHP function.
The function must accept a single parameter - the value
and return a string or boolean value. The updated value
is used by all following validation rules. Example:
$this->define_column('author_name', 'Author')->validation()->fn('trim'); ¶ ip() methodpublic Phpr_ValidationRules ip(string $custom_message=NULL)
Checks whether the value is a valid IP address.
¶ length() methodpublic Phpr_ValidationRules length(int $length, string $custom_message=NULL)
Checks whether a value length matches the specified value.
¶ maxLength() methodpublic Phpr_ValidationRules maxLength(int $length, string $custom_message=NULL)
Checks whether a value is not longer than the specified length.
¶ method() methodpublic Phpr_ValidationRules method(string $name)
Adds a rule that validates a value with an owner class' method.
Use this method with ActiveRecord models. The model class should
contain a public method with the specified name. The should accept two parameters -
the field name and value, and return a string or boolean value. Alternatively you can use
setError() method of the validation object to throw an exception.
public function define_columns($context = null) { $this->define_column('is_enabled', 'Enabled')->validation()->method('validate_enabled'); ... } public function validate_enabled($name, $value) { if (!$value && $this->is_default) $this->validation->setError('This theme is default and cannot be disabled.', $name, true); return $value; } ¶ minLength() methodpublic Phpr_ValidationRules minLength(int $length, string $custom_message=NULL)
Checks whether a value is not shorter than the specified length.
¶ numeric() methodpublic Phpr_ValidationRules numeric(string $custom_message=NULL)
Checks whether the value is a valid number.
Correct numeric values: 10, -10.
¶ optional() methodpublic Phpr_ValidationRules optional()
Makes the field optional.
¶ regexp() methodpublic Phpr_ValidationRules regexp(string $pattern, string $custom_message=NULL, boolean $allow_empty=false)
Checks whether the value matches the specified regular expression.
¶ required() methodpublic Phpr_ValidationRules required(string $custom_message=NULL)
Makes the field required.
¶ unique() methodpublic Phpr_ValidationRules unique(string $custom_message=NULL, callback $checker_filter_callback=NULL)
Checks whether a value is unique.
This rule is applicable only when validation is used with a model.
The rule creates a test object (an instance of the model class) to detect whether the value
is unique.
By default, if the second parameter omitted, the rule checks whether the value is unique in the entire table. The second parameter allows to define a callback method in the model for configuring the test model object. The method should accept 3 parameters - the test object, the model object and the deferred session key value. Example: public function define_columns($context = null) { $this->define_column('file_name', 'File Name')->validation() ->unique('File name "%s" already used by another template.', array($this, 'configure_unique_validator')); ... } public function configure_unique_validator($checker, $page, $deferred_session_key) { // Exclude pages from other themes $checker->where('theme_id=?', $page->theme_id); } ¶ url() methodpublic Phpr_ValidationRules url(string $custom_message=NULL)
Checks whether the value is a valid URL.
|