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'); } Extending this class<?php /** * New class description goes here * * @author Your Name, 2024-11-24 **/ class NewClassName extends Validator { /** * Get an array of all general errors **/ public function getGeneralErrors () { // Method code goes here } public function hasErrors () { // Method code goes here } /** * Create notification error messages for each error **/ public function createNotifications (string $scope) { // Method code goes here } /** * Check multiple fields against a validation method * * This is similar to {@see 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 function multipleCheck (array $fields, callable $func) { // Method code goes here } /** * Add a general error message, e.g. for errors affecting many fields **/ public function addGeneralError (string $message) { // Method code goes here } /** * Get an array of all field errors, indexed by field name * Fields may have multiple errors defined **/ public function getFieldErrors () { // Method code goes here } /** * Add an error message from a multiple-field validation (e.g. checking at least one is set) **/ public function addMultipleFieldError (array $fields, string $message) { // Method code goes here } /** * 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 function addArrayFieldError (string $field_name, int $index, string $message) { // Method code goes here } /** * Add an error message for a given field to the field errors list **/ public function addFieldError (string $field_name, string $message) { // Method code goes here } /** * 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 function required (array $fields) { // Method code goes here } /** * Sadly, the PHP builtin empty() considers '0' to be empty, but it actually isn't **/ public function isEmpty (mixed $val) { // Method code goes here } /** * Run a validation check against each value in an array. * Behaviour is very similar to the {@see 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 function arrayCheck (string $field_name, callable $func) { // Method code goes here } /** * 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 {@see 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 function check (string $field_name, callable $func) { // Method code goes here } /** * For a given function, expand the namespace for Sprout helpers **/ protected function expandNs (callable|string $func) { // Method code goes here } /** * Set the label for a single field **/ public function setFieldLabel (string $field, string $label) { // Method code goes here } /** * Set the value for a single data field **/ public function setFieldValue (string $field, mixed $value) { // Method code goes here } /** * Update the data to validate **/ public function setData (array $data) { // Method code goes here } /** * Field labels make error messages a little friendlier **/ public function setLabels (array $labels) { // Method code goes here } public function __construct (array $data) { // Method code goes here } /** * 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. **/ public function trim (array $data) { // Method code goes here } } ?> |
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 |