If you don’t know what this class does just based on its name, then Brain might not be for you. Therefore, without any further introduction, here is how Brain’s Validate class works:
Get an Instance of Validate
// Assuming container access
$v = $this->container['validate'];
Setting or Importing Validation Rules
// Example of setting a rule manually
$v->set_rules(array(
array(
'field' => 'name',
'label' => 'NAME',
'rules' => 'trim|required|xss_clean|max_length[30]'
)
));
/**
* Example of setting rules from a config file.
* In this case we are getting the rules from a
* file located at config/form_validation/contact_form.php.
* This is just another config file, and without a second
* parameter, the element holding the rules is assumed to
* be named "form_validation_rules".
*/
$v->import_rules('contact_form');
The Actual Rules
The actual rules, such as “required”, “xss_clean”, and “max_length” in the code example shown above, are just like CodeIgniter’s rules. Very little was changing frameworks would be as simple as possible.
Regular Callbacks
Callbacks can go right in your controller, model, or library as methods. Set them as you would any other rule, but prefixed by “callback_”. Make sure you use the set_callback_object() method so the validation class can run your callbacks.
$v->set_callback_object( $this );
// Check the field using the check_awesomeness method in THIS class
$v->set_rules(array(
array(
'field' => 'awesomeness',
'label' => 'AWESOMENESS',
'rules' => 'callback_check_awesomeness'
)
));
CodeIgniter does allow callbacks, but never had the functionality to call external files, so I developed a validation rule called “external_callbacks”.
External Callbacks
// In the rules that are imported
$cfg['form_validation_rules'] = array(
array(
'field' => 'name',
'label' => 'NAME',
'rules' => 'external_callbacks[name_callbacks,_check_name,TRUE]'
)
);
// In a class named Name_callbacks located at config/form_validation/name_callbacks.php
use \Brain\Lib\ContainerAccess;
class Name_callbacks extends ContainerAccess {
private $validate = NULL;
public static $container;
public function __construct( $instance )
{
$this->validate =& $instance;
self::$container = parent::$container;
}
public function _check_name( $name, $args )
{
if( $name == 'skunkbad' && $args[0] === 'TRUE' )
{
return $name;
}
else
{
$this->validate->set_message(
'external_callbacks',
'%s must be skunkbad, and $args[0] must be TRUE'
);
}
}
}
You can see in the code above that we passed in a single argument “TRUE” to the callback. If we want to pass in more values, we simply add them inside the brackets of the call to the external callback. For instance, if we wanted to add a second argument of “skunk”, and a third argument of “bad”, it would look like this:
// In the rules that are imported
$cfg['form_validation_rules'] = array(
array(
'field' => 'name',
'label' => 'NAME',
'rules' => 'external_callbacks[name_callbacks,_check_name,TRUE,skunk,bad]'
)
);
These arguments would be available inside our external callback as $args[1] and $args[2].
Running the Validation
if( $v->run() )
{
// Validation passed
$name = $v->set_value('name');
/**
* The validate class sets a view variable named $validation_passed to TRUE.
*/
}
else
{
// Validation failed
echo '
Errors:
' . $v->error_string() . '
';
/**
* The validate class sets a view variable named $validation_errors
* to the value of $v->error_string().
*/
}
Special Methods
CodeIgniter lacked some methods that I found myself needing, so I built them into Brain’s Validate class.
set_callback_object()
If external callbacks are too much for you, you might add a method or two to your controller, model, library, etc. The validate class needs to know where the callbacks are though, so pass $this to it!
$v->set_callback_object( $this );
get_error_array()
Access the protected $_error_array through this method.
unset_error( $field )
If there are errors and you need to unset one for any reason, pass the field name to this method.
get_field_data()
Access the protected $_field_data through this method.
unset_field_data( $element )
This method removes a field from the $_field_data. This is especially helpful if you are using set_value() but you don’t want to repopulate a form with a bad value. Pass an asterisk “*” as the param to unset the entire $_field_data array.
set_value()
The BaseController class autoloads a super tiny validation helper, and it’s only content is a set_value() function. This function is just like its CodeIgniter version. Very simple, yet used so often.