Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API Rename FormField Value to getFormattedValue #11550

Open
wants to merge 1 commit into
base: 6.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
179 changes: 49 additions & 130 deletions src/Core/Validation/ValidationResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,173 +50,95 @@ class ValidationResult
/**
* Is the result valid or not.
* Note that there can be non-error messages in the list.
*
* @var bool
*/
protected $isValid = true;
protected bool $isValid = true;

/**
* List of messages
*
* @var array
*/
protected $messages = [];
protected array $messages = [];

/**
* Record an error against this validation result,
*
* @param string $message The message string.
* @param string $messageType Passed as a CSS class to the form, so other values can be used if desired.
* @param $message The message string.
* @param $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.
* @param string $code A codename for this error. Only one message per codename will be added.
* @param $code A codename for this error. Only one message per codename will be added.
* This can be usedful for ensuring no duplicate messages
* @param string|bool $cast Cast type; One of the CAST_ constant definitions.
* Bool values will be treated as plain text flag.
* @param $cast Cast type; One of the CAST_ constant definitions.
* @return $this
*/
public function addError(
$message,
$messageType = ValidationResult::TYPE_ERROR,
$code = null,
$cast = ValidationResult::CAST_TEXT
) {
if ($code === null) {
Deprecation::notice(
'5.4.0',
'Passing $code as null is deprecated. Pass a blank string instead.',
Deprecation::SCOPE_GLOBAL
);
$code = '';
}
if ($cast === null) {
Deprecation::notice(
'5.4.0',
'Passing $cast as null is deprecated. Pass a ValidationResult::CAST_* constant instead.',
Deprecation::SCOPE_GLOBAL
);
$cast = ValidationResult::CAST_TEXT;
}
string $message,
string $messageType = ValidationResult::TYPE_ERROR,
string $code = '',
string $cast = ValidationResult::CAST_TEXT,
): static {
return $this->addFieldError('', $message, $messageType, $code, $cast);
}

/**
* Record an error against this validation result,
*
* @param string $fieldName The field to link the message to. If omitted; a form-wide message is assumed.
* @param string $message The message string.
* @param string $messageType The type of message: e.g. "bad", "warning", "good", or "required". Passed as a CSS
* @param $fieldName The field to link the message to. If omitted; a form-wide message is assumed.
* @param $message The message string.
* @param $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.
* @param string $code A codename for this error. Only one message per codename will be added.
* @param $code A codename for this error. Only one message per codename will be added.
* This can be usedful for ensuring no duplicate messages
* @param string|bool $cast Cast type; One of the CAST_ constant definitions.
* Bool values will be treated as plain text flag.
* @return $this
* @param $cast Cast type; One of the CAST_ constant definitions.
*/
public function addFieldError(
$fieldName,
$message,
$messageType = ValidationResult::TYPE_ERROR,
$code = null,
$cast = ValidationResult::CAST_TEXT,
) {
if ($code === null) {
Deprecation::notice(
'5.4.0',
'Passing $code as null is deprecated. Pass a blank string instead.',
Deprecation::SCOPE_GLOBAL
);
$code = '';
}
if ($cast === null) {
Deprecation::notice(
'5.4.0',
'Passing $cast as null is deprecated. Pass a ValidationResult::CAST_* constant instead.',
Deprecation::SCOPE_GLOBAL
);
$cast = ValidationResult::CAST_TEXT;
}
string $fieldName,
string $message,
string $messageType = ValidationResult::TYPE_ERROR,
string $code = '',
string $cast = ValidationResult::CAST_TEXT,
): static {
$this->isValid = false;
return $this->addFieldMessage($fieldName, $message, $messageType, $code, $cast);
}

