PHP-Wax Model Validation

Validating Data

Before a model is saved to the database a method called validations() will automatically be run. This can generally be used as a hook to add validation information to the model class. There are a number of built in validations, but since the method is overloaded you can perform any validations that you want.

You can also add your own error messages with the add_error($field, $message) method. The built in validations are as follows:

 valid_format($field, $format, $message="is an invalid format",
      $optional=true)
 valid_required($field, $message="is a required field")
 valid_length($field, $min, $max=0, 
      $message_short="must be at least", 
      $message_long="must be less than")
 valid_unique($field, $message="is already taken")

When using the valid_format method, the second value is a regular expression. There are however some built in regular expressions which will take care of most standard format validations. Instead of supplying a regular expression, if you pass in any of the following:

 valid_format('email', 'email')
 valid_format('postcode', 'uk_postcode')
 valid_format('zipcode', 'usa_zipcode')
 valid_format('date', 'usa_date')
 valid_format('date', 'uk_date')
 valid_format('number', 'number')

More values will also be added to these fixed validations as required.

How to specify this in your model file

Below is an example of a model file with some validations specified.

 <?php
 class Article extends WXActiveRecord
 {
   public function validations() {
     $this->valid_required('name');
     $this->valid_required('email');
     $this->valid_format('email', 'email');
     $this->valid_unique('email');
   }
 }
 ?>

Of course more advanced validations can be done by writing your own methods.

More from this section

RECENT PERSONAL WORK

A.G. Cox Plumbing
alltradeart Case Study
Kwik Travel Case Study
Allotment Case Study

RECENT ARTICLES

Rails and Apache

In what appears to be a very useful find ' read more

Useful bit of Javascript

Never thought about using javascript for this before but I had a requ... read more

Quick jquery Gallery

I had a look at some of the gallery plugins available for jquery and ... read more

Massive CPU usage when updating Fedora using YUM/RPMQ

We noticed that YUM and RPMQ processes were using a massive amount CP... read more

CREDITS

powered by php-wax the php framework