Class Details

Class that holds Validations errors.


Class Variables

public static mixed $DEFAULT_ERROR_MESSAGES = array( 'inclusion' => "is not included in the list", 'exclusion' => "is reserved", 'invalid' => "is invalid", 'confirmation' => "doesn't match confirmation", 'accepted' => "must be accepted", 'empty' => "can't be empty", 'blank' => "can't be blank", 'too_long' => "is too long (maximum is %d characters)", 'too_short' => "is too short (minimum is %d characters)", 'wrong_length' => "is the wrong length (should be %d characters)", 'taken' => "has already been taken", 'not_a_number' => "is not a number", 'greater_than' => "must be greater than %d", 'equal_to' => "must be equal to %d", 'less_than' => "must be less than %d", 'odd' => "must be odd", 'even' => "must be even", 'unique' => "must be unique", 'less_than_or_equal_to' => "must be less than or equal to %d", 'greater_than_or_equal_to' => "must be greater than or equal to %d" )

Class Methods

public Errors __construct ( Model $model )

Constructs an Errors object.

  • Model $model - The model the error is for
public void add ( string $attribute , string $msg )

Add an error message.

  • string $attribute - Name of an attribute on the model
  • string $msg - The error message
public void add_on_blank ( string $attribute , string $msg )

Adds the error message only if the attribute value was null or an empty string.

  • string $attribute - Name of an attribute on the model
  • string $msg - The error message
public void add_on_empty ( string $attribute , string $msg )

Adds an error message only if the attribute value is empty.

  • string $attribute - Name of an attribute on the model
  • string $msg - The error message
public void clear ( )

Clears out all error messages.

public void clear_model ( )

Nulls $model so we don't get pesky circular references. $model is only needed during the validation process and so can be safely cleared once that is done.

public array full_messages ( )

Returns all the error messages as an array.

 $model->errors->full_messages();

 # array(
 #  "Name can't be blank",
 #  "State is the wrong length (should be 2 chars)"
 # )

        

public ArrayIterator getIterator ( )

Returns an iterator to the error messages.

This will allow you to iterate over the Errors object using foreach.

 foreach ($model->errors as $msg)
   echo "$msg\n";

        

public void get_raw_errors ( )

Returns the internal errors object.

 $model->errors->get_raw_errors();

 # array(
 #  "name" => array("can't be blank"),
 #  "state" => array("is the wrong length (should be 2 chars)",
 # )

        

public boolean is_empty ( )

Returns true if there are no error messages.

public boolean is_invalid ( string $attribute )

Returns true if the specified attribute had any error messages.

  • string $attribute - Name of an attribute on the model
public string/array on ( string $attribute )

Returns the error message(s) for the specified attribute or null if none.

  • string $attribute - Name of an attribute on the model
  • return: Array of strings if several error occured on this attribute.
public int size ( )

Returns the number of error messages there are.

public array to_array ( [ array $closure = null] )

Returns all the error messages as an array, including error key.

  • array $closure - Closure to fetch the errors in some other format (optional) This closure has the signature function($attribute, $message) and is called for each available error message.

 $model->errors->errors();

 # array(
 #  "name" => array("Name can't be blank"),
 #  "state" => array("State is the wrong length (should be 2 chars)")
 # )

        

public array __get ( string $attribute )

Retrieve error messages for an attribute.

  • string $attribute - Name of an attribute on the model
  • return: or null if there is no error.
public string __toString ( )

Convert all error messages to a String.

This function is called implicitely if the object is casted to a string:

 echo $error;

 # "Name can't be blank\nState is the wrong length (should be 2 chars)"