Phly Documentation Phly
[ return to channel ] [ class tree: Phly ] [ index: Phly ] [ all elements ]

Class: Phly_InputFilter

Source Location: /Phly_InputFilter/Phly/InputFilter.php

Class Phly_InputFilter

Class Overview

Phly_InputFilter: basic input validation/normalization

Utilizes class methods to do basic validation and normalization. Extend the class to add custom validations/normalizations, or write libraries of functions to do the work.

Examples:

  1. $filter = new Phly_InputFilter();
  2. $filter->addValidation('phone', 'ctype_digit', 'Phone number must contain only digits');
  3. $filter->addValidation('email', 'isEmail', 'Invalid email address');
  4. $filter->addValidation('username', 'required');
  5. $filter->addValidation('company', 'noValidate');
  6.  
  7. $filter->addNormalization('username', 'strip_tags');
  8. $filter->addNormalization('company', 'trim');
  9.  
  10. if (!$clean = $filter->run($_POST)) {
  11. $errors = $filter->getErrors();
  12. return $errors;
  13. }
  14. // do something with your clean variables...

Validations and normalizations can use any valid method or function. Methods must be in the current class. Extend the Phly_InputFilter class to add custom validations:

  1. class MyForm extends Phly_InputFilter
  2. {
  3. public function isUsername($value) {
  4. if (ctype_alpha($value) && (4 < strlen($value))) {
  5. return true;
  6. }
  7. return false;
  8. }
  9.  
  10. public function clean($value) {
  11. return htmlentities(strip_tags(trim($value)));
  12. }
  13. }

Located in /Phly_InputFilter/Phly/InputFilter.php [line 80]



		
				Author(s):
		
		
		
Information Tags:
Version:  @release-version@
Copyright:  2006 - Present, Matthew Weier O'Phinney
[ Top ]
Property Summary
array   $_config   Configuration array
string   $_emailRegex   Email regex
array   $_errors   Array of errors. Each key points to an array of error messages.
array   $_normalizations   Array of normalizations. Each key points to an array of callbacks used to
array   $_params   Array of parameters being tested in run(); provided to allow access to other parameters when evaluating within a validation.
array   $_required   Array of required items
array   $_validations   Array of validations. Each key points to an array of associative arrays with they keys 'callback' and 'message'.

[ Top ]
Method Summary
void   addNormalization()   Add a normalization to a field
void   addValidation()   Add a validation to a field
float   castFloat()   cast a value to a float
int   castInt()   cast a value to an integer
string   castString()   cast a value to a string
mixed   config()   Configuration
false|array   getErrors()   Retrieve a list of key => error message pairs
mixed   getParam()   Return a parameter from the $_params list
string   htmlSafe()   Make a string HTML safe
boolean   isBoolean()   Value is boolean
boolean   isBooleanInt()   Check if an integer value is a boolean (0 or 1)
boolean   isEmail()   Validate an email address
boolean   isNotZero()   Value is not zero
boolean   isPercent()   value is a percentage
boolean   isPositive()   Value is positive
boolean   isUrl()   Validate a URL
true   noValidate()   Dummy validation
boolean   required()   Value required
array|false   run()   Run filters and normalizations
void   setParam()   Set a parameter in the $_params list
boolean   _isError()   Determine if a field has validation errors
mixed   _normalize()   Normalize a field's value
void   _pushError()   Push an error on to a field's error stack
null|boolean   _validate()   Validate a field against a value

[ Top ]
Properties
array   $_config = array() [line 128]

Configuration array

API Tags:
Access:  protected


[ Top ]
string   $_emailRegex = '/^((\"[^\"\f\n\r\t\v\b]+\")|([\w\!\#\$\%\&\'\*\+\-\~\/\^\`\
|\{\}]+(\.[\w\!\#\$\%\&\'\*\+\-\~\/\^\`\|\{\}]+)*))@((\[(((25[0-5])|(2[0-4][
0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[
0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?
[0-9])))\])|(((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0
-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0
-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9])))|((([A-Za-z0-9\-])+\.)+[A-Za-z\-]+))$/'
[line 86]

Email regex

API Tags:
Static:  
Access:  protected


[ Top ]
array   $_errors = array() [line 92]

Array of errors. Each key points to an array of error messages.

API Tags:
Access:  protected


[ Top ]
array   $_normalizations = array() [line 99]

Array of normalizations. Each key points to an array of callbacks used to

normalize the field

API Tags:
Access:  protected


[ Top ]
array   $_params = array() [line 107]

Array of parameters being tested in run(); provided to allow access to other parameters when evaluating within a validation.

API Tags:
Access:  protected


[ Top ]
array   $_required = array() [line 114]

Array of required items

API Tags:
Access:  protected


[ Top ]
array   $_validations = array() [line 121]

Array of validations. Each key points to an array of associative arrays with they keys 'callback' and 'message'.

API Tags:
Access:  protected


[ Top ]
Methods
addNormalization  [line 226]

  void addNormalization( string $field, string $method  )

Add a normalization to a field

Parameters:
string   $field: 
string   $method:  A class method or a function to use

API Tags:
Access:  public


[ Top ]
addValidation  [line 189]

  void addValidation( string $field, string $method, [string $message = 'Failed validation']  )

