1

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 ??

3
  • 1
    There is nothing like type as a button in drupal Commented Aug 18, 2016 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
    Commented Aug 18, 2016 at 11:36
  • 2
    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 ?
    – KTM
    Commented Aug 18, 2016 at 11:45

1 Answer 1

1

As mentioned in the comments, this is not supported yet but it might at some point: https://www.drupal.org/project/drupal/issues/1671190

What you can do is return false in the onclick event. For example:

$form[button] = array(
  '#type' => 'button',
  '#name' => 'button',
  '#value' => "Do some JS",
  "#attributes" => array(
    "onclick" => "doJs(); return false;",
  ),
);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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