Skip to content

Latest commit

Β 

History

History
1449 lines (1219 loc) Β· 38.3 KB

API.md

File metadata and controls

1449 lines (1219 loc) Β· 38.3 KB

Bouncer.php API

Bouncer is a library that allows you to flexible, quickly and easy to use filter and validate HTML forms.

← Back to readme

Table of contents

Constructor

new Bouncer( string|array|object $data = '', array $options = array() )

Parameters

  • $data (string|array|object) (Optional) Accepts array, object or query string. The array key is treated as the value pair field and its value. Thus, it is capable of accepting requests such as $_POST, $_GET.
  • $options (array) (Optional) Accepts arrays to overwrite default variables.

Return

  • Bouncer Bouncer instance.

Examples

Working with $_POST, $_GET requests
$variables = array(
  'messages' => array(
    'required' => 'Please fill in the blank fields'
  )
);
$bouncer = new Bouncer($_POST, $variables);
Working with custom data
$myData = array(
  'name' => 'John',
  'surname' => 'Doe',
  'email' => 'johndoe@example.tld'
);
$bouncer = new Bouncer($myData);
Working with query string
$bouncer = new Bouncer('name=John&surname=Doe&email=johndoe@example.tld');

↑ Return to top

Options

Bouncer comes with rule labels and error messages by default. You can change these variables in the settings while taking a instance.

Tip: Use this feature if you just want to change the defaults. At the Bouncer, each rule of each field allows you to add custom error messages.

Examples

Change the "required" error message
$variables = array(
  'messages' => array(
    'required' => 'Please fill in the blank fields'
  )
);
$bouncer = new Bouncer($_POST, $variables);

A complete list of variables

$defaults = array(
  'messages' => array(
    'default' => '%label% contains %rule% rule error.',
    'empty' => '%label% must be left blank.',
    'required' => '%label% must be filled in.',
    'length' => '%label% has to be %argument1% characters long.',
    'minlength' => '%label% must be at least %argument1% characters or longer.',
    'maxlength' => '%label% must be no longer than %argument1% characters.',
    'betweenlength' => '%label% has to be between %argument1% and %argument2% characters long.',
    'min' => '%label% must be greater than or equal to %argument1%.',
    'max' => '%label% must be less than or equal to %argument1%.',
    'between' => '%label% must be a number between %argument1% and %argument2%',
    'email' => '%label% must be valid email address.',
    'boolean' => '%label% must be valid boolean value.',
    'float' => '%label% must be valid floating point numbers.',
    'integer' => '%label% must be valid integer number.',
    'digits' => '%label% must be valid integer number.',
    'even' => '%label% must be an even number.',
    'odd' => '%label% must be an odd number.',
    'matches' => '%label% must be the same as "%argument1%".',
    'notmatches' => '%label% must be different from "%argument1%".',
    'startswith' => '%label% must start with "%argument1%".',
    'notstartswith' => '%label% must not start with "%argument1%".',
    'endswith' => '%label% must be end with "%argument1%".',
    'notendswith' => '%label% must be not end with "%argument1%".',
    'oneof' => '%label% must be one of the allowed "%argument1%".',
    'notoneof' => '%label% must be not one of the disallowed "%argument1%".',
    'ip' => '%label% must be valid IP address.',
    'url' => '%label% must be valid internet address "URL".',
    'ccnum' => '%label% must be valid credit card number.',
    'callback' => '%label% must conform to the required format.',
    'tcnum' => '%label% must be valid Turkish Republic identification number.',
  ),
  'rules' => array(
    'labels' => array(
      'default' => '(Unknown)',
      'empty' => '(Must empty value)',
      'required' => '(Must required)',
      'length' => '(Invalid length)',
      'minlength' => '(Invalid Minimum length)',
      'maxlength' => '(Invalid Maximum length)',
      'betweenlength' => '(Invalid length range)',
      'min' => '(Invalid minimum number)',
      'max' => '(Invalid maximum number)',
      'between' => '(Invalid number range)',
      'email' => '(Invalid email address)',
      'boolean' => '(Must boolean value)',
      'float' => '(Must float number)',
      'integer' => '(Must integer number)',
      'digits' => '(Must integer number)',
      'even' => '(Must even number)',
      'odd' => '(Must odd number)',
      'matches' => '(Must matches with ...)',
      'notmatches' => '(Must not match with ...)',
      'startswith' => '(Must starts with ...)',
      'notstartswith' => '(Must not starts with ...)',
      'endswith' => '(Must ends with ...)',
      'notendswith' => '(Must not ends with ...)',
      'oneof' => '(Must one of allowed)',
      'notoneof' => '(It shouldn\'t be one of disallowed)',
      'ip' => '(Invalid IP address)',
      'url' => '(Invalid internet address)',
      'ccnum' => '(Invalid credit card number)',
      'tcnum' => '(Invalid Turkish Republic identification number)',
    )
  )
);

