Abstract Class Misago\ActiveRecord\Validations

Validation methods.

Example:

class User extends Misago\ActiveRecord\Base
{
  static function __constructStatic()
  {
    static::validates_uniqueness_of('user_name');
    static::validates_length_of('user_name', array('minimum' => 3, 'maximum' => 20));
  }
} 

Calls

You may ask for a specific validation using is_valid() or when saving with save_with_validation(), which is the default for Misago\ActiveRecord\Base::save().

Error object

All validation errors are accessible throught the errors attribute, which is an instance of Misago\ActiveRecord\Errors. You may also manually add errors throught that same object.

Associations

Dependent associated records are also validated before saving. If any dependent record's validation fails, this record's validation will fail.

You must specify the associations to be validated:

class Post extends Misago\ActiveRecord\Base
{
  static function __constructStatic()
  {
    static::has_many('tags');
    static::validates_associated('tags');
  }
} 

Callbacks

See Misago\ActiveRecord\Base for help on the different callbacks.

TEST: Test validates_associated. IMPROVE: Add possibility to validate a date/time. IMPROVE: On update only validate changed attributes.

Inheritence

Extends:
Associations

Methods

Protected static methods

validates_associated($assoc_name)

Defines a list of associations to be validated when validating this object.

validates_exclusion_of($attribute, $options=array())

Validates if an attribute isn't within a list of values.

Options:

  • allow_null - allows the attribute to be null (defaults to nullity in DB).
  • allow_blank - allows the attribute to be blank (defaults to false).
  • message - error message.
  • in - an enumerable list of values.

validates_format_of($attribute, $options=array())

Validates the format of an attribute, using a regular expression.

Options:

  • allow_null - allows the attribute to be null (defaults to nullity in DB).
  • allow_blank - allows the attribute to be blank (defaults to false).
  • message - error message.
  • with - the regular expression to use.

validates_inclusion_of($attribute, $options=array())

Validates if an attribute is within a list of values.

Options:

  • allow_null - allows the attribute to be null (defaults to nullity in DB).
  • allow_blank - allows the attribute to be blank (defaults to false).
  • message - error message.
  • in - an enumerable list of values.

validates_length_of($attribute, $options=array())

Validates the length of an attribute.

Options:

  • allow_null - allows the attribute to be null (defaults to nullity in DB).
  • allow_blank - allows the attribute to be blank (defaults to false).
  • minimum - int
  • maximum - int
  • within - 'min..max'
  • is - int
  • message - generic error message.
  • too_short - error message when length < minimum.
  • too_long - error message when length > maximum.
  • wrong_length - error message when length isn't exactly the is size.

validates_presence_of($attribute, $options=array())

Validates the presence of an attribute. The attribute must be present and it cannot be blank.

Options:

  • message: the error message

validates_uniqueness_of($attribute, $options=array())

Validates if a column is unique.

On create checks if the column isn't already present in the database. On update it does the same, but excluding the record itself.

Options:

  • allow_null - allows the attribute to be null (defaults to nullity in DB).
  • allow_blank - allows the attribute to be blank (defaults to false).
  • message - error message.

Public instance methods

errors()

is_valid()

Runs validation tests. Returns true if tests were successfull, false otherwise.

save_with_validation($perform_validation=true)

save_without_validation()

Protected instance methods

after_validation()

after_validation_on_create()

after_validation_on_update()

before_validation()

before_validation_on_create()

before_validation_on_update()

validate()

Manual validations.

validate_exclusion_of($attribute, $options=array())

validate_format_of($attribute, $options=array())

validate_inclusion_of($attribute, $options=array())

validate_length_of($attribute, $options=array())

validate_on_create()

Manual validations on creation only.

validate_on_update()

Manual validations on update only.

validate_presence_of($attribute, $options=array())

validate_uniqueness_of($attribute, $options=array())