CakePHP 2.x User Management Premium Plugin Version 2.4.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() {
parent::beforeFilter();
if(isset($this->Security) && $this->request->is('ajax')) {
$this->Security->csrfCheck = false;
$this->Security->validatePost = false;
}
}
2. Now in controller's function ajax handling for e.g. in addUser function of users controller
if($this->request->is('post')) {
$this->User->set($this->request->data);
$addValidate = $this->User->addUserValidate();
if($this->request->is('ajax')) {
if($addValidate) {
$response = ['error'=>0, 'message'=>'success'];
} else {
$response = ['error'=>1, 'message'=>'failure'];
$response['data']['Users'] = $this->User->validationErrors;
}
echo json_encode($response);exit;
} else {
if($addValidate) {
//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/app/Plugin/Usermgmt/View/Elements/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('User', ['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->input('User.username', ['type'=>'text', 'label'=>false, 'class'=>'form-control']);?>
<?php echo $this->Form->input('UserDetail.cellphone', ['type'=>'text', 'label'=>false, 'class'=>'form-control']);?>
where
'User.username' username is a field of users table
'UserDetail.cellphone' cellphone is a field of user_details table associated with users table
For more information see
Users Controller yourapp/app/Plugin/Usermgmt/Controller/UsersController.php
Add User Template yourapp/app/Plugin/Usermgmt/View/Users/add_user.php
Ajax Validation Element yourapp/app/Plugin/Usermgmt/View/Elements/ajax_validation.php
Ajax Validation Js yourapp/app/Plugin/Usermgmt/webroot/js/ajax.validation.js