class Validations (View source)

Manages validations for a {@link Model}.

This class isn't meant to be directly used. Instead you define validators thru static variables in your {@link Model}. Example:

class Person extends ActiveRecord\Model { static $validates_length_of = array( array('name', 'within' => array(30,100), array('state', 'is' => 2) ); }

$person = new Person(); $person->name = 'Tito'; $person->state = 'this is not two characters';

if (!$person->is_valid()) print_r($person->errors);

Methods

__construct(Model $model)

Constructs a {@link Validations} object.

get_record()

No description

array
rules()

Returns validator data.

validate()

Runs the validators.

validates_presence_of(array $attrs)

Validates a field is not null and not blank.

validates_inclusion_of(array $attrs)

Validates that a value is included the specified array.

validates_exclusion_of(array $attrs)

This is the opposite of {@link validates_include_of}.

validates_inclusion_or_exclusion_of(string $type, $attrs)

Validates that a value is in or out of a specified list of values.

validates_numericality_of(array $attrs)

Validates that a value is numeric.

validates_size_of(array $attrs)

Alias of {@link validates_length_of}

validates_format_of(array $attrs)

Validates that a value is matches a regex.

validates_length_of(array $attrs)

Validates the length of a value.

validates_uniqueness_of(array $attrs)

Validates the uniqueness of a value.

Details

Validations __construct(Model $model)

Constructs a {@link Validations} object.

Parameters

Model $model The model to validate

Return Value

Validations

get_record()

array rules()

Returns validator data.

Return Value

array

Errors validate()

Runs the validators.

Return Value

Errors the validation errors if any

validates_presence_of(array $attrs)

Validates a field is not null and not blank.

class Person extends ActiveRecord\Model { static $validates_presence_of = array( array('first_name'), array('last_name') ); }

Available options:

  • message: custom error message
  • allow_blank: allow blank strings
  • allow_null: allow null strings

Parameters

array $attrs Validation definition

validates_inclusion_of(array $attrs)

Validates that a value is included the specified array.

class Car extends ActiveRecord\Model { static $validates_inclusion_of = array( array('fuel_type', 'in' => array('hyrdogen', 'petroleum', 'electric')), ); }

Available options:

  • in/within: attribute should/shouldn't be a value within an array
  • message: custome error message
  • allow_blank: allow blank strings
  • allow_null: allow null strings

Parameters

array $attrs Validation definition

validates_exclusion_of(array $attrs)

This is the opposite of {@link validates_include_of}.

Available options:

  • in/within: attribute should/shouldn't be a value within an array
  • message: custome error message
  • allow_blank: allow blank strings
  • allow_null: allow null strings

Parameters

array $attrs Validation definition

See also

validates_inclusion_of

validates_inclusion_or_exclusion_of(string $type, $attrs)

Validates that a value is in or out of a specified list of values.

Available options:

  • in/within: attribute should/shouldn't be a value within an array
  • message: custome error message
  • allow_blank: allow blank strings
  • allow_null: allow null strings

Parameters

string $type Either inclusion or exclusion
$attrs Validation definition

See also

validates_inclusion_of
validates_exclusion_of

validates_numericality_of(array $attrs)

Validates that a value is numeric.

class Person extends ActiveRecord\Model { static $validates_numericality_of = array( array('salary', 'greater_than' => 19.99, 'less_than' => 99.99) ); }

Available options:

  • only_integer: value must be an integer (e.g. not a float)
  • even: must be even
  • odd: must be odd"
  • greater_than: must be greater than specified number
  • greater_than_or_equal_to: must be greater than or equal to specified number
  • equal_to: ...
  • less_than: ...
  • less_than_or_equal_to: ...
  • allow_blank: allow blank strings
  • allow_null: allow null strings

Parameters

array $attrs Validation definition

validates_size_of(array $attrs)

Alias of {@link validates_length_of}

Parameters

array $attrs Validation definition

validates_format_of(array $attrs)

Validates that a value is matches a regex.

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

Parameters

array $attrs Validation definition

validates_length_of(array $attrs)

Validates the length of a value.

class Person extends ActiveRecord\Model { static $validates_length_of = array( array('name', 'within' => array(1,50)) ); }

Available options:

  • is: attribute should be exactly n characters long
  • in/within: attribute should be within an range array(min,max)
  • maximum/minimum: attribute should not be above/below respectively
  • message: custome error message
  • allow_blank: allow blank strings
  • allow_null: allow null strings. (Even if this is set to false, a null string is always shorter than a maximum value.)

Parameters

array $attrs Validation definition

validates_uniqueness_of(array $attrs)

Validates the uniqueness of a value.

class Person extends ActiveRecord\Model { static $validates_uniqueness_of = array( array('name'), array(array('blah','bleh'), 'message' => 'blech') ); }

Available options:

  • with: a regular expression
  • message: custom error message
  • allow_blank: allow blank strings
  • allow_null: allow null strings

Parameters

array $attrs Validation definition