1

I have a form built using for the most part many ajax elements within the form api. The form is working if a user is going through it at a normal pace however as this is to be a productivity tool, any user who is wanting the reaction speeds which you would expect for a front end delivered form, finds themselves clicking elements half way through an ajax process.

What is the best way to overcome this problem?

I was thinking that the basic form elements could be built with the form api with the additions (e.g. extra fields, blocks of fields, reactions to number of checkboxes etc) served front end with jQuery.

The problem with this seems to be if you then use an entity reference or other ajax addition, the form_state is unaware of the changes and will strip back to its previous load. How would I build a browser built form and pass that information to the form_state?

I hope this makes sense to someone.

As an example;

$form['section-1']['checkbox'] = array(
      '#type' => 'checkbox',
      '#ajax' => array(
        'callback' => 'count_callback',
        'trigger_as' => array(
              'name' =>  'button-' . 'section-1',
        ),

      ),
      '#title' => t('Select this one: '),
      '#title_display' => 'before',            
    );

    $form['section-n']['checkbox'] = array(
      '#type' => 'checkbox',
      '#ajax' => array(
        'callback' => 'count_callback',
        'trigger_as' => array(
              'name' =>  'button-' . 'section-n',
        ),

      ),
      '#title' => t('Select this one: '),
      '#title_display' => 'before',            
    );

If there are unlimited number of sections and there is a checkbox for each section to select it. At the moment I am triggering another hidden button element tied to the checkbox to go off to the server and count how many have been selected and return a configured drop down. Say you have 7 sections, 5 of which have been selected, the dropdown would have the options 1 to 5. This would be the hidden button for the section-n

$form['section-n']['button-' . '$section-n'] = array(
        '#type' => 'submit',
        '#attributes' => array(
          'class' => array('hidden button'),
        ),
        '#ajax' => array(
          'callback' => 'count_callback',
        ),
        '#name' => 'button-' . 'section-n',
      '#submit' => array('count_submit'),
      '#limit_validation_errors' => array(),
        '#weight' => 101,
      );

I feel the above method is a lot for what could be achieved with jQuery.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.