Validation

1. Introduction

Validation of data happens at several places within an application. User input is validated within a form, processing of database content in the background may also require validation.

The APF provides various validators to control form input. A list of available validators can be found under Forms. Most of the validators described there rely on the Validator implementations shipped along with the APF executing the underlying logic.

2. Definition and Implementation

Validators are defined by the APF\tools\validation\Validator interface:

PHP code
interface Validator { public function isValid($subject); }

Method isValid() returns true if the subject is valid and false in case the input is invalid.

Checking a form input being a given integer value the following can be used:

PHP code
class IntegerValidator implements Validator { public function isValid($subject) { $input = trim($subject); if ($input === (string) (int) $input) { return true; } return false; } }

Usage is as follows:

PHP code
(new IntegerValidator())->isValid(5);
In case you intent to implement your own validator based on the Validator interface you can add any kind of configuration using the constructor and/or additional setter methods.

3. Available validators

The following chapters list the validators delivered with the APF and describe their usage.

3.1. EMailValidator

The EMailValidator allows you to check e-mail addresses. It brings two rule sets that can be adapted as necessary. To change the rule set please provide the desired one during construction of the validator.

EMailValidator::RULE_SIMPLE executes a simple check EMailValidator::RULE_COMPLEX is more orientated towards RFC 822. In case you intend to change the rule set please provide an alternative regular expression to the constructor.

Example:

PHP code
// Simple validation (new EMailValidator())->isValid('foo@bar.de'); (new EMailValidator(EMailValidator::RULE_SIMPLE))->isValid('foo@bar.de'); // RFC 822 validation (new EMailValidator(EMailValidator::RULE_COMPLEX))->isValid('foo@bar.de'); // Custom validation (new EMailValidator('/^([a-zA-Z0-9\.\_\-@]+)$/'))->isValid('foo@bar.de');

3.2. IntegerValidator

Using the IntegerValidator you can check whether the input is a valid number. The Validator does not provide additional configuration options.

Example:

PHP code
(new IntegerValidator())->isValid(5);

3.3. NumberScopeValidator

Using the NumberScopeValidator you can validate numbers in various ways:

  • Match against a given minimum value. In case $minValue is initialized with 0 no limit is defined.
  • Match against a maximum number. In case $minValue is initialized with 0 no limit is defined.
  • Match against a range of values.

In addition the validator offers the following configuration options:

  • Using parameter $onlyIntegersAccepted you can limit the check to accept integers only.
  • $includeLowerEnd defines whether the value may equal the lower end of the range.
  • $includeLowerEnd defines whether the value may equal the upper end of the range.

Example:

PHP code
// Simple test (new NumberScopeValidator())->isValid(5) // Test with a min value (new NumberScopeValidator(5))->isValid(6)); // Test with a max value (new NumberScopeValidator(null, 5))->isValid(4)); // Test with a range (new NumberScopeValidator(3, 5))->isValid(4)); // Test with a range including the lower end (new NumberScopeValidator(3, 5, false, true))->isValid(4)); // Test with a range including the upper end (new NumberScopeValidator(3, 5, false, false, true))->isValid(4)); // Test with a range including both ends (new NumberScopeValidator(3, 5, false, true, true))->isValid(3)); // Test with integer numbers only (new NumberScopeValidator(3, 5, true, true, true))->isValid(3));

3.4. PhoneNumberValidator

The PhoneNumberValidator allows to validate phone numbers. It brings three rule sets that may be adapted on purpose. To change the rule set please provide the desired one during construction of the validator.

The three rule sets are:

  • PhoneNumberValidator::STANDARD (default): Let's you validate most of the phone numbers.
  • PhoneNumberValidator::INTERNATIONAL: Please use this rule to validate international phone numbers.
  • PhoneNumberValidator::INTERNATIONAL_EPP: Please use this rule to validate international phone numbers following the EPP standard.

In case you intend to change the rule set please provide an alternative regular expression to the constructor.

Example:

PHP code
// Simple test (new PhoneNumberValidator())->isValid('+49 (87) 69490555'); // Test for international phone numbers (new PhoneNumberValidator(PhoneNumberValidator::INTERNATIONAL))->isValid('+1 855 192 5323'); // Test for international phone numbers using the EPP standard (new PhoneNumberValidator(PhoneNumberValidator::INTERNATIONAL_EPP))->isValid('+44.02436596672');

3.5. TextLengthValidator

Using the TextLengthValidator you can validate strings complying with a dedicated rule set. Configuration includes the following parameters:

  • $minLength: Defines the minimum length the string must have.
  • $maxLength: Defines the maximum length the string must have. Setting the parameter to 0 means no limit.
  • $mode (optional): Mode defines whether the string is checked including leading and trailing blanks (TextLengthValidator::MODE_LAX) or whether it is trim'd before (TextLengthValidator::MODE_STRICT).

Example:

PHP code
// Test against a minimum length (new TextLengthValidator(3, 0))->isValid('Lorem ipsum')); // Test against a maximum length (new TextLengthValidator(0, 3))->isValid('Lorem ipsum')); // Test against a range (new TextLengthValidator(3, 11))->isValid('Lorem ipsum')); // Test against a range w/o leading and trailing blanks (new TextLengthValidator(3, 11, TextLengthValidator::MODE_STRICT))->isValid('Lorem ipsum'));

3.5. UriValidator

The UriValidator validates URLs. The Validator does not provide additional configuration options.

Example:

PHP code
(new UriValidator())->isValid('http://example.com/foo/bar');

Comments

Do you want to add a comment to the article above, or do you want to post additional hints? So please click here. Comments already posted can be found below.
« 1   »
Entries/Page: | 5 | 10 | 15 | 20 |
1
buy_cialis 24.11.2016, 08:44:09
Discount buy cialis Cannada, Instant Shipping, Best Prices For All Customers!
2
pharmacy 22.09.2016, 13:05:58
This is why diferencias entre online pharmacy cialis v levitra always seems to take for you in these shoppers.