Provided to each validation method to provide useful methods and validation context information.

 Methods

Creates a new ValidatorContext instance for a given Validator.

__construct(\Pajama\Validator $validator) 

Parameters

$validator

\Pajama\Validator

Returns the Validator instance associated with this context.

getValidator() : \Pajama\Validator

Tests whether the given value is null or an empty string.

optional(string $value) : bool

Example:

Validator::addMethod('example', function($context, $value) {
    return $context->optional($value) || ...;
}

Parameters

$value

string

The value to test.

Returns

boolTrue if the value is null or an empty string, false otherwise.

Parses a Pajama-compatible selector into two parts.

parseSelector(string $selector) : array | null

A Pajama-compatible selector has the format:

  • #foo
  • #foo:bar
  • [name=foo]
  • [name=foo]:bar

The two parts are:

  • name The field name in the selector.
  • pseudo-class The pseudo-class portion of the selector.

Example:

$context = ...;
$parts = $context->parseSelector('[name=foo]:checked]');
// $parts['name'] === 'foo'
// $parts['pseudo-class'] === 'checked'

Parameters

$selector

string

A Pajama-compatible selector.

Returns

arraynullAn array containing the two parts of the selector or null if the selector could not be parsed.

Resolves the param into a boolean value.

resolve(string $value, bool | string | callable $param) : bool
  • If the param is a boolean, it will be returned untouched.
  • If the param is a string, it will be parsed as a Pajama-compatible selector.
  • If the param is a callable, it will be called with the passed value.

Parameters

$value

string

The value to be passed to the param if it is a callable.

$param

boolstringcallable

The parameter to resolve.

Returns

bool

Resolve a Pajama-compatible selector expression based on the model values.

resolveExpression(string $expression) : bool

Parameters

$expression

string

Returns

bool

 Properties

 

The Validator instance that this context is being provided for.

$validator : \Pajama\Validator