Barebone\Validators

There are a lot of validators in the Barebone Framework. If there is something missing you can extend your own validator from the base validator class.

Usage example

$data = array('field1' => '1234');

$v = new Barebone\Validator($data, array(
    new Barebone\Validators\Required('field1', 'field1 is required'),
    new Barebone\Validators\Integer('field1', 'field1 must be an integer'),
));

if (! $v->execute()) {
    print_r($v->getErrors()); // Get all validation errors
}

Source activerecord validator

function validatorAction(array $args = []){
    // create client object
    $client = new \customer();
    // get some client from db
    $client->get_by_id(1);
    // if the form is send
    if($this->request->is_post()){
        // set the form data to the client object
        $client->set_data($_POST);
        // create validator rules array
        $validator_rules = array(
    	    new Barebone\Validators\Required('initialen', 'Initialen is required!'),
    	    new Barebone\Validators\MinLength('initialen', 'Minlength initialen is 9', 9),
    	    new Barebone\Validators\Required('achternaam', 'Achternaam is required!'),
    	    new Barebone\Validators\MinLength('achternaam', 'Minlength achternaam is 8', 8),
    	);
    	// validate the client data against your rules
    	$validator = $client->is_valid($validator_rules);
    	// if the form is valid it will show succes message
    	if($validator->isValid()){
    	    $this->view->flash('Successfully saved!', 'success');
    	// if it does not validat
    	}else{
    	    // use the flash function
    	    $this->view->flash($validator->flash_errors(), 'error');
    	    // or use the validator to render errors in the view
    	    $this->view->validator = $validator;
    	}
    }
    // assign the client to the view
    $this->view->client = $client;
    
}

Barebone\Validators

Alpha

Allow only letters.

new Barebone\Validators\Alpha($field, $error message);

AlphaNumeric

Allow only letters and digits.

new Barebone\Validators\AlphaNumeric($field, $error message);

Date

Validate datetime with the given formats.

new Barebone\Validators\Date($field, $error_message, array $formats);

Email

Addresses like user+hash@domain.tld are supported.

new Barebone\Validators\Email($field, $error_message);

GreaterThan

new Barebone\Validators\Integer($field, $error_message, $min);

Integer

Allow only integer, but also strings with digits only.

new Barebone\Validators\Integer($field, $error_message);

IP address

Allow only IPv4 or IPv6 addresses.

new Barebone\Validators\Ip($field, $error_message);

Length

Allow only strings with a correct length.

new Barebone\Validators\Length($field, $error_message, $min, $max);

MinLength

new Barebone\Validators\MinLength($field, $error_message, $min);

MaxLength

new Barebone\Validators\MaxLength($field, $error_message, $max);

Numeric

Allow float, integer and strings with only digits and dot.

new Barebone\Validators\Numeric($field, $error_message);

Range

Allow only numbers inside the specified range.

new Barebone\Validators\Range($field, $error_message, $min, $max);

Required

The specified field must exists.

new Barebone\Validators\Required($field, $error_message);

Equals

The specified fields must be equal to another field.

new Barebone\Validators\Equals($field, $field2, $error_message);

NotEquals

The specified fields must not be equal to another field.

new Barebone\Validators\NotEquals($field, $field2, $error_message);

NotEmpty

The specified fields cannot be empty.

new Barebone\Validators\NotEmpty($field, array $array, $error_message);

InArray

The specified field must be in the array.

new Barebone\Validators\InArray($field, array $array, $error_message);

NotInArray

The specified field cannot be be in the array.

new Barebone\Validators\NotInArray($field, array $array, $error_message);

Exists

Check inside a database if the column value exists or not.

new Barebone\Validators\Exists($field, $error_message, PDO $pdo, $table, $key = '');

$pdo must be an PDO instance, $table is the table name.

By default the column used in the table is the value of $field, but it can be overrided if the field doesn't have the same name in the database.

Unique

Check inside a database if the column value is unique or not.

new Barebone\Validators\Unique($field, $error_message, PDO $pdo, $table, $primary_key = 'id');

$pdo must be an PDO instance, $table is the table name and by default the primary key is "id".

$field can either be a string or an array of string to check inside a database if a value from several columns is unique or not.

If the primary key value is not null, we don't check the uniqueness of the column for this row. It's useful if you perform validation for an update.