I have a button element in form builder as follows

$form['fieldset']['add_new_event_btn'] = array(
  '#value' => t('Add new event trigger'),
  '#type' => 'button',
  '#attributes' => array(
    'style' => 'margin-top:20px;width:200px;margin-right:275px;float:right',
  ),
  '#ajax' => array(
    // Function to call when event on form element triggered.
    'callback' => '::enable_event_trigger',
    // Effect when replacing content. Options: 'none' (default), 'slide', 'fade'.
    'effect' => 'fade',
    // Javascript event to trigger Ajax. Currently for: 'onchange'.
    'event' => 'click',
    'progress' => array(
      // Graphic shown to indicate ajax. Options: 'throbber' (default), 'bar'.
      'type' => 'throbber',
      // Message to show along progress graphic. Default: 'Please wait...'.
      'message' => 'loading',
    ),
  ),
);

But this button is act as the submit button , and there for the validation for other elements works and shows the error message on the next reload when i leave the page as it is, How can i rectify this problem ??

bumped to the homepage by Community 13 hours ago

This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.

  • There is nothing like type as a button in drupal – vishal shah Aug 18 '16 at 11:19
  • What's the problem exactly? The button type exists to Format an action button. When the button is pressed, the form will be submitted to Drupal, where it is validated and rebuilt. The submit handler is not invoked. From what you've said it sounds like it's working perfectly – Clive Aug 18 '16 at 11:36
  • But i don't need to work it as submit button, want to work it as type='button' and do ajax requests .will that be possible ? – RubberDuckIsKing Aug 18 '16 at 11:45

To react on clicking a submit or action button a normal form needs to reload. A form element with ajax handles this in js and sends an ajax request in the background and gets an ajax response with commands, what to do without reloading the complete page, like exchange parts of the html or open a modal dialog.

You can ajaxify most form elements, definitely submit and button. And if your are successful, the page shouldn't reload anymore.

What the problem is with your code, I can't see any problem, like in your last ajax question.

  • can we have a button (Not action / submit) ? the problem is that as explained in the question the form is validating other required elements and throws the error message and it shows in the next reload when i leave the page as it is . – RubberDuckIsKing Aug 18 '16 at 14:07
  • @IcecreamJelly Drupal has you covered with #limit_validation_errors – Clive Aug 18 '16 at 14:27

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.