↑ Return to top

Methods

  • name() - Sets the current field.
  • label() - Defines the label of the field.
  • value() - Defines the value of the field.
  • validate() - Validates existing, preset or all fields.
  • get() - Returns the values of the specified properties.
  • errors() - Outputs error messages as HTML.
  • empty() - Field should not be filled.
  • required() - Field must be filled in.
  • length() - Field has to be X characters long.
  • minlength() - Field has to be greater than or equal to X characters long.
  • maxlength() - Field has to be less than or equal to X characters long.
  • betweenlength() - Field has to be between minlength and maxlength characters long.
  • min() - Field must be a number greater than [or equal to] X.
  • max() - Field must be a number less than [or equal to] X.
  • between() - Field must be a number between X and Y.
  • boolean() - Field must be a valid boolean value.
  • integer() - Field must be a valid integer value.
  • digits() - Field must be a valid digits.
  • float() - Field must be a valid float number.
  • numeric() - Field must be a valid numeric value.
  • even() - Field must be an even number.
  • odd() - Field must be an odd number.
  • matches() - Field must match a specific value.
  • notmatches() - Field must differ from a specific value.
  • startswith() - Field must start with a specific substring.
  • notstartswith() - Field must NOT start with a specific substring.
  • endswith() - Field must end with a specific substring.
  • notendswith() - Field must NOT end with a specific substring.
  • oneof() - Field has to be one of the allowed ones.
  • notoneof() - Field has to be not one of the disallowed ones.
  • email() - Field must be a valid email address.
  • ip() - Field must be valid IP address.
  • url() - Field must be valid internet address.
  • color() - Field must be valid hexadecimal color code.
  • ccnum() - Field must be a valid credit card number format.
  • tcnum() - Field must be a valid Turkish Republic identification number.
  • callback() - Add custom callable validation functionality.
  • filter() - Filter before validating a field's value.

↑ Return to top

name

name( string $name )

Sets the current field.

If the field is not predefined, it creates an empty one.

Parameters

  • $name (string) Field name.

Return

  • Bouncer Bouncer instance.

Examples

Set the field and determining its value
$bouncer = new Bouncer();
$bouncer
->name('field')
->value('Hello World!');
Set the field and change its value
$bouncer = new Bouncer(array(
  'field' => 'Yes'
));
$bouncer
->name('field')
->value('No');

↑ Return to top


label

label( string $label )

Defines the label of the field.

Defines a label to the field that can be used as a variable in error messages.

Parameters

  • $label (string) The label of the field.

Return

  • Bouncer Bouncer instance.

Examples

Define the label of the field
$bouncer = new Bouncer(array(
  'field' => 'Yes'
));
$bouncer
->name('field')
->label('Remember Me');

↑ Return to top


value

value( string $value )

Defines the value of the field.

If predefined, it overwrites the value. (see)

Parameters

  • $value (string) The value of the field.

Return

  • Bouncer Bouncer instance.

Examples

Change the value of the field
$bouncer = new Bouncer(array(
  'field' => 'Yes'
));
$bouncer
->name('field')
->value('No');

↑ Return to top


validate

validate( boolean|string $name = '' )

