SproutCMS

This is the code documentation for the SproutCMS project

Page options:

Inherited members

class Validator

New validation class for Sprout 3.
Used with the Validity class.

Example

// Multiedit example for a course with students
 
$has_error = false;
 
$valid = new Validator($_POST);
$valid->required(['name']);
$valid->check('name', 'Validity::length', 1, 100);
 
if ($valid->hasErrors()) {
    $_SESSION['course_edit']['field_errors'] = $valid->getFieldErrors();
    $valid->createNotifications();
    $has_error = true;
}
 
if (empty($_POST['multiedit_students'])) {
    $_POST['multiedit_students'] = [];
}
 
$record_num = 0;
foreach ($_POST['multiedit_students'] as $idx => $data) {
    if (MultiEdit::recordEmpty($data)) continue;
 
    ++$record_num;
 
    $multi_valid = new Validator($data);
    $multi_valid->setLabels([
        'name' => 'Name for student ' . $record_num,
        'email' => 'Email address for student ' . $record_num,
    ]);
 
    $multi_valid->required(['name', 'email']);
    $multi_valid->check('name', 'Validity::length', 1, 100);
    $multi_valid->check('email', 'Validity::email');
 
    if ($multi_valid->hasErrors()) {
        $_SESSION['course_edit']['field_errors']['multiedit_students'][$idx] = $multi_valid->getFieldErrors();
        $multi_valid->createNotifications();
        $has_error = true;
    }
}
 
if ($has_error) {
    Url::redirect('course/edit');
}

Example

// Plain example
 
$valid = new Validator($_POST);
 
$valid->required(['name', 'email']);
 
$valid->check('name', 'Validity::length', 1, 100);
$valid->check('email', 'Validity::email');
 
if ($valid->hasErrors()) {
    $_SESSION['register']['field_errors'] = $valid->getFieldErrors();
    $valid->createNotifications();
    Url::redirect('user/register');
}

Variables

NameVisibilityDescription
$data 
$field_errors 
$general_errors 
$labels 

Functions

NameVisibilityDescription
__constructpublic 
addArrayFieldErrorpublicAdd an error message for a given field to the field errors list
addFieldErrorpublicAdd an error message for a given field to the field errors list
addGeneralErrorpublicAdd a general error message, e.g. for errors affecting many fields
addMultipleFieldErrorpublicAdd an error message from a multiple-field validation (e.g. checking at least one is set)
arrayCheckpublicRun a validation check against each value in an array.
checkpublicCheck the value of a field against a validation method, storing any error messages received
createNotificationspublicCreate notification error messages for each error
expandNsprotectedFor a given function, expand the namespace for Sprout helpers
getFieldErrorspublicGet an array of all field errors, indexed by field name
getGeneralErrorspublicGet an array of all general errors
hasErrorspublic 
isEmptypublic (static)Sadly, the PHP builtin empty() considers '0' to be empty, but it actually isn't
multipleCheckpublicCheck multiple fields against a validation method
requiredpublicChecks various fields are required
setDatapublicUpdate the data to validate
setFieldLabelpublicSet the label for a single field
setFieldValuepublicSet the value for a single data field
setLabelspublicField labels make error messages a little friendlier
trimpublic (static)Recursive trim data

public __construct

void $Validator->__construct ( array $data );

This function does not have a description

public addArrayFieldError

void $Validator->addArrayFieldError ( string $field_name , int $index , string $message );

Add an error message for a given field to the field errors list
This variation is for array validation, e.g. an array of integers

public addFieldError

void $Validator->addFieldError ( string $field_name , string $message );

Add an error message for a given field to the field errors list

public addGeneralError

void $Validator->addGeneralError ( string $message );

Add a general error message, e.g. for errors affecting many fields

public addMultipleFieldError

void $Validator->addMultipleFieldError ( array $fields , string $message );

Add an error message from a multiple-field validation (e.g. checking at least one is set)

public arrayCheck

array $Validator->arrayCheck ( string $field_name , callable $func );

Run a validation check against each value in an array.
Behaviour is very similar to the Validator::check method.

Only supports single-depth arrays

Errors are put into the field_errors array under a subkey matching the array key

Return value is an array of key => boolean with the validation result for each key

public check

bool $Validator->check ( string $field_name , callable $func );

Check the value of a field against a validation method, storing any error messages received
Additional arguments are passed to the underlying method

Methods which are on classes within the Sprout\Helpers namespace do not need the namespace
specified on the function name

If a field has already been checked with Validator::required and the field was empty,
this function will not report errors (but will still return an appropriate value)

If a empty value is provided, it is not validated - returns true

public createNotifications

void $Validator->createNotifications ( [ string $scope ] );

Create notification error messages for each error

protected expandNs

callable $Validator->expandNs ( callable|string $func );

For a given function, expand the namespace for Sprout helpers

public getFieldErrors

array $Validator->getFieldErrors ( );

Get an array of all field errors, indexed by field name
Fields may have multiple errors defined

public getGeneralErrors

array $Validator->getGeneralErrors ( );

Get an array of all general errors

public hasErrors

bool $Validator->hasErrors ( );

This function does not have a description

public isEmpty

bool Validator::isEmpty ( mixed $val );

Sadly, the PHP builtin empty() considers '0' to be empty, but it actually isn't

public multipleCheck

bool $Validator->multipleCheck ( array $fields , callable $func );

Check multiple fields against a validation method

This is similar to Validator::check but it's designed for different validation
methods, which work on a set of fields instead of a single field (e.g. Validity::oneRequired)

Additional arguments are passed to the underlying method
Methods which are on classes within the Sprout\Helpers namespace do not need the namespace
specified on the function name

public required

void $Validator->required ( array $fields );

Checks various fields are required
If a field is required and no value is provided, no other validation will be proessed for that field.

public setData

void $Validator->setData ( array $data );

Update the data to validate

public setFieldLabel

void $Validator->setFieldLabel ( string $field , string $label );

Set the label for a single field

public setFieldValue

void $Validator->setFieldValue ( string $field , mixed $value );

Set the value for a single data field

public setLabels

void $Validator->setLabels ( array $labels );

Field labels make error messages a little friendlier

public trim

array Validator::trim ( array &$data );

Recursive trim data

Alters in-place AND returns the array
This allows for use such as:

   $_SESSION['register']['field_values'] = Validator::trim($_POST);

When used like this, the session gets set and the POST data is also trimmed,
so can be used directly for database inserts.