I have a use case where I will be having buttons for connecting with YouTube and Twitter within my signup form. Now, on clicking on either of these buttons, the users will be redirected to the respective social platform's OAuth authorization pages, the logic of which is written in separate functions in other custom modules. For example, on clicking the 'Connect with YouTube' button on the signup form, I want a custom function to be called. This is how I have currently represented this form button:
$form['signup_connect_with_youtube_submit'] = array(
'#type' => 'button',
'#attributes' => array('onclick' => 'return youtube_form_submit();'),
'#value' => 'Connect with Youtube',
);
where youtube_form_submit() is the function that I want to call when the user clicks this button. However, on clicking this button, the entire form gets submitted instead of just that function being called even though I have a separate submit button for the form.
How can I implement this use case in my module? Basically, I want to include multiple buttons in my form apart from the main submit button, clicking on which will not submit the form but will instead call separate functions.
Comments
$form['submit_button_1'] =
Note that by adding submit handlers (functions) to a button, the default _submit() function will not be called when that button is clicked. Only the added submit handler(s) will be called.
Contact me to contract me for D7 -> D10/11 migrations.
Thanks, this worked just fine
Thanks, this worked just fine.
#valid must be different
Worth noting if both submit buttons have the same #value this problem will persist. Ensure they have different ID's and different values.
Really useful the tip!
Really useful the tip!
I wish I saw your comments 2
I wish I saw your comment 2 days ago. I spent 2 days trying to find the bug in my code...
Thanks. Worked exactly as
Thanks. Worked exactly as required and saved time too.
form action and javascript submit
if i have a drupal submit handler and if i add a form action and javascript on submit attribute then my drupal submit handler is not being call what to do in this case . kindly please help me on this.
That's not really relevant to
That's not really relevant to this topic, so you should open a new thread for this issue.
Contact me to contract me for D7 -> D10/11 migrations.
Submit handler update
The submit function should be like below:
$form['reset'] = [
'#type' => 'submit',
'#value' => $this->t('Reset'),
'#submit' => [[$this, 'submitPass']],
];
And function should be like below:
public function submitPass(array &$form, FormStateInterface $form_state) { }
Also worth noting, you will
Also worth noting, you will need to use a different "#value" for each button or the last handler function will be used regardless of the button clicked.
how to validate multiple
how to validate multiple submit form?