Validates existing, preset or all fields.

Parameters

  • $name (boolean|string) (Optional) Field name. If true, validates all fields. If empty, validates preset field. The default is ''.

Return

  • mixed All body data.

If it doesn't find any fields, it returns body data without validation.

Examples

Simple validation
$bouncer = new Bouncer($_POST);
$bouncer->validate(true);
Validate only one field
$bouncer = new Bouncer(array(
  'author' => 'John',
  'title' => 'Hello World!',
  'content' => 'The quick, brown fox jumps over a lazy dog.',
  'visibility' => ''
));
$bouncer->validate('visibility'); // Only validates the visibility field.

↑ Return to top


get

get( string $property = '', string $name = '' )

Returns the values of the specified properties.

Returns all data collected about the field or body, or the specified data.

Parameters

  • $property (string) The property name. (see)
  • $name (string) Field name. If empty, the whole body. The default is ''.

Return

  • mixed Preferred data.

Examples

Check the validity
$bouncer = new Bouncer(array(
  'title' => 'Hello World!',
  'content' => 'The quick, brown fox jumps over a lazy dog.'
));
$bouncer
->name('title')
->required()
->validate();

var_dump( $bouncer->get('valid', 'title') ); // returns 1
var_dump( $bouncer->get('valid') ); // Returns -1 because the body has fields that have not yet been validated

↑ Return to top


errors

errors( mixed $options = array(), string $name = '' )

Parameters

  • $options (mixed) Options.
Defaults
array(
  'before' => '',
  'after' => '',
  'item_before' => '',
  'item_after' => '',
  'echo' => ''
);
  • $name (string) Field name. If empty, the whole errors. The default is ''.

Return

  • mixed HTML output.

Examples

Print all errors
$bouncer = new Bouncer(array(
  'title' => 'Hello World!',
  'content' => 'The quick, brown fox jumps over a lazy dog.'
));
$bouncer
->name('title')
->required()
->minlength(100)
->name('content')
->required()
->minlength(900)
->validate();

$options = array(
  'before' => '<ol>',
  'after' => '</ol>',
  'error_before' => '<li>',
  'error_after' => '</li>'
);

$bouncer->errors($options, true);

↑ Return to top


empty

empty( boolean|string $message = true )

Field should not be filled.

Parameters

  • $message (boolean|string) (Optional) The error message that will appear. Nothing will appear if '' is entered. Accepts allowed variables (See). Bool true shows the default message. The default is true.

Return

  • Bouncer Bouncer instance.

Examples

Forcing field to be blank
$bouncer = new Bouncer(array(
  'field' => 'Field value'
));
$bouncer
->name('field')
->empty('Please do not fill in this field.')
->validate();

↑ Return to top


required

required( boolean|string $message = true )

Field must be filled in.

It can also be called notempty() to make it easier to use.

Parameters

  • $message (boolean|string) (Optional) The error message that will appear. Nothing will appear if '' is entered. Accepts allowed variables (See). Bool true shows the default message. The default is true.

Return

  • Bouncer Bouncer instance.

Examples

Field is required
$bouncer = new Bouncer(array(
  'field' => ''
));
$bouncer
->name('field')
->required()
->validate();

↑ Return to top


length

length( integer|string $length, boolean|string $message = true )

Field has to be X characters long.

Parameters

  • $length (integer|string) Character length.
  • $message (boolean|string) (Optional) The error message that will appear. Nothing will appear if '' is entered. Accepts allowed variables (See). Bool true shows the default message. The default is true.

Return

  • Bouncer Bouncer instance.

Examples

Limiting a field to a certain number of characters
$bouncer = new Bouncer(array(
  'field' => 'Hello'
));
$bouncer
->name('field')
->length(4)
->validate();

↑ Return to top


minlength

minlength( integer|string $minlength, boolean|string $message = true )

Field has to be greater than or equal to X characters long.

Parameters

  • $minlength (integer|string) Minimum character length.
  • $message (boolean|string) (Optional) The error message that will appear. Nothing will appear if '' is entered. Accepts allowed variables (See). Bool true shows the default message. The default is true.

