New validation class for Sprout 3. Used with the Validity class.
This is the code documentation for the SproutCMS project
Search documentation |
class ValidatorNew 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
Functions
public __constructvoid $Validator->__construct ( array $data ); This function does not have a description public addArrayFieldErrorvoid $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 addFieldErrorvoid $Validator->addFieldError ( string $field_name , string $message ); Add an error message for a given field to the field errors list public addGeneralErrorvoid $Validator->addGeneralError ( string $message ); Add a general error message, e.g. for errors affecting many fields public addMultipleFieldErrorvoid $Validator->addMultipleFieldError ( array $fields , string $message ); Add an error message from a multiple-field validation (e.g. checking at least one is set) public arrayCheckarray $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 checkbool $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 createNotificationsvoid $Validator->createNotifications ( [ string $scope ] ); Create notification error messages for each error protected expandNscallable $Validator->expandNs ( callable|string $func ); For a given function, expand the namespace for Sprout helpers public getFieldErrorsarray $Validator->getFieldErrors ( ); Get an array of all field errors, indexed by field name Fields may have multiple errors defined public getGeneralErrorsarray $Validator->getGeneralErrors ( ); Get an array of all general errors public hasErrorsbool $Validator->hasErrors ( ); This function does not have a description public isEmptybool Validator::isEmpty ( mixed $val ); Sadly, the PHP builtin empty() considers '0' to be empty, but it actually isn't public multipleCheckbool $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 requiredvoid $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 setDatavoid $Validator->setData ( array $data ); Update the data to validate public setFieldLabelvoid $Validator->setFieldLabel ( string $field , string $label ); Set the label for a single field public setFieldValuevoid $Validator->setFieldValue ( string $field , mixed $value ); Set the value for a single data field public setLabelsvoid $Validator->setLabels ( array $labels ); Field labels make error messages a little friendlier public trimarray 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. |
Powered by Pelzini, version 0.9.0 |
Documentation is made available under the
GNU Free Documentation License 1.2. Generated: Monday, 3rd April, 2023 at 02:59 pm |