SproutCMS

This is the code documentation for the SproutCMS project

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');
}

Class structure

As a function argument

collateData 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

validateAddress from class LocaleInfoGBR

Validate address fields

Argument name: $valid
From /sprout/Helpers/Locales/LocaleInfoGBR.php

validateAddress from class LocaleInfoCHN

Validate address fields

Argument name: $valid
From /sprout/Helpers/Locales/LocaleInfoCHN.php

validateAddress from class LocaleInfoCAN

Validate address fields

Argument name: $valid
From /sprout/Helpers/Locales/LocaleInfoCAN.php

validateAddress from class LocaleInfoUSA

Validate address fields

Argument name: $valid
From /sprout/Helpers/Locales/LocaleInfoUSA.php

validateAddress from class LocaleInfo

Validate address fields

Argument name: $valid
From /sprout/Helpers/Locales/LocaleInfo.php

validateAddress from class LocaleInfoAUS

Validate address fields

Argument name: $valid
From /sprout/Helpers/Locales/LocaleInfoAUS.php

validateAddress from class LocaleInfoNZL

Validate address fields

Argument name: $valid
From /sprout/Helpers/Locales/LocaleInfoNZL.php

validateAddress from class LocaleInfoGBR

Validate address fields

Argument name: $valid
From /sprout/Helpers/Locales/LocaleInfoGBR.php

validateAddress from class LocaleInfoCAN

Validate address fields

Argument name: $valid
From /sprout/Helpers/Locales/LocaleInfoCAN.php

validateAddress from class LocaleInfoCHN

Validate address fields

Argument name: $valid
From /sprout/Helpers/Locales/LocaleInfoCHN.php

validateAddress from class LocaleInfoUSA

Validate address fields

Argument name: $valid
From /sprout/Helpers/Locales/LocaleInfoUSA.php

validateAddress from class LocaleInfo

Validate address fields

Argument name: $valid
From /sprout/Helpers/Locales/LocaleInfo.php

validateAddress from class LocaleInfoAUS

Validate address fields

Argument name: $valid
From /sprout/Helpers/Locales/LocaleInfoAUS.php

validateAddress from class LocaleInfoNZL

Validate address fields

Argument name: $valid
From /sprout/Helpers/Locales/LocaleInfoNZL.php