CakePHP 4.x User Management Premium Plugin Version 4.1.x
How to use ajax validations in plugin and outside plugin
1. If you are using FormProtection component then your controller's before filter should look like -
public function beforeFilter(EventInterface $event) {
parent::beforeFilter($event);
if(isset($this->FormProtection) && $this->getRequest()->is('ajax')) {
$this->FormProtection->setConfig('unlockedActions', [$this->getRequest()->getParam('action')]);
}
}
2. Now in controller's function ajax handling for e.g. in addUser function of users controller
$userEntity = $this->Users->newEmptyEntity();
if($this->getRequest()->is('post')) {
$userEntity = $this->Users->patchEntity($userEntity, $this->getRequest()->getData(), ['validate'=>'forAddUser', 'associated'=>['UserDetails'=>['validate'=>'forAddUser']]]);
$errors = $userEntity->getErrors();
if($this->getRequest()->is('ajax')) {
if(empty($errors)) {
$response = ['error'=>0, 'message'=>'success'];
} else {
$response = ['error'=>1, 'message'=>'failure'];
$response['data']['Users'] = $errors;
}
echo json_encode($response);exit;
} else {
if(empty($errors)) {
//rest code here
}
}
}
3. Include ajax pagination js file in template file (ignore if you already added in layout)
<?php echo $this->Html->script('/usermgmt/js/ajax.validation.js'); ?>
4. Now in template file you should add ajax_validation element in following way for e.g. in add_user.php
<?php echo $this->element('Usermgmt.ajax_validation', ['formId'=>'addUserForm', 'submitButtonId'=>'addUserSubmitBtn']); ?>
where
'Usermgmt.ajax_validation' is a ajax validation element, location is yourapp/plugins/Usermgmt/templates/element/ajax_validation.php.
'formId' is your form id where you want to add ajax validations.
'submitButtonId' is your form submit button id. (use either submitButtonId or submitButtonClass)
'submitButtonClass' is your form submit button class. (use either submitButtonId or submitButtonClass)
5. Now in template file don't forget to add form id and submit button id
<?php echo $this->Form->create($userEntity, ['id'=>'addUserForm']);?>
<?php echo $this->Form->Submit(__('Add User'), ['class'=>'btn btn-primary', 'id'=>'addUserSubmitBtn']);?>
OR
<?php echo $this->Form->Submit(__('Add User'), ['class'=>'btn btn-primary', 'class'=>'addUserSubmitBtn']);?>
6. Please note: your form input fields must have model names with field names for e.g.
<?php echo $this->Form->control('Users.username', ['type'=>'text', 'label'=>false, 'class'=>'form-control']);?>
<?php echo $this->Form->control('Users.user_detail.cellphone', ['type'=>'text', 'label'=>false, 'class'=>'form-control']);?>
where
'Users.username' username is a field of users table
'Users.user_detail.cellphone' cellphone is a field of user_details table associated with users table
make sure to use correct model names with valid Singular and Plural Nouns.
For more information see
Users Controller yourapp/plugins/Usermgmt/src/Controller/UsersController.php
Add User Template yourapp/plugins/Usermgmt/templates/Users/add_user.php
Ajax Validation Element yourapp/plugins/Usermgmt/templates/element/ajax_validation.php
Ajax Validation Js yourapp/plugins/Usermgmt/webroot/js/ajax.validation.js