Here at Affinitive we have been starting to use the php framework named “Lithium” or “Li3”. One of the nice little features of this framework is that it comes built in with some very handy validators for validating your data before sending it to the database. But, as with all default measures that are taken to ensure your data is complete, sometimes you need to extend upon it, you do this with li3 validators by writing your own.

The process of writing a validator is very simple, you define it in the __init function of the specific Model. A sample validator is shown below

class ValidateExample extends \lithium\data\Model {
    public $validates = array();
    public static function __init() {
        $self = static::_object();
        static::config();
        // your __init code here 
        Validator::add('sampleValidator', function(&$value, $format = null, array $options = array()) {
            // your code here
            return $is_valid;
        });
    }
}

Lets look at the three values that are given to the Validator::add function so that it can work and validate effectively [see http://lithify.me/docs/lithium/util/Validator::add() for complete official api documentation].

  • &$value – a reference to the given value
  • $format – a string that describes the format of the input. (see `$options[‘format’])
  • $options – a range of options that are based on the data that may or not be saved
    • $options['values'] – an array of values that will be saved if all the validators return true
    • $options['messages'] – the message that will be used if your validator failed
    • $options['format'] – This is the source for your raw format, the $format variable is set to 0. At the time of publication, there are two bug reports dealing with this, see bug reports here: here and here

The $options array allows you to check other values very easily. For instance, lets say you have a normal web signup form where you ask the user to put in their email address twice to make sure that they typed it in correctly. You can use the following validator (with a custom option) to specify that you want to compare the two form values.

Validator::add('match', function(&$value, $format = null, array $options = array()){
    if (isset($options['against']) && in_array($options['against'], array_keys($options['values']))) {
        return $options['values'][$options['against']] == $value;
    } else if (!isset($options['against'])) {
        throw new InvalidArgumentException("You did not specify a value to match against using the keyword 'against' ");
    } else {
        throw new InvalidArgumentException("The name {$options['against']} is not valid");
    }
});

You can use this to make sure that two entries on your form are the same by entering the following line into your list of validations for a field.

array(
    'match',
    'message' => "field1 must match this field",
    'against' => "field1"
);

With this array, we call the “match” validator and give it two options, a message string and an against string. These both get passed to the validator, where it uses the against string to find the value of the intended variable and compare it to the given value.

We must also check to make sure that we are not being given faulty data (ex. wrong column names, etc.), so we sprinkle a few if statements and exception throwing to make sure the developer knows there is something wrong.



Affinitive Dev Blog

Ramblings from the dev team at Affinitive, a word of mouth and social media marketing and technology solutions pioneer and Facebook Preferred Marketing Developer (PMD). Also see @Affinitive, @RMarscher, and @BobTroia.