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

Methods

static Injectable
create(array ...$args)

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

static Injectable
singleton(string $class = null)

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

__construct()

Create a new ValidationResult.

$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,

$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,

$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

$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

bool
isValid()

Returns true if the result is valid.

array
getMessages()

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

$this
combineAnd(ValidationResult $other)

Combine this Validation Result with the ValidationResult given in other.

string
serialize()

String representation of object

unserialize(string $serialized)

Constructs the object

Details

static Injectable create(array ...$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'); $list = SiteTree::get();

Parameters

array ...$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

string serialize()

String representation of object

Return Value

string

the string representation of the object or null

unserialize(string $serialized)

Constructs the object

Parameters

string $serialized