I want to add 2 callback to Js->submit, My first return true or false. Continuing with that I want to put action. I mean If it is true then it continue to action else halt at that moment.
What I have tried yet but its not taking 2 callback. Plaese help.
echo $this->Js->submit("Apply",
array(
'id' => 'submitForm', 'div' => false, 'class' => 'general_button', 'style' => array('float:none;', 'margin: 10px;'),
'url' => array(
'controller' => 'categories', 'action' => 'index', 'field' => $search_term, 'value' => $search_value, 'parentId' => $parentId
),
'update' => '#listID',
'confirm' => 'Are you sure you want to apply action to selected records ??',
'before' => $this->Js->event('click', 'return checkboxCount();', false),
'before' => $this->Js->get('#loaderID')->effect('fadeIn', array('buffer' => false)),
'complete' => $this->Js->get('#loaderID')->effect('fadeOut', array('buffer' => false)),
)
);
How will I continue with it. As function return checkboxCount(); returns true or false. but in here case my function is not even called.
Related
I am using cakephp3 and select2 dropdown script.
My call to the data looks like this
$roles = $this->ParticipantsProjectsRoles->Roles->find('list', [
'keyField' => 'id',
'valueField' => 'description'
]);
within my view I call this
<?=$this->Form->input('role_id', ['options' => $roles, 'label' => false, 'class' => 'form-control select2me']);?>
The output HTML will load always the first data entry into the select.
Is there a way to have the first value always empty?
Set the empty key in the options array to true or another value, e.g. Select Role:
$this->Form->input(
'role_id', [
'options' => $roles,
'label' => false,
'class' => 'form-control select2me',
'empty' => true
]
);
How I want it to work:
I have list of buttons(type button) that trigger an Ajax call that add the that item to a list. This works as exactly as I want it except for the button looks ugly .
The problem:
When I try to replace the "button" with an "image button" the form is submitted, which is not what I want. Is there a way to add an image to a button that do not trigger a submit? Can I disable the submit for "image button"? Or shall I add the image using css on the button?
What is the difference between "button", "image button" and "submit"?
To make the type image_button act like an button is looked in system.module and found "'#executes_submit_callback' => TRUE", so change this to false will prevent submit function to be called on your image_button.
Code from system.module:
$types['button'] = array(
'#input' => TRUE,
'#name' => 'op',
'#button_type' => 'submit',
'#executes_submit_callback' => FALSE,
'#limit_validation_errors' => FALSE,
'#process' => array('ajax_process_form'),
'#theme_wrappers' => array('button'),
);
$types['image_button'] = array(
'#input' => TRUE,
'#button_type' => 'submit',
'#executes_submit_callback' => TRUE, // <--- This is why submit is triggered
'#limit_validation_errors' => FALSE,
'#process' => array('ajax_process_form'),
'#return_value' => TRUE,
'#has_garbage_value' => TRUE,
'#src' => NULL,
'#theme_wrappers' => array('image_button'),
);
Another thing that you might encounter is the validation error, to remove it just add the "'#validate' => array()" or if you want to run validation but ignore errors use "#limit_validation_errors' => array()". This also apply for button.
Here is an example where you can experiment the above things and see when the validation and submit callback is triggered. Also a checkbox is included to show when validation errors occurs.
function button_menu()
{
$items = array();
$items['button'] = array(
'title' => 'Button',
'page callback' => 'drupal_get_form',
'page arguments' => array('button_form'),
'access callback' => array(TRUE),
'type' => MENU_CALLBACK,
);
return $items;
}
function button_form($form, &$form_state)
{
// Add to prove the point of how validation is working
$form['checkbox'] = array(
'#type' => 'checkbox',
'#title' => t('checkbox'),
'#default_value' => FALSE,
);
$form['button'] = array(
'#id' => 'button_1',
'#type' => 'button',
'#name' => 'test1',
'#value' => 'test1',
//'#validate' => array(), // This line will remove validation completely
'#limit_validation_errors' => array(), // This line will run validation but ignore any errors
'#ajax' => array(
'callback' => 'button_test_callback',
'wrapper' => 'wrapper',
'method' => 'replace',
'effect' => 'fade',
),
);
$form['image_button'] = array(
'#id' => 'image_button_1',
'#type' => 'image_button',
'#src' => '/themes/bartik/logo.png', // hope you still have bartik theme
'#executes_submit_callback' => FALSE, // This line removes the submit callback
//'#validate' => array(), // This line will remove validation completely
'#limit_validation_errors' => array(), // This line will run validation but ignore any errors
'#ajax' => array(
'callback' => 'button_test_callback',
'wrapper' => 'wrapper',
'method' => 'replace',
'effect' => 'fade',
),
);
// Just some code to show what button was pressed
if (array_key_exists('triggering_element', $form_state) &&
($form_state['triggering_element']['#id'] == 'button_1' || $form_state['triggering_element']['#id'] == 'image_button_1'))
{
$form['markup'] = array(
'#type' => 'markup',
'#markup' => '<div id="wrapper"><p>'. $form_state['triggering_element']['#id'] .'</p></div>',
);
}
else {
$form['markup'] = array(
'#type' => 'markup',
'#markup' => '<div id="wrapper"><p>nothing</p></div>',
);
}
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Submit',
);
return $form;
}
function button_test_callback($form, $form_state)
{
return $form['markup'];
}
function button_form_validate($form, &$form_state)
{
// To display when validation is triggered
drupal_set_message('button_form_validate');
// Only when submit button is pressed we care about this error.
if ( $form_state['values']['checkbox'] == 0) {
form_set_error('checkbox', 'checkbox not checked');
}
}
function button_form_submit($form, &$form_state)
{
// To display when submit is triggered
drupal_set_message('button_form_submit');
}
Here's the component declaration in AppController.php>
'Auth' => array(
'authenticate' => array(
'Form' => array(
'userModel' => 'User',
'fields' => array('username' => 'email', 'password' => 'code'),
'scope' => array('activated' => true),
),
),
'loginAction' => array('controller' => 'users', 'action' => 'login'),
'loginRedirect' => array('controller' => 'members', 'action' => 'dashboard', 'admin' => true),
'authError' => 'No Permission',
'logoutRedirect' => array('controller' => 'pages', 'action' => 'home'),
'userScope' => array('User.activated' => true),
),
The login form:
<?= $this->Form->create('User', array('url' => '/users/login', 'class' => 'form-inline'));?>
<div class="form-group">
<?= $this->Form->input('User.email', array(
'div' => false,
'label' => false,
'placeholder' => 'е-пошта',
'class' => 'form-control',
'required' => true,
));?>
<?= $this->Form->input('User.code', array(
'div' => false,
'label' => false,
'placeholder' => 'сериски број',
'class' => 'form-control',
'required' => true,
));?>
<?= $this->Form->button('<i class="fa fa-user"></i>', array('type' => 'submit', 'class' => 'btn btn-primary', 'escape' => false));?>
</div>
<?= $this->Form->end();?>
And a snippet of the login function:
// ...
if($this->request->is('post')) {
if($this->Auth->login()) {
if(isset($this->request->data['User']['token']) && $this->request->data['User']['token']) {
$token = substr(md5(time()), 0, 32);
$this->User->id = $this->Auth->user('id');
$this->User->saveField('token', $token);
$this->Cookie->write('remember_me', $token, false, '1 week');
}
return $this->redirect($this->Auth->loginRedirect);
}
// ...
Now, when I use $this->Auth->login($this->request->data) or $this->Auth->login($this->request->data['User']), it works, but when I use only $this->Auth->login() it doesn't. I can do a workaround by logging in with $this->request->data and then putting the rest of the user data manually to be available afterwards, but I want to know why this happens. Any ideas?
EDIT
So, as Karthik Keyan mentioned hashing, i figured this was the problem. CakePHP was automatically hashing the password (code field) and I didn't want it to. So I made a custom hasher class named NoPasswordHasher as follows:
App::uses('AbstractPasswordHasher', 'Controller/Component/Auth');
class NoPasswordHasher extends AbstractPasswordHasher {
public function hash($password) {
return $password;
}
public function check($password, $hashedPassword) {
return $password == $hashedPassword;
}
}
and used it in the Auth component:
'Auth' => array(
'authenticate' => array(
'Form' => array(
'userModel' => 'User',
'fields' => array('username' => 'email', 'password' => 'code'),
'scope' => array('activated' => true),
'passwordHasher' => 'No',
),
),
It works now. Thank you.
Tell what type of errors can be display for you.Please check can you store the password in HASH (salt) format.
I add the checkbox functionality from the yii-booster. But the widget renders the model view without the needed boxes. What's wrong?
Widjet code in view
<?php
$this->widget('bootstrap.widgets.TbExtendedGridView',array(
'id'=>'docs-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'type'=>'bordered condensed',
'template' => "{items}",
'bulkActions' => array(
'actionButtons' => array(
array(
'buttonType' => 'button',
'type' => 'primary',
'size' => 'small',
'label' => 'Choose',
'click' => 'js:function(values){console.log(values);}'
)
),
// if grid doesn't have a checkbox column type, it will attach
// one and this configuration will be part of it
'checkBoxColumnConfig' => array(
'name' => 'id'
),
),
));
If you are using bulkActions, you have to use 'columns' to list out the columns you want to display instead of using 'template'.
'columns' => array(
'id',
'title',
...
),
the problem has been in the wrong hierarchy from the example:
The 'checkBoxColumnConfig' attribute must be outside of the 'actionButtons' attribute:
'bulkActions' => array(
'actionButtons' => array(
/*array(
'buttonType' => 'button',
'type' => 'primary',
'size' => 'small',
'label' => 'Выбрать отмеченные',
'click' => 'js:function(values){console.log(values);}'
)
),*/
// if grid doesn't have a checkbox column type, it will attach
// one and this configuration will be part of it
),
'checkBoxColumnConfig' => array(
'name' => 'id'
),
...
));
but now the widget doesn't work when i uncomment the array part inside 'actionButtons':
array(
'buttonType' => 'button',
'type' => 'primary',
'size' => 'small',
'label' => 'Выбрать отмеченные',
'click' => 'js:function(values){console.log(values);}'
)
what might be a cause?
I am testing to load taxonomy/term/1 page into a wrapper 'ajax-content', but can not figure out if this is the right thing:
drupal_add_library('system', 'drupal.ajax');
module_load_include('inc', 'taxonomy', 'taxonomy.pages');
$link = array(
'#type' => 'link',
'#title' => t('Some Taxonomy'),
'#href' => 'taxonomy/term/1', //'taxonomy/term/1/nojs',
'#attributes' => array('class' => array('use-ajax')),
'#ajax' => array(
'callback' => 'taxonomy_term_page',
'wrapper' => 'ajax-content',
'method' => 'replace',
'effect' => 'fade',
),
);
print "<div id='ajax-content'></div>" . drupal_render($link);
This always returns a 200 error.
Any hint would be very much appreciated.
Thanks