Return

  • Bouncer Bouncer instance.

Examples

Forcing the field to be above a certain character
$bouncer = new Bouncer(array(
  'field' => 'Hello'
));
$bouncer
->name('field')
->minlength(10)
->validate();

↑ Return to top


maxlength

maxlength( integer|string $maxlength, boolean|string $message = true )

Field has to be less than or equal to X characters long.

Parameters

  • $maxlength (integer|string) Maximum character length.
  • $message (boolean|string) (Optional) The error message that will appear. Nothing will appear if '' is entered. Accepts allowed variables (See). Bool true shows the default message. The default is true.

Return

  • Bouncer Bouncer instance.

Examples

Forcing the field to be below a certain character
$bouncer = new Bouncer(array(
  'field' => 'Hello'
));
$bouncer
->name('field')
->maxlength(3)
->validate();

↑ Return to top


betweenlength

betweenlength( integer|string $minlength, integer|string $maxlength, boolean|string $message = true )

Field has to be between minlength and maxlength characters long.

Parameters

  • $minlength (integer|string) Minimum character length.
  • $maxlength (integer|string) Maximum character length.
  • $message (boolean|string) (Optional) The error message that will appear. Nothing will appear if '' is entered. Accepts allowed variables (See). Bool true shows the default message. The default is true.

Return

  • Bouncer Bouncer instance.

Examples

Limit the field to a specific range of characters
$bouncer = new Bouncer(array(
  'field' => 'Hello'
));
$bouncer
->name('field')
->betweenlength(1, 50, 'The field has an error in the number of characters')
->validate();

↑ Return to top


min

min( integer|string $min, boolean|string $message = true )

Field must be a number greater than [or equal to] X.

Parameters

  • $min (integer|string) Minimum number.
  • $message (boolean|string) (Optional) The error message that will appear. Nothing will appear if '' is entered. Accepts allowed variables (See). Bool true shows the default message. The default is true.

Return

  • Bouncer Bouncer instance.

Examples

Force a field to be numerically above a certain value
$bouncer = new Bouncer(array(
  'field' => '99'
));
$bouncer
->name('field')
->min(100, 'Error: This field can be a minimum of %argument1%.')
->validate();

↑ Return to top


max

max( integer|string $max, boolean|string $message = true )

Field must be a number less than [or equal to] X.

Parameters

  • $max (integer|string) Maximum number.
  • $message (boolean|string) (Optional) The error message that will appear. Nothing will appear if '' is entered. Accepts allowed variables (See). Bool true shows the default message. The default is true.

Return

  • Bouncer Bouncer instance.

Examples

Force a field to be numerically below a certain value
$bouncer = new Bouncer(array(
  'field' => '99'
));
$bouncer
->name('field')
->max(50, 'Error: This field can be a maximum of %argument1%.')
->validate();

↑ Return to top


between

between( integer|string $min, boolean|string $max, boolean|string $message = true )

Field must be a number between X and Y.

Parameters

  • $min (integer|string) Minimum number.
  • $max (integer|string) Maximum number.
  • $message (boolean|string) (Optional) The error message that will appear. Nothing will appear if '' is entered. Accepts allowed variables (See). Bool true shows the default message. The default is true.

Return

  • Bouncer Bouncer instance.

Examples

Numerically forcing a field to a value within a specified range
$bouncer = new Bouncer(array(
  'field' => '99'
));
$bouncer
->name('field')
->between(50, 60, 'This field should be between %argument1% and %argument2%.')
->validate();

↑ Return to top


boolean

boolean( boolean|string $message = true )

Field must be a valid boolean value.

Parameters

  • $message (boolean|string) (Optional) The error message that will appear. Nothing will appear if '' is entered. Accepts allowed variables (See). Bool true shows the default message. The default is true.

Return

  • Bouncer Bouncer instance.

Examples

Field must be a valid boolean value
$bouncer = new Bouncer(array(
  'field' => 'false'
));
$bouncer
->name('field')
->boolean()
->validate();