/**
* Add a message to this ValidationResult without necessarily marking it as an error
*
* @param string $message The message string.
* @param string $messageType The type of message: e.g. "bad", "warning", "good", or "required". Passed as a CSS
* @param $message The message string.
* @param $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.
* @param string $code A codename for this error. Only one message per codename will be added.
* @param $code A codename for this error. Only one message per codename will be added.
* This can be usedful for ensuring no duplicate messages
* @param string|bool $cast Cast type; One of the CAST_ constant definitions.
* Bool values will be treated as plain text flag.
* @return $this
* @param $cast Cast type; One of the CAST_ constant definitions.
*/
public function addMessage(
$message,
$messageType = ValidationResult::TYPE_ERROR,
$code = null,
$cast = ValidationResult::CAST_TEXT,
) {
if ($code === null) {
Deprecation::notice(
'5.4.0',
'Passing $code as null is deprecated. Pass a blank string instead.',
Deprecation::SCOPE_GLOBAL
);
$code = '';
}
if ($cast === null) {
Deprecation::notice(
'5.4.0',
'Passing $cast as null is deprecated. Pass a ValidationResult::CAST_* constant instead.',
Deprecation::SCOPE_GLOBAL
);
$cast = ValidationResult::CAST_TEXT;
}
return $this->addFieldMessage(null, $message, $messageType, $code, $cast);
string $message,
string $messageType = ValidationResult::TYPE_ERROR,
string $code = '',
string $cast = ValidationResult::CAST_TEXT,
): static {
return $this->addFieldMessage('', $message, $messageType, $code, $cast);
}

/**
* Add a message to this ValidationResult without necessarily marking it as an error
*
* @param string $fieldName The field to link the message to. If omitted; a form-wide message is assumed.
* @param string $message The message string.
* @param string $messageType The type of message: e.g. "bad", "warning", "good", or "required". Passed as a CSS
* @param $fieldName The field to link the message to. If omitted; a form-wide message is assumed.
* @param $message The message string.
* @param $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.
* @param string $code A codename for this error. Only one message per codename will be added.
* @param $code A codename for this error. Only one message per codename will be added.
* This can be usedful for ensuring no duplicate messages
* @param string|bool $cast Cast type; One of the CAST_ constant definitions.
* Bool values will be treated as plain text flag.
* @return $this
* @param $cast Cast type; One of the CAST_ constant definitions.
*/
public function addFieldMessage(
$fieldName,
$message,
$messageType = ValidationResult::TYPE_ERROR,
$code = null,
$cast = ValidationResult::CAST_TEXT,
) {
if ($code === null) {
Deprecation::notice(
'5.4.0',
'Passing $code as null is deprecated. Pass a blank string instead.',
Deprecation::SCOPE_GLOBAL
);
$code = '';
}
if ($cast === null) {
Deprecation::notice(
'5.4.0',
'Passing $cast as null is deprecated. Pass a ValidationResult::CAST_* constant instead.',
Deprecation::SCOPE_GLOBAL
);
$cast = ValidationResult::CAST_TEXT;
}
string $fieldName,
string $message,
string $messageType = ValidationResult::TYPE_ERROR,
string $code = '',
string $cast = ValidationResult::CAST_TEXT,
): static {
if ($code && is_numeric($code)) {
throw new InvalidArgumentException("Don't use a numeric code '$code'. Use a string.");
}
if (is_bool($cast)) {
$cast = $cast ? ValidationResult::CAST_TEXT : ValidationResult::CAST_HTML;
throw new InvalidArgumentException("Don't use a numeric code '$code'. Use a non-numeric code instead.");
}
$metadata = [
'message' => $message,
Expand All @@ -229,25 +151,23 @@ public function addFieldMessage(
} else {
$this->messages[] = $metadata;
}

return $this;
}

/**
* Returns true if the result is valid.
* @return boolean
*/
public function isValid()
public function isValid(): bool
{
return $this->isValid;
}

/**
* Return the full error meta-data, suitable for combining with another ValidationResult.
*
* @return array Array of messages, where each item is an array of data for that message.
* @return Array of messages, where each item is an array of data for that message.
*/
public function getMessages()
public function getMessages(): array
{
return $this->messages;
}
Expand All @@ -257,10 +177,9 @@ public function getMessages()
* It will be valid if both this and the other result are valid.
* This object will be modified to contain the new validation information.
*
* @param ValidationResult $other the validation result object to combine
* @return $this
* @param $other the validation result object to combine
*/
public function combineAnd(ValidationResult $other)
public function combineAnd(ValidationResult $other): static
{
$this->isValid = $this->isValid && $other->isValid();
$this->messages = array_merge($this->messages, $other->getMessages());
Expand Down
4 changes: 2 additions & 2 deletions src/Forms/CheckboxField.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function dataValue()
return ($this->value) ? 1 : null;
}

public function Value()
public function getValue(): mixed
{
return ($this->value) ? 1 : 0;
}
Expand All @@ -39,7 +39,7 @@ public function getAttributes()
return array_merge(
$attributes,
[
'checked' => ($this->Value()) ? 'checked' : null,
'checked' => ($this->getValue()) ? 'checked' : null,
'type' => 'checkbox',
]
);
Expand Down
2 changes: 1 addition & 1 deletion src/Forms/CheckboxField_Readonly.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public function performReadonlyTransformation()
return clone $this;
}

