class ValidationResult implements Serializable (View source)

A class that combined as a boolean result with an optional list of error messages.

This is used for returning validation results from validators

Each message can have a code or field which will uniquely identify that message. However, messages can be stored without a field or message as an "overall" message.

Traits

A class that can be instantiated or replaced via DI

Constants

TYPE_ERROR

Standard "error" type

TYPE_GOOD

Standard "good" message type

TYPE_INFO

Non-error message type.

TYPE_WARNING

Warning message type

CAST_HTML

Message type is html

CAST_TEXT

Message type is plain text

Properties

protected bool $isValid

Is the result valid or not.

protected array $messages

List of messages

Methods

public static 
create(mixed ...$args)

An implementation of the factory method, allows you to create an instance of a class

public static 
singleton(string $class = null)

Creates a class instance by the "singleton" design pattern.

public
__construct()

Create a new ValidationResult.

public
$this
addError(string $message, string $messageType = self::TYPE_ERROR, string $code = null, string|bool $cast = self::CAST_TEXT)

Record an error against this validation result,

public
$this
addFieldError(string $fieldName, string $message, string $messageType = self::TYPE_ERROR, string $code = null, string|bool $cast = self::CAST_TEXT)

Record an error against this validation result,

public
$this
addMessage(string $message, string $messageType = self::TYPE_ERROR, string $code = null, string|bool $cast = self::CAST_TEXT)

Add a message to this ValidationResult without necessarily marking it as an error

public
$this
addFieldMessage(string $fieldName, string $message, string $messageType = self::TYPE_ERROR, string $code = null, string|bool $cast = self::CAST_TEXT)

Add a message to this ValidationResult without necessarily marking it as an error

public
bool
isValid()

Returns true if the result is valid.

public
array
getMessages()

Return the full error meta-data, suitable for combining with another ValidationResult.

public
$this
combineAnd(ValidationResult $other)

Combine this Validation Result with the ValidationResult given in other.

public
array
__serialize()

No description

public
void
__unserialize(array $data)

No description

public
string
serialize() deprecated

The __serialize() magic method will be automatically used instead of this

public
unserialize(string $serialized) deprecated

The __unserialize() magic method will be automatically used instead of this almost all the time This method will be automatically used if existing serialized data was not saved as an associative array and the PHP version used in less than PHP 9.0

Details

static Injectable create(mixed ...$args)

An implementation of the factory method, allows you to create an instance of a class

This method will defer class substitution to the Injector API, which can be customised via the Config API to declare substitution classes.

This can be called in one of two ways - either calling via the class directly, or calling on Object and passing the class name as the first parameter. The following are equivalent: $list = DataList::create(SiteTree::class); $list = SiteTree::get();

Parameters

mixed ...$args

Return Value

Injectable

static Injectable singleton(string $class = null)

Creates a class instance by the "singleton" design pattern.

It will always return the same instance for this class, which can be used for performance reasons and as a simple way to access instance methods which don't rely on instance data (e.g. the custom SilverStripe static handling).

Parameters

string $class

Optional classname to create, if the called class should not be used

Return Value

Injectable

The singleton instance

__construct()

Create a new ValidationResult.

By default, it is a successful result. Call $this->error() to record errors.

$this addError(string $message, string $messageType = self::TYPE_ERROR, string $code = null, string|bool $cast = self::CAST_TEXT)

Record an error against this validation result,

Parameters

string $message

The message string.

string $messageType

Passed as a CSS class to the form, so other values can be used if desired. Standard types are defined by the TYPE_ constant definitions.

string $code

A codename for this error. Only one message per codename will be added. This can be usedful for ensuring no duplicate messages

string|bool $cast

Cast type; One of the CAST_ constant definitions. Bool values will be treated as plain text flag.

Return Value

$this

$this addFieldError(string $fieldName, string $message, string $messageType = self::TYPE_ERROR, string $code = null, string|bool $cast = self::CAST_TEXT)

Record an error against this validation result,

Parameters

string $fieldName

The field to link the message to. If omitted; a form-wide message is assumed.

string $message

The message string.

string $messageType

The type of message: e.g. "bad", "warning", "good", or "required". Passed as a CSS class to the form, so other values can be used if desired.

string $code

A codename for this error. Only one message per codename will be added. This can be usedful for ensuring no duplicate messages

string|bool $cast

Cast type; One of the CAST_ constant definitions. Bool values will be treated as plain text flag.

Return Value

$this

$this addMessage(string $message, string $messageType = self::TYPE_ERROR, string $code = null, string|bool $cast = self::CAST_TEXT)

Add a message to this ValidationResult without necessarily marking it as an error

Parameters

string $message

The message string.

string $messageType

The type of message: e.g. "bad", "warning", "good", or "required". Passed as a CSS class to the form, so other values can be used if desired.

string $code

A codename for this error. Only one message per codename will be added. This can be usedful for ensuring no duplicate messages

string|bool $cast

Cast type; One of the CAST_ constant definitions. Bool values will be treated as plain text flag.

Return Value

$this

$this addFieldMessage(string $fieldName, string $message, string $messageType = self::TYPE_ERROR, string $code = null, string|bool $cast = self::CAST_TEXT)

Add a message to this ValidationResult without necessarily marking it as an error

Parameters

string $fieldName

The field to link the message to. If omitted; a form-wide message is assumed.

string $message

The message string.

string $messageType

The type of message: e.g. "bad", "warning", "good", or "required". Passed as a CSS class to the form, so other values can be used if desired.

string $code

A codename for this error. Only one message per codename will be added. This can be usedful for ensuring no duplicate messages

string|bool $cast

Cast type; One of the CAST_ constant definitions. Bool values will be treated as plain text flag.

Return Value

$this

bool isValid()

Returns true if the result is valid.

Return Value

bool

array getMessages()

Return the full error meta-data, suitable for combining with another ValidationResult.

Return Value

array

Array of messages, where each item is an array of data for that message.

$this combineAnd(ValidationResult $other)

Combine this Validation Result with the ValidationResult given in other.

It will be valid if both this and the other result are valid. This object will be modified to contain the new validation information.

Parameters

ValidationResult $other

the validation result object to combine

Return Value

$this

array __serialize()

No description

Return Value

array

void __unserialize(array $data)

No description

Parameters

array $data

Return Value

void

string serialize() deprecated

deprecated 4.12.0 Use __serialize() instead

The __serialize() magic method will be automatically used instead of this

Return Value

string

unserialize(string $serialized) deprecated

deprecated 4.12.0 Use __unserialize() instead

The __unserialize() magic method will be automatically used instead of this almost all the time This method will be automatically used if existing serialized data was not saved as an associative array and the PHP version used in less than PHP 9.0

Parameters

string $serialized