↑ Return to top


integer

integer( boolean|string $message = true )

Field must be a valid integer value.

Parameters

  • $message (boolean|string) (Optional) The error message that will appear. Nothing will appear if '' is entered. Accepts allowed variables (See). Bool true shows the default message. The default is true.

Return

  • Bouncer Bouncer instance.

Examples

Field must be a valid integer value
$bouncer = new Bouncer(array(
  'field' => '5'
));
$bouncer
->name('field')
->integer('Please enter a valid integer value.')
->validate();

↑ Return to top


digits

digits( boolean|string $message = true )

Field must be a valid digits.

This is just like integer(), except there is no upper limit.

Parameters

  • $message (boolean|string) (Optional) The error message that will appear. Nothing will appear if '' is entered. Accepts allowed variables (See). Bool true shows the default message. The default is true.

Return

  • Bouncer Bouncer instance.

Examples

Field must be a valid digits
$bouncer = new Bouncer(array(
  'field' => '3e2'
));
$bouncer
->name('field')
->digits('Please enter a valid digits.')
->validate();

↑ Return to top


float

float( boolean|string $message = true )

Field must be a valid float number.

Parameters

  • $message (boolean|string) (Optional) The error message that will appear. Nothing will appear if '' is entered. Accepts allowed variables (See). Bool true shows the default message. The default is true.

Return

  • Bouncer Bouncer instance.

Examples

Field must be a valid float number
$bouncer = new Bouncer(array(
  'field' => '25.5'
));
$bouncer
->name('field')
->float()
->validate();

↑ Return to top


numeric

numeric( boolean|string $message = true )

Field must be a valid numeric value.

Parameters

  • $message (boolean|string) (Optional) The error message that will appear. Nothing will appear if '' is entered. Accepts allowed variables (See). Bool true shows the default message. The default is true.

Return

  • Bouncer Bouncer instance.

Examples

Field must be a valid numeric value
$bouncer = new Bouncer(array(
  'field' => '56.6e3'
));
$bouncer
->name('field')
->numeric()
->validate();

↑ Return to top


even

even( boolean|string $message = true )

Field must be an even number.

Parameters

  • $message (boolean|string) (Optional) The error message that will appear. Nothing will appear if '' is entered. Accepts allowed variables (See). Bool true shows the default message. The default is true.

Return

  • Bouncer Bouncer instance.

Examples

Field must be an even number
$bouncer = new Bouncer(array(
  'field' => '71'
));
$bouncer
->name('field')
->even()
->validate();

↑ Return to top


odd

odd( boolean|string $message = true )

Field must be an odd number.

Parameters

  • $message (boolean|string) (Optional) The error message that will appear. Nothing will appear if '' is entered. Accepts allowed variables (See). Bool true shows the default message. The default is true.

Return

  • Bouncer Bouncer instance.

Examples

Field must be an odd number
$bouncer = new Bouncer(array(
  'field' => '92'
));
$bouncer
->name('field')
->odd()
->validate();

↑ Return to top


matches

matches( string $string, boolean|string $message = true )

Field must match a specific value (password comparison etc).

It can also be called equal() to make it easier to use.

Parameters

  • $string (string) String to match.
  • $message (boolean|string) (Optional) The error message that will appear. Nothing will appear if '' is entered. Accepts allowed variables (See). Bool true shows the default message. The default is true.

Return

  • Bouncer Bouncer instance.

Examples

Field must match a specific value
$bouncer = new Bouncer(array(
  'field' => 'Hello World!'
));
$bouncer
->name('field')
->matches('Hello Bouncer!', 'Field does not match "%argument1%".')
->validate();

↑ Return to top


notmatches

notmatches( string $string, boolean|string $message = true )

Field must differ from a specific value.

It can also be called notequal() to make it easier to use.

Parameters

  • $string (string) String to match.
  • $message (boolean|string) (Optional) The error message that will appear. Nothing will appear if '' is entered. Accepts allowed variables (See). Bool true shows the default message. The default is true.

Return

  • Bouncer Bouncer instance.

