CakePHP 3.x User Management Premium Plugin Version 3.1.x
How to use ajax validations in plugin and outside plugin
1. If you are using Security component then your controller's before filter should look like -
public function beforeFilter(Event $event) {
parent::beforeFilter($event);
if(isset($this->Security) && $this->request->is('ajax')) {
$this->Security->config('unlockedActions', [$this->request['action']]);
}
}
2. Now in controller's function ajax handling for e.g. in addUser function of users controller
$userEntity = $this->Users->newEntity($this->request->data, ['validate'=>'forAddUser', 'associated'=>['UserDetails'=>['validate'=>'forAddUser']]]);
if($this->request->is('post')) {
$errors = $userEntity->errors();
if($this->request->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 in Template file (ignore if you already added in layout)
<?php echo $this->Html->script('/usermgmt/js/ajaxValidation'); ?>
4. Now in Template file you should add ajax_validation element in following way for e.g. in add_user.ctp
<?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/src/Template/Element/ajax_validation.ctp.
'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', 'class'=>'form-horizontal', 'type'=>'file']);?>
<?php echo $this->Form->Submit(__('Add User'), ['div'=>false, 'class'=>'btn btn-primary', 'id'=>'addUserSubmitBtn']);?>
OR
<?php echo $this->Form->Submit(__('Add User'), ['div'=>false, '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->input('Users.username', ['type'=>'text', 'label'=>false, 'div'=>false, 'class'=>'form-control']);?>
<?php echo $this->Form->input('Users.user_detail.cellphone', ['type'=>'text', 'label'=>false, 'div'=>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 user 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/src/Template/Users/add_user.ctp
Ajax Validation Element yourapp/plugins/Usermgmt/src/Template/Element/ajax_validation.ctp
Ajax Validation Js yourapp/plugins/Usermgmt/webroot/js/ajaxValidation.js