class ValidationResult (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
string
getModelClass()

No description

public
setModelClass(string $modelClass)

No description

public
mixed
getRecordID()

No description

public
setRecordID(mixed $recordID)

No description

public
addError(string $message, string $messageType = ValidationResult::TYPE_ERROR, string $code = '', string $cast = ValidationResult::CAST_TEXT)

Record an error against this validation result,

public
addFieldError(string $fieldName, string $message, string $messageType = ValidationResult::TYPE_ERROR, string $code = '', string $cast = ValidationResult::CAST_TEXT)

Record an error against this validation result,

public
addMessage(string $message, string $messageType = ValidationResult::TYPE_ERROR, string $code = '', string $cast = ValidationResult::CAST_TEXT)

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

public
addFieldMessage(string $fieldName, string $message, string $messageType = ValidationResult::TYPE_ERROR, string $code = '', string $cast = ValidationResult::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
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

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

string getModelClass()

No description

Return Value

string

ValidationResult setModelClass(string $modelClass)

No description

Parameters

string $modelClass

Return Value

ValidationResult

mixed getRecordID()

No description

Return Value

mixed

ValidationResult setRecordID(mixed $recordID)

No description

Parameters

mixed $recordID

Return Value

ValidationResult

ValidationResult addError(string $message, string $messageType = ValidationResult::TYPE_ERROR, string $code = '', string $cast = ValidationResult::CAST_TEXT)

Record an error against this validation result,

Parameters

string $message
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 $cast

Cast type; One of the CAST_ constant definitions.

Return Value

ValidationResult

ValidationResult addFieldError(string $fieldName, string $message, string $messageType = ValidationResult::TYPE_ERROR, string $code = '', string $cast = ValidationResult::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
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 $cast

Cast type; One of the CAST_ constant definitions.

Return Value

ValidationResult

ValidationResult addMessage(string $message, string $messageType = ValidationResult::TYPE_ERROR, string $code = '', string $cast = ValidationResult::CAST_TEXT)

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

Parameters

string $message
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 $cast

Cast type; One of the CAST_ constant definitions.

Return Value

ValidationResult

ValidationResult addFieldMessage(string $fieldName, string $message, string $messageType = ValidationResult::TYPE_ERROR, string $code = '', string $cast = ValidationResult::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
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 $cast

Cast type; One of the CAST_ constant definitions.

Return Value

ValidationResult

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

ValidationResult 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

Return Value

ValidationResult

array __serialize()

No description

Return Value

array

void __unserialize(array $data)

No description

Parameters

array $data

Return Value

void