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

Source code (54 results)

/modules/Demo/Controllers/MultiStepDemoController.php   Highlighted file source

Line 16: use Sprout\Helpers\Validator;
Line 101: Validator::trim($_POST);
Line 102: $valid = new Validator($_POST);
Line 110: Validator::trim($_POST);
Line 111: $valid = new Validator($_POST);

/modules/Welcome/Controllers/WelcomeController.php   Highlighted file source

Line 38: use Sprout\Helpers\Validator;
Line 280: $_SESSION['db_conf']['field_values'] = Validator::trim($_POST);
Line 282: $valid = new Validator($_POST);
Line 552: $_SESSION['super_op']['field_values'] = Validator::trim($_POST);
Line 554: $valid = new Validator($_POST);

/sprout/Controllers/Admin/CategoryAdminController.php   Highlighted file source

Line 29: use Sprout\Helpers\Validator;
Line 168: $valid = new Validator($_POST);
Line 254: $valid = new Validator($_POST);

/sprout/Controllers/Admin/CategoryAdminController.php   Highlighted file source

Line 29: use Sprout\Helpers\Validator;
Line 168: $valid = new Validator($_POST);
Line 254: $valid = new Validator($_POST);

/sprout/Controllers/Admin/EmailTextAdminController.php   Highlighted file source

Line 18: use Sprout\Helpers\Validator;
Line 93: $_SESSION['admin']['field_values'] = Validator::trim($_POST);
Line 96: $valid = new Validator($_POST);

/sprout/Controllers/Admin/EmailTextAdminController.php   Highlighted file source

Line 18: use Sprout\Helpers\Validator;
Line 93: $_SESSION['admin']['field_values'] = Validator::trim($_POST);
Line 96: $valid = new Validator($_POST);

/sprout/Controllers/Admin/ExtraPageAdminController.php   Highlighted file source

Line 22: use Sprout\Helpers\Validator;
Line 114: $_SESSION['admin']['field_values'] = Validator::trim($_POST);
Line 117: $valid = new Validator($_POST);

/sprout/Controllers/Admin/ExtraPageAdminController.php   Highlighted file source

Line 22: use Sprout\Helpers\Validator;
Line 114: $_SESSION['admin']['field_values'] = Validator::trim($_POST);
Line 117: $valid = new Validator($_POST);

/sprout/Controllers/Admin/FileAdminController.php   Highlighted file source

Line 52: use Sprout\Helpers\Validator;
Line 770: $_SESSION['admin']['field_values'] = Validator::trim($_POST);
Line 772: $valid = new Validator($_POST);

/sprout/Controllers/Admin/FileAdminController.php   Highlighted file source

Line 52: use Sprout\Helpers\Validator;
Line 770: $_SESSION['admin']['field_values'] = Validator::trim($_POST);
Line 772: $valid = new Validator($_POST);

/sprout/Controllers/Admin/ManagedAdminController.php   Highlighted file source

Line 44: use Sprout\Helpers\Validator;
Line 574: $_SESSION['admin']['field_values'] = Validator::trim($_POST);
Line 589: $valid = new Validator($_POST);

/sprout/Controllers/Admin/ManagedAdminController.php   Highlighted file source

Line 44: use Sprout\Helpers\Validator;
Line 574: $_SESSION['admin']['field_values'] = Validator::trim($_POST);
Line 589: $valid = new Validator($_POST);

/sprout/Controllers/Admin/MySettingsAdminController.php   Highlighted file source

Line 26: use Sprout\Helpers\Validator;
Line 82: $_SESSION['admin_my_settings']['field_values'] = Validator::trim($_POST);
Line 84: $valid = new Validator($_POST);

/sprout/Controllers/Admin/MySettingsAdminController.php   Highlighted file source

Line 26: use Sprout\Helpers\Validator;
Line 82: $_SESSION['admin_my_settings']['field_values'] = Validator::trim($_POST);
Line 84: $valid = new Validator($_POST);

/sprout/Controllers/Admin/OperatorAdminController.php   Highlighted file source

Line 27: use Sprout\Helpers\Validator;
Line 179: $_SESSION['admin']['field_values'] = Validator::trim($_POST);
Line 182: $valid = new Validator($_POST);
Line 309: $_SESSION['admin']['field_values'] = Validator::trim($_POST);
Line 312: $valid = new Validator($_POST);

/sprout/Controllers/Admin/OperatorAdminController.php   Highlighted file source

Line 27: use Sprout\Helpers\Validator;
Line 179: $_SESSION['admin']['field_values'] = Validator::trim($_POST);
Line 182: $valid = new Validator($_POST);
Line 309: $_SESSION['admin']['field_values'] = Validator::trim($_POST);
Line 312: $valid = new Validator($_POST);

