utilityValidator

Aggregates various validators and allows an array of data to be validated in one go. This is ideal for handling form data or data that is in an associative array of key => value pairs.

The data to be validated needs to have a string key that identifies it, and then a set of rules can be added based on that key. For example: username might have validators for string, length, regex etc.

If validation fails, an array of messages is stored internally detailing what went wrong during validation. These can be used to correct the data.

utilityValidator supports multiple namespaces that will be scanned for validators. The default is utilityValidate. The leading letter is automatically capitalised.

$data = array(
	'number1' => 123,
	'number2' => 34738,
	'number3' => 123.1121,
	'number4' => 'a23',
);

$rules = array(
	'number1' => array(
		'number' => array()
	),
	'number2' => array(
		'number' => array('min' => 100, 'max' => 1000)
),
	'number3' => array(
		'number' => array('min' => 100, 'max' => 125.0000)
	),
	'number4' => array(
		'number' => array()
	),
);

$oValidator = new utilityValidator($data, $rules);
$oValidator->isValid();
print_r($oValidator);

All validators support translation of the error messages. To enable translations, configure an instance of translateManager and pass this in during instantiation. Now all messages can be translated automatically - so long as the language resources contains strings.

// using translateManager
$translateManager = translateManager::getInstance(
	translateManager::ADAPTOR_CSV, '/path/to/lang.csv', 'en_GB', array()
);

$oValidator = new utilityValidator($data, $rules, $translateManager);
if ( !$oValidator->isValid() ) {
    print_r($oValidator->getMessages());
}

Methods

public __construct([$inData = array()], [$inRules = array()], [$inTranslateManager = null])

Creates a new validator object

public isValid([$inData = null])

Returns true only if ALL data passes validation, otherwise returns false

public reset()

Reset the object

public isModified()

Returns true if object has been modified

public setModified([$status = true])

Set the status of the object if it has been changed

public getData()

Returns the array of data being validated

public setData([$inData = array()])

Set the data to be validated

Data must be an associative array of varName => value with matching rules using varName.

public getNamespaces()

Returns the array of class prefixes to use when searching for validation classes.

public addNamespace($inNamespace)

Adds a new namespace prefix for validators

public setNamespaces($inNamespaces)

Set the array of class prefixes to use when searching for validate classes.

public getMessages()

Returns the array of messages from validation

public addMessage($inVarName, $inMessage)

Adds a message to the internal stack for the set $inVarName

public setMessages($inMessages)

Sets an array of messages, overwriting any existing values

public getRules([$inVarName = null])

Returns the rules for $inVarName or if null, all rules

public setRules([$inRules = array()])

Sets the validation rules for the data

The rules should be an associative array with the following format: varname => validator => array(options for validator) e.g.:

$rules = array();
$rules['MyVar']['Number'] = array('min' => 0, 'max' => 3);

Alternatively, for single validation or to use the default options you can specify rules using the following formats:

$rules = array();
$rules['MyVar'] = array('string','notEmpty','date');
$rules['MyVar2'] = 'string';

public addRule($inVarName, $inValidator, [$inOptions = array()])

Adds a rule that will validate $inVarName against $inValidator

$inValidator is the name of the validator minus any prefix. For example to validate a number use 'number'. The leading letter does not need to be capitalised.

public removeRule($inVarName, $inValidator)

Removes the validator for $inVarName

public getTranslationManager()

Returns the translation manager, or null if not set

public setTranslationManager($inManager)

Sets the translation manager for use with validators

<  1  >