Examples

Field must differ from a specific value
$bouncer = new Bouncer(array(
  'field' => 'Hello World!'
));
$bouncer
->name('field')
->matches('Hello World!', 'Field should not match "%argument1%".')
->validate();

↑ Return to top


startswith

startswith( string $string, boolean|string $message = true )

Field must start with a specific substring.

Parameters

  • $string (string) String to match.
  • $message (boolean|string) (Optional) The error message that will appear. Nothing will appear if '' is entered. Accepts allowed variables (See). Bool true shows the default message. The default is true.

Return

  • Bouncer Bouncer instance.

Examples

Field must start with a specific substring
$bouncer = new Bouncer(array(
  'field' => 'Hello World!'
));
$bouncer
->name('field')
->startswith('Hi')
->validate();

↑ Return to top


notstartswith

notstartswith( string $string, boolean|string $message = true )

Field must NOT start with a specific substring.

Parameters

  • $string (string) String to match.
  • $message (boolean|string) (Optional) The error message that will appear. Nothing will appear if '' is entered. Accepts allowed variables (See). Bool true shows the default message. The default is true.

Return

  • Bouncer Bouncer instance.

Examples

Field must NOT start with a specific substring
$bouncer = new Bouncer(array(
  'field' => 'Hello World!'
));
$bouncer
->name('field')
->notstartswith('H')
->validate();

↑ Return to top


endswith

endswith( string $string, boolean|string $message = true )

Field must end with a specific substring.

Parameters

  • $string (string) String to match.
  • $message (boolean|string) (Optional) The error message that will appear. Nothing will appear if '' is entered. Accepts allowed variables (See). Bool true shows the default message. The default is true.

Return

  • Bouncer Bouncer instance.

Examples

Field must end with a specific substring
$bouncer = new Bouncer(array(
  'field' => 'Hello World!'
));
$bouncer
->name('field')
->endswith('.')
->validate();

↑ Return to top


notendswith

notendswith( string $string, boolean|string $message = true )

Field must NOT end with a specific substring.

Parameters

  • $string (string) String to match.
  • $message (boolean|string) (Optional) The error message that will appear. Nothing will appear if '' is entered. Accepts allowed variables (See). Bool true shows the default message. The default is true.

Return

  • Bouncer Bouncer instance.

Examples

Field must NOT end with a specific substring
$bouncer = new Bouncer(array(
  'field' => 'Hello World!'
));
$bouncer
->name('field')
->notendswith('World!')
->validate();

↑ Return to top


oneof

oneof( string|array $allowed, boolean|string $message = true )

Field has to be one of the allowed ones.

Parameters

  • $allowed (string|array) Allowed list. Use an array or separate the words with commas. If you don't want the space to be included in the match, don't add spaces between them.
  • $message (boolean|string) (Optional) The error message that will appear. Nothing will appear if '' is entered. Accepts allowed variables (See). Bool true shows the default message. The default is true.

Return

  • Bouncer Bouncer instance.

Examples

Field has to be one of the allowed ones
$bouncer = new Bouncer(array(
  'field' => 'Hello World!'
));
$bouncer
->name('field')
->oneof('Hello Guys!,Hi World!')
->validate();

↑ Return to top


notoneof

notoneof( string|array $disallowed, boolean|string $message = true )

Field has to be not one of the disallowed ones.

Parameters

  • $disallowed (string|array) Disallowed list. Use an array or separate the words with commas. If you don't want the space to be included in the match, don't add spaces between them.
  • $message (boolean|string) (Optional) The error message that will appear. Nothing will appear if '' is entered. Accepts allowed variables (See). Bool true shows the default message. The default is true.

Return

  • Bouncer Bouncer instance.

Examples

Field has to be not one of the disallowed ones
$bouncer = new Bouncer(array(
  'field' => 'Hello World!'
));
$bouncer
->name('field')
->notoneof(
  'Hello Guys!,Hello Friends!,Hello World!', 
  'The field cannot be one of those (%argument1%).'
)->validate();

↑ Return to top


