You are currently browsing Crystal’s documentation for the 0.4 version - Switch to version: 0.3

Validation rules

alpha

Checks if the data for the field contains anything other than alphabetical characters.

 $rules = array(
    'login' => array('alpha, message: Usernames must only contain letters.')
);

alpha_numeric

Checks if the data for the field contains anything other than alpha-numeric characters.

 $rules = array(
    'login' => array('alpha_numeric, message : Usernames must only contain letters and numbers.')
);

between

Checks if the length of the data for the field is within the specified numeric range. Both minimum and maximum values must be supplied. Uses <= not < .

 $rules = array(
    'password' => array('between:(6 8), message: Passwords must be between 6 and 15 characters long.')
);

comparsion

Comparison is used to compare numeric values. It supports >=, <=, >, < and =.

 $rules = array(
    'age' => array('comparsion:(>= 18), message: Must be at least 18 years old.' )
);

valid_email

Checks if the data for the field contains anything other than valid email address

 $rules = array(
    'email' => array('valid_email, message: Please provide valid email address.')
);

Crystal uses the RFC-compliant email address validator by Dominic Sayers

extension

This rule checks for valid file extensions like .jpg or .png. Allow multiple extensions by passing them in array.

$avatar =  'file.ai';

$rules = array(
    'avatar' => array('extension:(jpg png gif psd ai), message: Please supply a valid image.')
);

valid_ip

Checks if the data for the field contains anything other than valid IPv4 address.

 $rules = array(
    'ip' => array('valid_ip, message: Please supply a valid IP address.')
);

matches

Checks if the data for the field the one in the parameter.

 $rules = array(
    'pet' => array('matches:(cat), message: Please supply a valid pet type.')
);

max_length

Checks if the data for the field is longer then the parameter value.

 $rules = array(
    'username' => array('max_length:(15), message: Usernames must be no larger than 15 characters long.'
     )
);

min_length

Checks if the data for the field is shorter then the parameter value.

 $rules = array(
    'username' => array('min_length: (3), message: Usernames must be at least 5 characters long.'
     )
);

numeric

Checks if the data for the field contains anything other than numeric characters.

 $rules = array(
    'participants' => array('numeric, message: Please supply a valid number.'
     )
);

valid_url

This rule checks for valid URL formats.

 $rules = array(
    'website' => array('valid_url , message: Please supply a valid url.'
     )
);

unique

Checks a database field for uniqueness

// Requirements 
// This validation method requires Crystal database instance
// $db  = Crystal::db();
// $validation = Crystal::validation($rules, $data, $db);

// Options
// table - specify the name of the table you want to look in
// field - specify the field name, if not specified Crystal will use the field name you are validating.
//  In the following example that will be 'email'
	
 $rules = array(
    'email' => array('unique , table: users, field: email,   message : This email is already taken')
);

regexp

This method accepts standart regular expressions

// Validate username, consist of alpha-numeric (a-z, A-Z, 0-9), underscores, and has minimum 5 character and maximum 20 character
$username= 'user_name12';

 $rules = array(
    'email' => array('regexp: (/^[a-z\d_]{5,20}$/i),   message : This email is already taken')
);