info@ektanjali.com +91 99276-99286

CakePHP 3.x User Management Premium Plugin Version 3.3.x

Ajax Form Validations

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->getRequest()->is('ajax')) {
        $this->Security->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->newEntity($this->getRequest()->getData(), ['validate'=>'forAddUser', 'associated'=>['UserDetails'=>['validate'=>'forAddUser']]]);
if($this->getRequest()->is('post')) {
    $errors = $userEntity->errors();
    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 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->control('Users.username', ['type'=>'text', 'label'=>false, 'div'=>false, 'class'=>'form-control']);?>
<?php echo $this->Form->control('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