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'); } Class structureAs a function argumentcollateData from class JsonForm Collates POST data using specified config options Argument name: $validator From /sprout/Helpers/JsonForm.php collateFieldData from class JsonForm Collates a single field's $_POST data for INSERT/UPDATE queries, and performs validation Argument name: $valid From /sprout/Helpers/JsonForm.php jsonExtraValidate from class Controller Do any additional validation prior to saving the record Argument name: $validator From /sprout/Controllers/Controller.php jsonExtraValidate from class RedirectAdminController Do any additional validation prior to saving the record Argument name: $validator From /sprout/Controllers/Admin/RedirectAdminController.php jsonExtraValidate from class Controller Do any additional validation prior to saving the record Argument name: $validator From /sprout/Controllers/Controller.php jsonExtraValidate from class RedirectAdminController Do any additional validation prior to saving the record Argument name: $validator From /sprout/Controllers/Admin/RedirectAdminController.php validate from class MultiStepFormController validate from class MultiStepFormController validateAddress from class LocaleInfoGBR validateAddress from class LocaleInfoCHN validateAddress from class LocaleInfoCAN validateAddress from class LocaleInfoUSA validateAddress from class LocaleInfo validateAddress from class LocaleInfoAUS validateAddress from class LocaleInfoNZL validateAddress from class LocaleInfoGBR validateAddress from class LocaleInfoCAN validateAddress from class LocaleInfoCHN validateAddress from class LocaleInfoUSA validateAddress from class LocaleInfo validateAddress from class LocaleInfoAUS validateAddress from class LocaleInfoNZL |
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 |