Overview

Akelos has a number of methods and different approaches to facilitate Validations of your ActiveRecord model.

Overview

Why validate?

Validation is a useful tool to ensure the integrity of information in the database. It forms a minimum standard that all records must meet before they can be saved.

A valid user example:

  • has a name and password
  • has a unique name
  • has a password that is between 5 and 15 characters long

With those rules (except written in real PHP) as part of your model, Akelos will not save any user that doesn't meet the requirements.

When using validation, Akelos keeps track of any validation errors and will build error messages for those errors. It is then useful to display these messages back to the user so the errors may be corrected.

When does validation take place?

Validation takes place before a model is saved. If the model being saved doesn't meet its validation requirements, it will NOT be saved to the database. Attempts to save an invalid model return false. This allows for the “if-save” convention:

if($User->save()){
  // user was valid and saved
}else{
  // user was invalid, not saved
}

You can trigger validation manually by calling the models validate method.

$User->validate();

But likely more useful would be to call isValid(), which runs @validate@ then returns true or false.

if ($User->isValid()){
}

Avoiding validation

You can skip validation by passing false to save().

$User->save(false);

The method updateAttribute() does not trigger validation, and as such, methods that call updateAttributes() (toggleAttribute(), incrementAttribute() and decrementAttribute()) do not trigger validation either.

Skipping validation may result (obviously) in invalid data being recorded to the database.

What happens when a model is validated?

When a model is validated, any validation errors will be collected in the models _errors array.

More information

 
understanding-validations.txt · Last modified: 2008/06/13 10:23 by bermi