email

email( boolean $checkDNS = true, boolean|string $message = true )

Field must be a valid email address.

Parameters

  • $checkDNS (boolean) If true, it verifies that it is a valid domain name with DNS records. The default is true.
  • $message (boolean|string) (Optional) The error message that will appear. Nothing will appear if '' is entered. Accepts allowed variables (See). Bool true shows the default message. The default is true.

Return

  • Bouncer Bouncer instance.

Examples

Field must be a valid email address
$bouncer = new Bouncer(array(
  'field' => 'john@example.tld'
));
$bouncer
->name('field')
->email(false, 'Please enter a valid email address.')
->validate();
Field must be a valid email address and valid DNS records
$bouncer = new Bouncer(array(
  'field' => 'john@example.tld'
));
$bouncer
->name('field')
->email(true, 'Please enter a valid email address.')
->validate();

↑ Return to top


ip

ip( boolean|string $message = true )

Field must be valid IP address.

Parameters

  • $message (boolean|string) (Optional) The error message that will appear. Nothing will appear if '' is entered. Accepts allowed variables (See). Bool true shows the default message. The default is true.

Return

  • Bouncer Bouncer instance.

Examples

Field must be valid IP address
$bouncer = new Bouncer(array(
  'field' => '120.58.101-449'
));
$bouncer
->name('field')
->ip('[%value%] is not a valid IP address!')
->validate();

↑ Return to top


url

url( boolean|string $message = true )

Field must be valid internet address.

Parameters

  • $message (boolean|string) (Optional) The error message that will appear. Nothing will appear if '' is entered. Accepts allowed variables (See). Bool true shows the default message. The default is true.

Return

  • Bouncer Bouncer instance.

Examples

Field must be valid internet address
$bouncer = new Bouncer(array(
  'field' => 'example.tld'
));
$bouncer
->name('field')
->url('[%value%] is not a valid URL!')
->validate();

↑ Return to top


color

color( boolean|string $message = true )

Field must be valid hexadecimal color code.

Parameters

  • $message (boolean|string) (Optional) The error message that will appear. Nothing will appear if '' is entered. Accepts allowed variables (See). Bool true shows the default message. The default is true.

Return

  • Bouncer Bouncer instance.

Examples

Field must be valid hexadecimal color code
$bouncer = new Bouncer(array(
  'field' => '#ff0000'
));
$bouncer
->name('field')
->url('[%value%] is not a valid color code!')
->validate();

↑ Return to top


ccnum

ccnum( boolean|string $message = true )

Field must be a valid credit card number format.

Parameters

  • $message (boolean|string) (Optional) The error message that will appear. Nothing will appear if '' is entered. Accepts allowed variables (See). Bool true shows the default message. The default is true.

Return

  • Bouncer Bouncer instance.

Examples

Field must be a valid credit card number format
$bouncer = new Bouncer();
$bouncer
->name('field')
->value('12345678912345')
->ccnum('[%value%] is not a valid credit card number!')
->validate();

↑ Return to top


tcnum

ccnum( boolean|string $message = true )

Field must be a valid Turkish Republic identification number.

Parameters

  • $message (boolean|string) (Optional) The error message that will appear. Nothing will appear if '' is entered. Accepts allowed variables (See). Bool true shows the default message. The default is true.

Return

  • Bouncer Bouncer instance.

Examples

Field must be a valid Turkish Republic identification number
$bouncer = new Bouncer();
$bouncer
->name('field')
->value('1216608554128')
->label('T.C. identification number')
->tcnum('[%label%] field is not a valid!')
->validate();

↑ Return to top


callback

callback( callable $callback, mixed $params = array(), boolean|string $message = true )

Add custom callable validation functionality.

Parameters

  • $callback (callable) The function itself or the name of the function you want to be called.

If you want to call a method, pass it as a array. Example: array('class', 'method');

  • $params (mixed) (Optional) The parameter array to be passed.
  • $message (boolean|string) (Optional) The error message that will appear. Nothing will appear if '' is entered. Accepts allowed variables (See). Bool true shows the default message. The default is true.