/sprout/Controllers/Admin/OperatorCategoryAdminController.php   Highlighted file source

Line 22: use Sprout\Helpers\Validator;
Line 122: $_SESSION['admin']['field_values'] = Validator::trim($_POST);
Line 128: $valid = new Validator($_POST);

/sprout/Controllers/Admin/OperatorCategoryAdminController.php   Highlighted file source

Line 22: use Sprout\Helpers\Validator;
Line 122: $_SESSION['admin']['field_values'] = Validator::trim($_POST);
Line 128: $valid = new Validator($_POST);

/sprout/Controllers/Admin/PageAdminController.php   Highlighted file source

Line 68: use Sprout\Helpers\Validator;
Line 301: $valid = new Validator($_POST);
Line 1429: $valid = new Validator($_POST);

/sprout/Controllers/Admin/PageAdminController.php   Highlighted file source

Line 68: use Sprout\Helpers\Validator;
Line 301: $valid = new Validator($_POST);
Line 1429: $valid = new Validator($_POST);

/sprout/Controllers/Admin/RedirectAdminController.php   Highlighted file source

Line 21: use Sprout\Helpers\Validator;
Line 74: * @param Validator $validator Validator instance to attach your errors to
Line 77: protected function jsonExtraValidate($id, Validator $validator)
Line 79: $validator->multipleCheck(

/sprout/Controllers/Admin/RedirectAdminController.php   Highlighted file source

Line 21: use Sprout\Helpers\Validator;
Line 74: * @param Validator $validator Validator instance to attach your errors to
Line 77: protected function jsonExtraValidate($id, Validator $validator)
Line 79: $validator->multipleCheck(

/sprout/Controllers/Admin/SubsiteAdminController.php   Highlighted file source

Line 22: use Sprout\Helpers\Validator;
Line 55: $_SESSION['admin']['field_values'] = Validator::trim($_POST);
Line 58: $valid = new Validator($_POST);

/sprout/Controllers/Admin/SubsiteAdminController.php   Highlighted file source

Line 22: use Sprout\Helpers\Validator;
Line 55: $_SESSION['admin']['field_values'] = Validator::trim($_POST);
Line 58: $valid = new Validator($_POST);

/sprout/Controllers/Controller.php   Highlighted file source

Line 32: use Sprout\Helpers\Validator;
Line 420: * @param Validator $validator Validator instance to attach your errors to
Line 423: protected function jsonExtraValidate($id, Validator $validator)
Line 495: $_SESSION[$session_key]['field_values'] = Validator::trim($_POST);
Line 498: $validator = new Validator($_POST);
Line 500: list($data, $errs) = JsonForm::collateData($conf, $mode, $validator, $item_id);
Line 502: $this->jsonExtraValidate($item_id, $validator);
Line 503: $errs = array_merge($errs, $validator->getFieldErrors());
Line 507: $validator->createNotifications();

/sprout/Controllers/Controller.php   Highlighted file source

Line 32: use Sprout\Helpers\Validator;
Line 420: * @param Validator $validator Validator instance to attach your errors to
Line 423: protected function jsonExtraValidate($id, Validator $validator)
Line 495: $_SESSION[$session_key]['field_values'] = Validator::trim($_POST);
Line 498: $validator = new Validator($_POST);
Line 500: list($data, $errs) = JsonForm::collateData($conf, $mode, $validator, $item_id);
Line 502: $this->jsonExtraValidate($item_id, $validator);
Line 503: $errs = array_merge($errs, $validator->getFieldErrors());
Line 507: $validator->createNotifications();

/sprout/Controllers/DbToolsController.php   Highlighted file source

Line 69: use Sprout\Helpers\Validator;
Line 2023: $_SESSION['module_builder_existing']['field_values'] = Validator::trim($_POST);

/sprout/Controllers/DbToolsController.php   Highlighted file source

Line 69: use Sprout\Helpers\Validator;
Line 2023: $_SESSION['module_builder_existing']['field_values'] = Validator::trim($_POST);

/sprout/Controllers/EmailShareController.php   Highlighted file source

Line 30: use Sprout\Helpers\Validator;
Line 150: $_SESSION['email_share']['field_values'] = Validator::trim($_POST);
Line 152: $valid = new Validator($_POST);

/sprout/Controllers/EmailShareController.php   Highlighted file source

Line 30: use Sprout\Helpers\Validator;
Line 150: $_SESSION['email_share']['field_values'] = Validator::trim($_POST);
Line 152: $valid = new Validator($_POST);

/sprout/Controllers/MultiStepFormController.php   Highlighted file source

Line 25: use Sprout\Helpers\Validator;
Line 202: * @param array $rules Each element is an array which is passed to {@see Validator::check},
Line 204: * @param mixed $valid a {@see Validator}, or null to create one on the fly. Specifying a Validator
Line 208: protected function validate($step, array $reqd, array $rules, Validator $valid = null)
Line 213: Validator::trim($_POST);
Line 214: $valid = new Validator($_POST);

/sprout/Controllers/MultiStepFormController.php   Highlighted file source

Line 25: use Sprout\Helpers\Validator;
Line 202: * @param array $rules Each element is an array which is passed to {@see Validator::check},
Line 204: * @param mixed $valid a {@see Validator}, or null to create one on the fly. Specifying a Validator
Line 208: protected function validate($step, array $reqd, array $rules, Validator $valid = null)
Line 213: Validator::trim($_POST);
Line 214: $valid = new Validator($_POST);

/sprout/Helpers/Form.php   Highlighted file source

Line 114: * The output from the {@see Validator::getFieldErrors} method can be used directly as input to this method

/sprout/Helpers/JsonForm.php   Highlighted file source

Line 266: // Metadata which is passed into argReplace for display/validator argument replacement
Line 477: * @param Validator $validator To validate the data; must be created externally so it can be used for other
Line 482: public static function collateData($conf, $mode, Validator $validator, $item_id)
Line 500: // Metadata which is passed into argReplace for display/validator argument replacement
Line 509: $validator->setFieldLabel($field_defn['name'], @$field_defn['label']);
Line 511: self::collateFieldData($field_defn, @$_POST[$field_defn['name']], $metadata, $validator, $data);
Line 516: $segment_validator = new Validator($_POST);
Line 520: self::collateFieldData($temp_defn, @$_POST[$name], $metadata, $segment_validator, $data);
Line 521: $field_errors = $segment_validator->getFieldErrors();
Line 527: $validator->addFieldError($field_defn['name'], $err);
Line 531: $errs = array_merge($errs, $validator->getFieldErrors());
Line 559: if (!isset($valid[$src][$item_num])) $valid[$src][$item_num] = new Validator([]);
Line 602: * @param Validator $valid The validator instance to do validation with
Line 605: protected static function collateFieldData(array $field_defn, $input, array $metadata, Validator $valid, array &$data)
Line 618: if (Validator::isEmpty($input)) {

/sprout/Helpers/Locales/LocaleInfo.php   Highlighted file source

Line 23: use Sprout\Helpers\Validator;
Line 289: * @param Validator $valid Validator for the form being processed
Line 293: public function validateAddress(Validator $valid, $required = false)

/sprout/Helpers/Locales/LocaleInfo.php   Highlighted file source

Line 23: use Sprout\Helpers\Validator;
Line 289: * @param Validator $valid Validator for the form being processed
Line 293: public function validateAddress(Validator $valid, $required = false)

/sprout/Helpers/Locales/LocaleInfoAUS.php   Highlighted file source

Line 16: use Sprout\Helpers\Validator;
Line 44: * @param Validator $valid The validation object to add rules to
Line 47: public function validateAddress(Validator $valid, $required = false)

/sprout/Helpers/Locales/LocaleInfoAUS.php   Highlighted file source

Line 16: use Sprout\Helpers\Validator;
Line 44: * @param Validator $valid The validation object to add rules to
Line 47: public function validateAddress(Validator $valid, $required = false)

/sprout/Helpers/Locales/LocaleInfoCAN.php   Highlighted file source

Line 17: use Sprout\Helpers\Validator;
Line 79: * @param Validator $valid The validation object to add rules to
Line 82: public function validateAddress(Validator $valid, $required = false)

/sprout/Helpers/Locales/LocaleInfoCAN.php   Highlighted file source

Line 17: use Sprout\Helpers\Validator;
Line 79: * @param Validator $valid The validation object to add rules to
Line 82: public function validateAddress(Validator $valid, $required = false)

/sprout/Helpers/Locales/LocaleInfoCHN.php   Highlighted file source

Line 17: use Sprout\Helpers\Validator;
Line 80: * @param Validator $valid The validation object to add rules to
Line 83: public function validateAddress(Validator $valid, $required = false)

/sprout/Helpers/Locales/LocaleInfoCHN.php   Highlighted file source

Line 17: use Sprout\Helpers\Validator;
Line 80: * @param Validator $valid The validation object to add rules to
Line 83: public function validateAddress(Validator $valid, $required = false)

/sprout/Helpers/Locales/LocaleInfoGBR.php   Highlighted file source

Line 17: use Sprout\Helpers\Validator;
Line 174: * @param Validator $valid The validation object to add rules to
Line 177: public function validateAddress(Validator $valid, $required = false)

/sprout/Helpers/Locales/LocaleInfoGBR.php   Highlighted file source

Line 17: use Sprout\Helpers\Validator;
Line 174: * @param Validator $valid The validation object to add rules to
Line 177: public function validateAddress(Validator $valid, $required = false)

/sprout/Helpers/Locales/LocaleInfoNZL.php   Highlighted file source

Line 16: use Sprout\Helpers\Validator;
Line 34: * @param Validator $valid Validator for the form being processed
Line 38: public function validateAddress(Validator $valid, $required = false)

/sprout/Helpers/Locales/LocaleInfoNZL.php   Highlighted file source

Line 16: use Sprout\Helpers\Validator;
Line 34: * @param Validator $valid Validator for the form being processed
Line 38: public function validateAddress(Validator $valid, $required = false)

/sprout/Helpers/Locales/LocaleInfoUSA.php   Highlighted file source

Line 17: use Sprout\Helpers\Validator;
Line 103: * @param Validator $valid The validation object to add rules to
Line 106: public function validateAddress(Validator $valid, $required = false)

/sprout/Helpers/Locales/LocaleInfoUSA.php   Highlighted file source

Line 17: use Sprout\Helpers\Validator;
Line 103: * @param Validator $valid The validation object to add rules to
Line 106: public function validateAddress(Validator $valid, $required = false)

/sprout/Helpers/Model.php   Highlighted file source

Line 17: use karmabunny\kb\RulesValidatorTrait;
Line 39: use RulesValidatorTrait;

/sprout/Helpers/Model.php   Highlighted file source

Line 17: use karmabunny\kb\RulesValidatorTrait;
Line 39: use RulesValidatorTrait;

/sprout/Helpers/Validator.php   Highlighted file source

Line 27: * $valid = new Validator($_POST);
Line 44: * $valid = new Validator($_POST);
Line 64: * $multi_valid = new Validator($data);
Line 85: class Validator
Line 99: * $_SESSION['register']['field_values'] = Validator::trim($_POST);
Line 204: * If a field has already been checked with {@see Validator::required} and the field was empty,
Line 239: * Behaviour is very similar to the {@see Validator::check} method.
Line 249: * $validator = new Validator($data);
Line 250: * $result = $validator->arrayCheck('vals', 'Validity::positiveInt');
Line 252: * $errs = $validator->getFieldErrors();
Line 295: * This is similar to {@see Validator::check} but it's designed for different validation

/sprout/Helpers/Validity.php   Highlighted file source

Line 26: * Used with the {@see Validator} class.

/sprout/tests/validiatorTest.php   Highlighted file source

Line 14: use Sprout\Helpers\Validator;
Line 17: class validatorTest extends PHPUnit_Framework_TestCase
Line 36: $validator = new Validator(['field' => $value]);
Line 38: // Awful hack to pass through varargs to the Validator::check method
Line 41: call_user_func_array([$validator, 'check'], $args);
Line 43: $this->assertTrue($validator->hasErrors());
Line 44: $this->assertCount(1, $validator->getFieldErrors());
Line 45: $this->assertArrayHasKey('field', $validator->getFieldErrors());
Line 46: $errs = $validator->getFieldErrors();
Line 53: $validator = new Validator($data);
Line 55: $results = $validator->arrayCheck('vals', 'Validity::positiveInt');
Line 64: $this->assertTrue($validator->hasErrors());
Line 65: $this->assertCount(1, $validator->getFieldErrors());
Line 66: $this->assertArrayHasKey('vals', $validator->getFieldErrors());
Line 68: $errs = $validator->getFieldErrors();

/sprout/tests/validiatorTest.php   Highlighted file source

Line 14: use Sprout\Helpers\Validator;
Line 17: class validatorTest extends PHPUnit_Framework_TestCase
Line 36: $validator = new Validator(['field' => $value]);
Line 38: // Awful hack to pass through varargs to the Validator::check method
Line 41: call_user_func_array([$validator, 'check'], $args);
Line 43: $this->assertTrue($validator->hasErrors());
Line 44: $this->assertCount(1, $validator->getFieldErrors());
Line 45: $this->assertArrayHasKey('field', $validator->getFieldErrors());
Line 46: $errs = $validator->getFieldErrors();
Line 53: $validator = new Validator($data);
Line 55: $results = $validator->arrayCheck('vals', 'Validity::positiveInt');
Line 64: $this->assertTrue($validator->hasErrors());
Line 65: $this->assertCount(1, $validator->getFieldErrors());
Line 66: $this->assertArrayHasKey('vals', $validator->getFieldErrors());
Line 68: $errs = $validator->getFieldErrors();

A total of 226 lines in 54 files were found