Add a validation to a field

Parameters:
string   $field: 
string   $method:  A class method or a function to use
string   $message:  Optional; error message to display on failed validation

API Tags:
Access:  public

Information Tags:
Throws:  Phly_InputFilter_Exception if method is not a valid method or function

[ Top ]
castFloat  [line 649]

  float castFloat( mixed $value  )

cast a value to a float

Parameters:
mixed   $value: 

API Tags:
Access:  public


[ Top ]
castInt  [line 637]

  int castInt( mixed $value  )

cast a value to an integer

Parameters:
mixed   $value: 

API Tags:
Access:  public


[ Top ]
castString  [line 661]

  string castString( mixed $value  )

cast a value to a string

Parameters:
mixed   $value: 

API Tags:
Access:  public


[ Top ]
config  [line 151]

  mixed config( string $key, mixed $value  )

Configuration

Allows storing/retrieving values for use in validations and normalizations.

Passing no values returns the entire $_config.

Passing a single string value returns the value associated with that key in $_config.

Passing two values associates the second value with the key specified in the first in $_config.

If all else fails, returns null.

Parameters:
string   $key:  Optional; configuration key
mixed   $value:  Optional; value to store in $key

API Tags:
Access:  public


[ Top ]
getErrors  [line 417]

  false|array getErrors( )

Retrieve a list of key => error message pairs

Retrieves a list of key => array of error messages.


API Tags:
Access:  public


[ Top ]
getParam  [line 433]

  mixed getParam( string $key  )

Return a parameter from the $_params list

Parameters:
string   $key: 

API Tags:
Access:  public


[ Top ]
htmlSafe  [line 625]

  string htmlSafe( string $value  )

Make a string HTML safe

Parameters:
string   $value: 

API Tags:
Access:  public


[ Top ]
isBoolean  [line 529]

  boolean isBoolean( mixed $value  )

Value is boolean

Tests for various flavors of boolean:

  • 'Y' or 'N' (case insenstitive; also 'yes' and 'no')
  • true or false
  • 'T' or 'F' (case insentitive; also 'true' and 'false')
  • 0 or 1

Parameters:
mixed   $value: 

API Tags:
Access:  public


[ Top ]
isBooleanInt  [line 555]

  boolean isBooleanInt( mixed $value  )

Check if an integer value is a boolean (0 or 1)

Parameters:
mixed   $value: 

API Tags:
Access:  public


[ Top ]
isEmail  [line 466]

  boolean isEmail( string $value  )

Validate an email address

Parameters:
string   $value: 

API Tags:
Access:  public


[ Top ]
isNotZero  [line 571]

  boolean isNotZero( mixed $value  )

Value is not zero

Parameters:
mixed   $value: 

API Tags:
Access:  public


[ Top ]
isPercent  [line 588]

  boolean isPercent( mixed $value  )

value is a percentage

Parameters:
mixed   $value: 

API Tags:
Access:  public


[ Top ]
isPositive  [line 604]

  boolean isPositive( mixed $value  )

Value is positive

Parameters:
mixed   $value: 

API Tags:
Access:  public


[ Top ]
isUrl  [line 482]

  boolean isUrl( string $value  )

Validate a URL

Parameters:
string   $value: 

API Tags:
Access:  public


[ Top ]
noValidate  [line 495]

  true noValidate( mixed $value  )

Dummy validation

Parameters:
mixed   $value: 

API Tags:
Access:  public


[ Top ]
required  [line 507]

  boolean required( mixed $value  )

Value required

Parameters:
mixed   $value: 

API Tags:
Access:  public


[ Top ]
run  [line 339]

  array|false run( mixed $array  )

Run filters and normalizations

Loops through each key in $array, using only those registered in addValidation() and/or addNormalization().

Returns array of clean variables, or false on validation error. Error messages may be retrieved using getErrors().

Note: Only variables that have validations are run through normalizations; if you wish to normalize a variable without validating it, add an 'noValidate' validation to the field.


API Tags:
Access:  public

Information Tags:
Throws:  Phly_InputFilter_Exception

[ Top ]
setParam  [line 450]

  void setParam( string $key, mixed $value  )

Set a parameter in the $_params list

Parameters:
string   $key: 
mixed   $value: 

API Tags:
Access:  public


[ Top ]
_isError  [line 266]

  boolean _isError( string $field  )

Determine if a field has validation errors

Parameters:
string   $field: 

API Tags:
Access:  protected


[ Top ]
_normalize  [line 311]

  mixed _normalize( string $field, mixed $value  )

Normalize a field's value

Run normalizations on a field. Normalizations are run in the order in which they are registered.

Parameters:
string   $field: 
mixed   $value: 

API Tags:
Access:  protected


[ Top ]
_pushError  [line 250]

  void _pushError( string $field, string $message  )

Push an error on to a field's error stack

Parameters:
string   $field: 
string   $message: 

API Tags:
Access:  protected


[ Top ]
_validate  [line 284]

  null|boolean _validate( string $field, mixed $value  )

Validate a field against a value

Parameters:
string   $field: 
mixed   $value: 

API Tags:
Return:  Returns boolean success or failure; returns null if key has no validations associated
Access:  protected


[ Top ]