chg: [CRUD] Improved validation message feedback

pull/67/head
mokaddem 2021-06-30 12:18:58 +02:00
parent 186fba395a
commit 25ca89a701
2 changed files with 30 additions and 11 deletions

View File

@ -165,7 +165,8 @@ class CRUDComponent extends Component
} }
} else { } else {
$this->Controller->isFailResponse = true; $this->Controller->isFailResponse = true;
$validationMessage = $this->prepareValidationError($data); $validationErrors = $data->getErrors();
$validationMessage = $this->prepareValidationMessage($validationErrors);
$message = __( $message = __(
'{0} could not be added.{1}', '{0} could not be added.{1}',
$this->ObjectAlias, $this->ObjectAlias,
@ -174,7 +175,7 @@ class CRUDComponent extends Component
if ($this->Controller->ParamHandler->isRest()) { if ($this->Controller->ParamHandler->isRest()) {
$this->Controller->restResponsePayload = $this->RestResponse->viewData($message, 'json'); $this->Controller->restResponsePayload = $this->RestResponse->viewData($message, 'json');
} else if ($this->Controller->ParamHandler->isAjax()) { } else if ($this->Controller->ParamHandler->isAjax()) {
$this->Controller->ajaxResponsePayload = $this->RestResponse->ajaxFailResponse($this->ObjectAlias, 'add', $data, $message, $validationMessage); $this->Controller->ajaxResponsePayload = $this->RestResponse->ajaxFailResponse($this->ObjectAlias, 'add', $data, $message, $validationErrors);
} else { } else {
$this->Controller->Flash->error($message); $this->Controller->Flash->error($message);
} }
@ -183,6 +184,21 @@ class CRUDComponent extends Component
$this->Controller->set('entity', $data); $this->Controller->set('entity', $data);
} }
private function prepareValidationMessage($errors)
{
$validationMessage = '';
if (!empty($errors)) {
if (count($errors) == 1) {
$field = array_keys($errors)[0];
$fieldError = implode(', ', array_values($errors[$field]));
$validationMessage = __('{0}: {1}', $field, $fieldError);
} else {
$validationMessage = __('There has been validation issues with multiple fields');
}
}
return $validationMessage;
}
private function prepareValidationError($data) private function prepareValidationError($data)
{ {
$validationMessage = ''; $validationMessage = '';
@ -192,7 +208,7 @@ class CRUDComponent extends Component
foreach ($errorData as $key => $value) { foreach ($errorData as $key => $value) {
$errorMessages[] = $value; $errorMessages[] = $value;
} }
$validationMessage .= __(' {1}', $field, implode(',', $errorMessages)); $validationMessage .= __('{0}: {1}', $field, implode(',', $errorMessages));
} }
} }
return $validationMessage; return $validationMessage;
@ -261,14 +277,15 @@ class CRUDComponent extends Component
} }
} }
} else { } else {
$validationMessage = $this->prepareValidationError($data); $validationErrors = $data->getErrors();
$validationMessage = $this->prepareValidationMessage($validationErrors);
$message = __( $message = __(
__('{0} could not be modified.'), __('{0} could not be modified.'),
$this->ObjectAlias $this->ObjectAlias
); );
if ($this->Controller->ParamHandler->isRest()) { if ($this->Controller->ParamHandler->isRest()) {
} else if ($this->Controller->ParamHandler->isAjax()) { } else if ($this->Controller->ParamHandler->isAjax()) {
$this->Controller->ajaxResponsePayload = $this->RestResponse->ajaxFailResponse($this->ObjectAlias, 'edit', $data, $message, $data->getErrors()); $this->Controller->ajaxResponsePayload = $this->RestResponse->ajaxFailResponse($this->ObjectAlias, 'edit', $data, $message, $validationErrors);
} else { } else {
$this->Controller->Flash->error($message); $this->Controller->Flash->error($message);
} }
@ -699,7 +716,8 @@ class CRUDComponent extends Component
} }
} }
} else { } else {
$validationMessage = $this->prepareValidationError($data); $validationErrors = $data->getErrors();
$validationMessage = $this->prepareValidationMessage($validationErrors);
$message = __( $message = __(
'{0} could not be modified.{1}', '{0} could not be modified.{1}',
$this->ObjectAlias, $this->ObjectAlias,
@ -707,7 +725,7 @@ class CRUDComponent extends Component
); );
if ($this->Controller->ParamHandler->isRest()) { if ($this->Controller->ParamHandler->isRest()) {
} else if ($this->Controller->ParamHandler->isAjax()) { } else if ($this->Controller->ParamHandler->isAjax()) {
$this->Controller->ajaxResponsePayload = $this->RestResponse->ajaxFailResponse($this->ObjectAlias, 'toggle', $message, $validationMessage); $this->Controller->ajaxResponsePayload = $this->RestResponse->ajaxFailResponse($this->ObjectAlias, 'toggle', $message, $validationErrors);
} else { } else {
$this->Controller->Flash->error($message); $this->Controller->Flash->error($message);
if (empty($params['redirect'])) { if (empty($params['redirect'])) {

View File

@ -996,21 +996,22 @@ class FormValidationHelper {
injectValidationErrorInForm(fieldName, errors) { injectValidationErrorInForm(fieldName, errors) {
const inputField = Array.from(this.form).find(node => { return node.name == fieldName }) const inputField = Array.from(this.form).find(node => { return node.name == fieldName })
if (inputField !== undefined) { if (inputField !== undefined) {
const $messageNode = this.buildValidationMessageNode(errors) const $messageNode = this.buildValidationMessageNode(fieldName, errors)
const $inputField = $(inputField) const $inputField = $(inputField)
$inputField.addClass('is-invalid') $inputField.addClass('is-invalid')
$messageNode.insertAfter($inputField) $messageNode.insertAfter($inputField)
} else { } else {
const $messageNode = this.buildValidationMessageNode(errors, true) const $messageNode = this.buildValidationMessageNode(fieldName, errors, true)
const $flashContainer = $(this.form).parent().find('#flashContainer') const $flashContainer = $(this.form).parent().find('#flashContainer')
$messageNode.insertAfter($flashContainer) $messageNode.insertAfter($flashContainer)
} }
} }
buildValidationMessageNode(errors, isAlert=false) { buildValidationMessageNode(fieldName, errors, isAlert=false) {
const $messageNode = $('<div></div>') const $messageNode = $('<div></div>')
if (isAlert) { if (isAlert) {
$messageNode.addClass('alert alert-danger').attr('role', 'alert') $messageNode.addClass('alert alert-danger').attr('role', 'alert')
$messageNode.append($('<strong></strong>').text(`${fieldName}: `))
} else { } else {
$messageNode.addClass('invalid-feedback') $messageNode.addClass('invalid-feedback')
} }
@ -1020,7 +1021,7 @@ class FormValidationHelper {
if (hasMultipleErrors) { if (hasMultipleErrors) {
$messageNode.append($('<li></li>').text(error)) $messageNode.append($('<li></li>').text(error))
} else { } else {
$messageNode.text(error) $messageNode.append($('<span></span>').text(error))
} }
} }
} else { } else {