diff --git a/src/Controller/AppController.php b/src/Controller/AppController.php index 07ec3aa..dacb95f 100644 --- a/src/Controller/AppController.php +++ b/src/Controller/AppController.php @@ -110,6 +110,9 @@ class AppController extends Controller $this->ACL->checkAccess(); $this->set('menu', $this->ACL->getMenu()); $this->set('ajax', $this->request->is('ajax')); + if (!empty($this->request->getHeader('X-Request-Html-On-Failure'))) { + $this->ajax_with_html_on_failure = true; + } $this->request->getParam('prefix'); $this->set('darkMode', !empty(Configure::read('Cerebrate.dark'))); } diff --git a/src/Controller/Component/CRUDComponent.php b/src/Controller/Component/CRUDComponent.php index 20755c4..3ec060e 100644 --- a/src/Controller/Component/CRUDComponent.php +++ b/src/Controller/Component/CRUDComponent.php @@ -81,13 +81,16 @@ class CRUDComponent extends Component $patchEntityParams['fields'] = $params['fields']; } $data = $this->Table->patchEntity($data, $input, $patchEntityParams); - if ($this->Table->save($data)) { + $savedData = $this->Table->save($data); + if ($savedData !== false) { $message = __('{0} added.', $this->ObjectAlias); if (!empty($input['metaFields'])) { $this->saveMetaFields($data->id, $input); } if ($this->Controller->ParamHandler->isRest()) { $this->Controller->restResponsePayload = $this->RestResponse->viewData($data, 'json'); + } else if ($this->Controller->ParamHandler->isAjax()) { + $this->Controller->ajaxResponsePayload = $this->Controller->RestResponse->ajaxSuccessResponse($this->ObjectAlias, 'add', $savedData, $message); } else { $this->Controller->Flash->success($message); if (!empty($params['displayOnSuccess'])) { @@ -103,6 +106,7 @@ class CRUDComponent extends Component } } } else { + $this->Controller->isFailResponse = true; $validationMessage = $this->prepareValidationError($data); $message = __( '{0} could not be added.{1}', @@ -110,7 +114,8 @@ class CRUDComponent extends Component empty($validationMessage) ? '' : ' ' . __('Reason:{0}', $validationMessage) ); if ($this->Controller->ParamHandler->isRest()) { - + } else if ($this->Controller->ParamHandler->isAjax()) { + $this->Controller->ajaxResponsePayload = $this->Controller->RestResponse->ajaxFailResponse($this->ObjectAlias, 'add', $data, $message, $validationMessage); } else { $this->Controller->Flash->error($message); } diff --git a/src/Controller/UsersController.php b/src/Controller/UsersController.php index d1bbcbd..7d39103 100644 --- a/src/Controller/UsersController.php +++ b/src/Controller/UsersController.php @@ -25,6 +25,10 @@ class UsersController extends AppController $this->CRUD->add(); if ($this->ParamHandler->isRest()) { return $this->restResponsePayload; + } else if ($this->ParamHandler->isAjax() && $this->request->is(['post', 'put'])) { + if (empty($this->isFailResponse) || empty($this->ajax_with_html_on_failure)) { + return $this->ajaxResponsePayload; + } } $dropdownData = [ 'role' => $this->Users->Roles->find('list', [ diff --git a/templates/element/genericElements/Form/genericForm.php b/templates/element/genericElements/Form/genericForm.php index 2217ca7..6e642ff 100644 --- a/templates/element/genericElements/Form/genericForm.php +++ b/templates/element/genericElements/Form/genericForm.php @@ -12,6 +12,7 @@ - use these to define dynamic form fields, or anything that will feed into the regular fields via JS population * - submit: The submit button itself. By default it will simply submit to the form as defined via the 'model' field */ + $this->Form->setConfig('errorClass', 'is-invalid'); $modelForForm = empty($data['model']) ? h(\Cake\Utility\Inflector::singularize(\Cake\Utility\Inflector::classify($this->request->getParam('controller')))) : h($data['model']); @@ -35,11 +36,14 @@ 'select' => '', 'checkbox' => '', 'checkboxFormGroup' => '{{label}}', - 'formGroup' => '