Following the Examples Module in Drupal 7, I'm creating a multi-step form. Page 1 are the values, with a submit to go to page 2 (a sort of confirmation screen). On page 2, I have a submit and a back button.
According to Example #8 in the Examples module, we use a submit button for the back button, with a custom #submit handler, as so:
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
'#submit' => array('my_final_submit_function'),
);
$form['back'] = array(
'#type' => 'submit',
'#value' => t('<< Back'),
'#submit' => array('my_back_button_handler_function'),
);
Everything works fine until I try to submit or hit back. Whichever button (submit or back) is placed first in the code is the button Drupal 7 registers as the clicked button, including in the form_state. Each function has their own #submit pointing to a different function. I have confirmed that Drupal can see/execute both functions.
Whichever submit button Drupal thinks was triggered is the #submit handler that gets processed.
When I put the back button first, both buttons trigger the back button functionality. When I put the submit button first, both trigger the submit logic. Although the examples module does not set a #name on each (to prevent name=op for both fields), I've tried doing that. I've tried it without that. It doesn't seem to make a difference.
I am not using an image_button, but a normal '#type'='submit'. Has anyone else encountered this issue?
You need to find out the trigger button like this
$form_state['triggering_element']['#value'] == 'Back'
That way you will sure what to do with the operation performed.
Related
Here's the situation: I have a multi-staged tunnel that basically passes a user from page to page via form submits. Fill out stage one, click submit, get sent to page two and soforth. The trouble is that my fields are creating similarly-named form elements, so when the new page loads, Cake seems to see it as the same form as the previous one. Thus, the checkboxes selected in the first form get checked in the second form, even though they are of completely different values.
TL;DR is there a way to specify with this call that there should absolutely NOT be any prefilling of the fields?
<?= $this->Form->control('estimate_options.0.option_value', [
'type' => 'select',
'multiple' => 'checkbox',
'options' => $deliverables,
'label' => false,
'value' => !empty($selectedDeliverables->option_value) ? $selectedDeliverables->option_value : false,
]);
?>
As you can see, I'm attempting to pass "false" (I've also tried NULL) as the 'value' to specify that nothing should be selected, but they get checked anyway?
I'm working on a webshop. In the categories view we load the products in with jquery because the user is able to filter them. This works great.
We then tried to save the page the user was on (in the pagination), so that if he views a product and gets back to the categorie view, he starts on the page he was previously on. For example, I'm on pagination page 3, view a product, hit the browser back button, and continue on page 3 instead on 1 (which is default cakephp behaviour). We save the page number in a session, which works great as far as we can see. Then we read that pagenumber from the session and use it in our $this->paginate as 'page' => $pagenumber. This also works.
But there is one weird problem. When you start on page 2 or 3 (read: any page bigger than 1, but we only have 3 pages at the moment), you can't get back to page 1. Switching to page 2 or 3 works great, but you can't switch to page 1. If you click on it nothing happens, it does send a network request thingy in the chrome element inspector so something is happing in the background. If we manually set the page to 'page' => 1. It works, you can switch to all 3 pages.
I hope you understand my problem, I tried to explain it as best as I can. (Sorry if my English isn't too good, I tried the best I could).
My code (I made it as short as possible without fields en joins):
In the controller
$page_number = $this->Session->read('Pagination.currentpage');
$this->paginate = array(
'conditions' => array(
'Product.status' => 'active',
'Product.visibility' => 1,
'Product.hidden_on_site' => 0,
),
'order' => 'Product.name ASC',
'limit' => 20,
'recursive' => 2,
'page' => $page_number,
);
The $page_number works, it outputs the right page. But it gives the problem that you can't get tot page 1 if you're on page 2 or 3. If we manually set 'page' => 1 it works.
I hope you have a clue or a solution that works in an other way...
The CakePHP 2.x PaginationRecall Component works very well for this purpose. I use it in several of my projects.
Author is SO member Athanassios Bakalidis.
I need to theme the login form. I need additional css classes and tabindex for the form elements.
Now I have a problem with these both for the login button.
In template.php I use 'attributes' for that. For e.g. the password field it works perfect with this code:
$form['pass']['#attributes']['class'][] = 'input-sm form-control';
$form['pass']['#attributes']['tabindex'][] = '2';
When I use this for the button - nothing is rendered in html:
$form['op']['#attributes']['class'][] = 'test-button';
$form['op']['#attributes']['tabindex'][] = '3';
'op' is the name of the login button which I can see in html output.
How can I get the 'class' and 'tabindex' to the login button?
For the button you should use $form['#submit'].
In any case you can use hook_form_alter() and devel's dpm($form) to get the form data. A generic example:
function MYTHEME_form_alter(&$form, &$form_state, $form_id) {
dpm($form);
}
The submit button may be accessed like this for a common Drupal theme (structure taken from the dpm() function):
$form['actions']['submit']['#attributes']['tabindex'][] = "2";
One important thing here is to be careful because you may override the form again later on your theme... In this case look for the very last hook and apply changes there.
I use https://github.com/CakeDC/TinyMCE to download plugin and did follow all the step to integrate on my cakePHP project. so right now, all the textarea was successfully changed to tinyMCE editor
But when click "SUBMIT" to submit my form, page cannot submit and post data. Without loading editor my form can submit and post data.
Is any jquery problem ? please advise me.
thank you.
Bootsrab.php
CakePlugin::load('TinyMCE');
Configure::write('TinyMCE.editorOptions', array('width' => '500px','height'=>'250px' ));
Controller:
public $helpers=array('Html','Form','TinyMCE.TinyMCE');
View:
$this->TinyMCE->editor(array('theme' => 'advanced', 'mode' => 'textareas'));
echo $this->Form->input('user_requirements',array('required'=>true) );
Layout : default
loding js file:
echo $this->Html->script(array('ddsmoothmenu','jquery-1.7.1.min','jquery-ui-1.8.17.custom.min'));
You've set the field to be required, so the problem you are experiencing is probably the browser based form validation.
The problem is that the validation applies before TinyMCE injects the contents into the textarea, and so the validation will always fail as the textarea is empty. This is a very long known "bug" btw:
http://www.tinymce.com/develop/bugtracker_view.php?id=4768
http://www.tinymce.com/develop/bugtracker_view.php?id=5671
In Firefox you might notice a validation bubble that appears "behind" the browser in the bottom left corner of the screen, and in Chrome for example it would throw the following error: "An invalid form control with name='...' is not focusable".
The quick and dirty fix would be to set required to false. In order to keep the required class on the generated container div you would have to set this manually using the div option:
'div' => array('class' => 'input text required')
It's also possible to disable browser validation completely by defining the novalidate attribute on the form:
$this->Form->create('ModelName', array('novalidate' => true));
or using the formnovalidate attribute on the submit button:
$this->Form->submit('Submit', array('formnovalidate' => true));
Theoretically it would also be possible to listen to the invalid event and display custom validation bubbles, but the problem here is that the browser behavior is not consistent, ie in Chrome it's not possible to validate invisible (using display or visibility) fields. Also the content would still be missing in the textarea field.
What seems to work is using opacity to hide the field, that way one could position the textarea under the editor, and the validation bubble would be displayed correctly. However that would also require to inject the editor contents in the textarea manually when pressing Enter and when clicking the submit button (or probably even simpler using proper editor change events). I'll see if I can come up with an example for this later on.
Update: I've implemented a fix/workaround in form of a TinyMCE 4.x plugin, as this was bugging me in some of my own applications too, see https://github.com/ndm2/tinymce-validatable
I'd like to create well formatted Form element with Form helper but without creation of form itself. So i wrote:
$this->Form->input(
'Kid.id',
array(
'type' => 'text',
)
);
This is ajax inside "Kids" controller response, and I'd like to update form after user make some actions.
I have no idea how to do it, except manually write HTML code.
Shame on me. This works I just forgot to add an echo at the beginning of line.