mirror of
https://github.com/lucaspalomodevelop/core.git
synced 2026-03-13 16:14:40 +00:00
mvc: improve field validation message handling (#6872)
* provide defaultValidationMessage() to inject gettext-supported string * assume $internalValidationMessage can only be set by XML now * the goal here is to translate and improve all the field validation messages
This commit is contained in:
parent
d2eb2fcc91
commit
7dcb31c024
@ -692,6 +692,24 @@ abstract class BaseField
|
||||
$this->internalValue = $this->internalDefaultValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string default validation message
|
||||
*/
|
||||
protected function defaultValidationMessage()
|
||||
{
|
||||
return gettext('Validation failed.');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string current validation message
|
||||
*/
|
||||
protected function getValidationMessage()
|
||||
{
|
||||
return $this->internalValidationMessage !== null ?
|
||||
gettext($this->internalValidationMessage) :
|
||||
$this->defaultValidationMessage();
|
||||
}
|
||||
|
||||
/**
|
||||
* set Validation message ( for usage in model xml )
|
||||
* @param string $msg validation message (on failure)
|
||||
|
||||
@ -58,21 +58,13 @@ abstract class BaseListField extends BaseField
|
||||
protected $internalMultiSelect = false;
|
||||
|
||||
/**
|
||||
* @var string default validation message string
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected $internalValidationMessage = null;
|
||||
|
||||
/**
|
||||
* @return string validation message
|
||||
*/
|
||||
protected function getValidationMessage()
|
||||
protected function defaultValidationMessage()
|
||||
{
|
||||
if ($this->internalValidationMessage == null) {
|
||||
return gettext('option not in list');
|
||||
} else {
|
||||
return $this->internalValidationMessage;
|
||||
}
|
||||
return gettext('Option not in list.');
|
||||
}
|
||||
|
||||
/**
|
||||
* select if multiple interfaces may be selected at once
|
||||
* @param $value boolean value 0/1
|
||||
@ -132,14 +124,16 @@ abstract class BaseListField extends BaseField
|
||||
{
|
||||
$validators = parent::getValidators();
|
||||
if ($this->internalValue != null) {
|
||||
$domain = array_map('strval', array_keys($this->internalOptionList));
|
||||
$this_message = $this->getValidationMessage();
|
||||
$args = [
|
||||
'domain' => array_map('strval', array_keys($this->internalOptionList)),
|
||||
'message' => $this->getValidationMessage(),
|
||||
];
|
||||
if ($this->internalMultiSelect) {
|
||||
// field may contain more than one option
|
||||
$validators[] = new CsvListValidator(array('message' => $this_message, 'domain' => $domain));
|
||||
$validators[] = new CsvListValidator($args);
|
||||
} else {
|
||||
// single option selection
|
||||
$validators[] = new InclusionIn(array('message' => $this_message, 'domain' => $domain));
|
||||
$validators[] = new InclusionIn($args);
|
||||
}
|
||||
}
|
||||
return $validators;
|
||||
|
||||
@ -56,16 +56,9 @@ class NetworkAliasField extends BaseListField
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function getValidationMessage()
|
||||
protected function defaultValidationMessage()
|
||||
{
|
||||
if ($this->internalValidationMessage == null) {
|
||||
return sprintf(
|
||||
gettext("%s is not a valid source IP address or alias."),
|
||||
(string)$this
|
||||
);
|
||||
} else {
|
||||
return $this->internalValidationMessage;
|
||||
}
|
||||
return sprintf(gettext("%s is not a valid source IP address or alias."), (string)$this);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -173,17 +173,13 @@ class PortField extends BaseListField
|
||||
}
|
||||
|
||||
/**
|
||||
* return validation message
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function getValidationMessage()
|
||||
protected function defaultValidationMessage()
|
||||
{
|
||||
if ($this->internalValidationMessage == null) {
|
||||
$msg = gettext('Please specify a valid port number (1-65535).');
|
||||
if ($this->enableWellKnown) {
|
||||
$msg .= ' ' . sprintf(gettext('A service name is also possible (%s).'), implode(', ', self::$wellknownservices));
|
||||
}
|
||||
} else {
|
||||
$msg = $this->internalValidationMessage;
|
||||
$msg = gettext('Please specify a valid port number (1-65535).');
|
||||
if ($this->enableWellKnown) {
|
||||
$msg .= ' ' . sprintf(gettext('A service name is also possible (%s).'), implode(', ', self::$wellknownservices));
|
||||
}
|
||||
return $msg;
|
||||
}
|
||||
|
||||
@ -42,11 +42,6 @@ class TextField extends BaseField
|
||||
*/
|
||||
protected $internalIsContainer = false;
|
||||
|
||||
/**
|
||||
* @var string default validation message string
|
||||
*/
|
||||
protected $internalValidationMessage = "text validation error";
|
||||
|
||||
/**
|
||||
* @var null|string validation mask (regex)
|
||||
*/
|
||||
@ -61,6 +56,14 @@ class TextField extends BaseField
|
||||
$this->internalMask = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function defaultValidationMessage()
|
||||
{
|
||||
return gettext('Text does not validate.');
|
||||
}
|
||||
|
||||
/**
|
||||
* retrieve field validators for this field type
|
||||
* @return array returns Text/regex validator
|
||||
@ -70,8 +73,10 @@ class TextField extends BaseField
|
||||
$validators = parent::getValidators();
|
||||
if ($this->internalValue != null) {
|
||||
if ($this->internalValue != null && $this->internalMask != null) {
|
||||
$validators[] = new Regex(array('message' => $this->internalValidationMessage,
|
||||
'pattern' => trim($this->internalMask)));
|
||||
$validators[] = new Regex([
|
||||
'message' => $this->getValidationMessage(),
|
||||
'pattern' => trim($this->internalMask),
|
||||
]);
|
||||
}
|
||||
}
|
||||
return $validators;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user