mirror of
https://github.com/silverstripe/silverstripe-spamprotection.git
synced 2024-10-22 14:05:59 +02:00
updating validateField method on EditableSpamProtectionField to use correct error message
This commit is contained in:
parent
8d1cc403b9
commit
529960d485
@ -110,11 +110,46 @@ if (class_exists('EditableFormField')) {
|
||||
return $fields;
|
||||
}
|
||||
|
||||
public function validateField($data, $form)
|
||||
/**
|
||||
* Using custom validateField method
|
||||
* as Spam Protection Field implementations may have their own error messages
|
||||
* and may not be based on the field being required, e.g. Honeypot Field
|
||||
*
|
||||
* @param array $data
|
||||
* @param Form $form
|
||||
* @return void
|
||||
*/
|
||||
public function validateField($data, $form)
|
||||
{
|
||||
$formField = $this->getFormField();
|
||||
if (!$formField->validate($form->getValidator())) {
|
||||
$form->addErrorMessage($this->Name, $this->getErrorMessage()->HTML(), 'error', false);
|
||||
|
||||
if (isset($data[$this->Name])) {
|
||||
$formField->setValue($data[$this->Name]);
|
||||
}
|
||||
|
||||
$validator = $form->getValidator();
|
||||
if (!$formField->validate($validator)) {
|
||||
$errors = $validator->getErrors();
|
||||
$foundError = false;
|
||||
|
||||
// field validate implementation may not add error to validator
|
||||
if (count($errors) > 0) {
|
||||
// check if error already added from fields' validate method
|
||||
foreach ($errors as $error) {
|
||||
if ($error['fieldName'] == $this->Name) {
|
||||
$foundError = $error;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ($foundError !== false) {
|
||||
// use error messaging already set from validate method
|
||||
$form->addErrorMessage($this->Name, $foundError['message'], $foundError['messageType'], false);
|
||||
} else {
|
||||
// fallback to custom message set in CMS or default message if none set
|
||||
$form->addErrorMessage($this->Name, $this->getErrorMessage()->HTML(), 'error', false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user