public function Value()
public function getFormattedValue(): mixed
{
return $this->value ?
_t('SilverStripe\\Forms\\CheckboxField.YESANSWER', 'Yes') :
Expand Down
10 changes: 5 additions & 5 deletions src/Forms/ConfirmedPasswordField.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ public function Field($properties = [])
}

// Check if the field should be visible up front
$visible = $this->hiddenField->Value();
$visible = $this->hiddenField->getValue();
$classes = $visible
? 'showOnClickContainer'
: 'showOnClickContainer d-none';
Expand Down Expand Up @@ -418,7 +418,7 @@ public function setName($name)
public function isSaveable()
{
return !$this->showOnClick
|| ($this->showOnClick && $this->hiddenField && $this->hiddenField->Value());
|| ($this->showOnClick && $this->hiddenField && $this->hiddenField->getValue());
}

public function validate(): ValidationResult
Expand All @@ -432,10 +432,10 @@ public function validate(): ValidationResult

$this->getPasswordField()->setValue($this->value);
$this->getConfirmPasswordField()->setValue($this->confirmValue);
$value = $this->getPasswordField()->Value();
$value = $this->getPasswordField()->getValue();

// both password-fields should be the same
if ($value != $this->getConfirmPasswordField()->Value()) {
if ($value != $this->getConfirmPasswordField()->getValue()) {
$result->addFieldError(
$name,
_t('SilverStripe\\Forms\\Form.VALIDATIONPASSWORDSDONTMATCH', "Passwords don't match"),
Expand All @@ -446,7 +446,7 @@ public function validate(): ValidationResult

if (!$this->canBeEmpty) {
// both password-fields shouldn't be empty
if (!$value || !$this->getConfirmPasswordField()->Value()) {
if (!$value || !$this->getConfirmPasswordField()->getValue()) {
$result->addFieldError(
$name,
_t('SilverStripe\\Forms\\Form.VALIDATIONPASSWORDSNOTEMPTY', "Passwords can't be empty"),
Expand Down
2 changes: 1 addition & 1 deletion src/Forms/DateField.php
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ public function setValue($value, $data = null)
return $this;
}

public function Value()
public function getFormattedValue(): mixed
{
return $this->internalToFrontend($this->value);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Forms/DateField_Disabled.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function Field($properties = [])
// Render the display value with some complement of info
$displayValue = Convert::raw2xml(sprintf(
$format ?? '',
$this->Value(),
$this->getFormattedValue(),
$infoComplement
));
}
Expand Down
2 changes: 1 addition & 1 deletion src/Forms/DatetimeField.php
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ public function setValue($value, $data = null)
*
* @return string
*/
public function Value()
public function getFormattedValue(): mixed
{
return $this->internalToFrontend($this->value);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Forms/DropdownField.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ class DropdownField extends SingleSelectField
protected function getFieldOption($value, $title)
{
// Check selection
$selected = $this->isSelectedValue($value, $this->Value());
$selected = $this->isSelectedValue($value, $this->getValue());

// Check disabled
$disabled = false;
Expand Down
2 changes: 1 addition & 1 deletion src/Forms/FileField.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ public function saveInto(DataObjectInterface $record)
}
}

public function Value()
public function getValue(): mixed
{
return isset($_FILES[$this->getName()]) ? $_FILES[$this->getName()] : null;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Forms/FileUploadReceiver.php
Original file line number Diff line number Diff line change
Expand Up @@ -237,11 +237,11 @@ public function getItems()
*/
public function getItemIDs()
{
$value = $this->Value();
$value = $this->getValue();
return empty($value['Files']) ? [] : $value['Files'];
}

public function Value()
public function getValue(): mixed
{
// Re-override FileField Value to use data value
return $this->dataValue();
Expand Down
Loading
Loading