Return

  • Bouncer Bouncer instance.

Examples

Calling an external function
$bouncer = new Bouncer(array(
  'field' => '76e4'
));
$bouncer
->name('field')
->callback('is_numeric', null, 'Field is not numeric.')
->validate();
Comparing the values of two fields
$bouncer = new Bouncer(array(
  'pass' => '12345',
  'repass' => '01234'
));
$bouncer
->name('repass')
->callback(function( $repass, $pass ) {
  return ( $repass === $pass );
}, array($bouncer->get('value', 'pass')), 'Passwords do not match!')
->validate();
Creating a custom function
$bouncer = new Bouncer(array(
  'field' => 'Hello Guys!'
));
$bouncer
->name('field')
->callback(function( $value, $arg1 ){
  return ( $arg1 == 'Hello' && false !== strpos($value, 'World') );
}, array('Hello'), 'Since %value% contains the word "Hello", you should also use the word "World" at least once!')
->validate();
Addicted to another field
$bouncer = new Bouncer(array(
  'product' => 'null',
  'quantity' => '3'
));
$bouncer
->name('piece')
->required()
->integer()
->max(5)
->min(1)
->callback(
  function( $value, $isProductValid ){
    return ( $isProductValid === 1 );
  }, 
  array( $bouncer->get('valid', 'product') ), 
  'Please select a valid product before choosing quantity.'
)
->validate();

↑ Return to top


filter

filter( callable $callback )

Filter before validating a field's value.

Parameters

  • $callback (callable) The function itself or the name of the function you want to be called.
  • $message (boolean|string) (Optional) The error message that will appear. Nothing will appear if '' is entered. Accepts allowed variables (See). Bool true shows the default message. The default is true.

Return

  • Bouncer Bouncer instance.

Examples

Filtering by external function
$bouncer = new Bouncer(array(
  'field' => '10'
));
$bouncer
->name('field')
->filter('intval')
->integer()
->validate();
Create new custom function to filter
$bouncer = new Bouncer(array(
  'field' => ' John Doe '
));
$bouncer
->name('field')
->filter(function( $value ){
  return trim( $value );
})
->validate();

↑ Return to top


Properties

Bouncer contains two different data: body data including all field data and data of only a specific field.

Properties of the whole body

  • fieldset - Get fields of the whole body,
  • errors - Get error messages of the whole body ,
  • valid - Get validity of the whole body. (see)

valid property

Status Description
1 all fields are valid
0 at least one field is invalid
-1 at least one field is not validated

Examples

Get all data of body
$bouncer->get();
Get all error messages
$bouncer->get('errors');
Another way to do this
$bouncer->get()['messages'];
Get the body validation result
$bouncer->get('valid');

↑ Return to top

Properties of the field

  • value - Get value of field,
  • errors - Get error messages of the field,
  • valid - Get validity of the field, (see)
  • messages - Predefined error messages of the field,
  • rules - Predefined rules of the field,
  • functions - Predefined functions of the field,
  • arguments - Predefined arguments of the field.

valid property

Status Description
1 the field is valid
0 the field is invalid
-1 field not yet validated

Examples

Get the all data of a specific field
$bouncer->get('', 'fieldname');
Get the validation result of a specific field
$bouncer->get('valid', 'fieldname');
Get the rules of a specific field
$bouncer->get('rules', 'fieldname');

↑ Return to top


Variables

Bouncer can filter a number of variables so you can use them in error messages.

  • %value% - Outputs the value of the field.
  • %label% - Outputs the label of the field if preset, otherwise the name of the field.
  • %argument1% - Outputs the first parameter of the related rule.
  • %argument2% - Outputs the second parameter of the related rule.

Examples

Mentioning the value of the field in the error message
$bouncer
->name('piece')
->integer('%value% is not a integer.')
->validate();
Mentioning the arguments of the rule in the error message
$bouncer
->name('piece')
->min(100, 'Error: This field can be a minimum of %argument1%.')
->validate();

← Back to readme / ↑ Return to top