1

Basically, I have a hidden field I set using arg().

$form['slideid'] = array(
  '#type' => 'hidden',
  '#value' => arg(2)
);

$form['fid'] = array(
    '#type' => 'managed_file',
    '#title' => t('Image'),
    '#default_value' => "",
    '#size' => 40,
    '#description' => t("Image should be less than xxxxxxxxxxxx."),
    '#upload_location' => 'public://',
    '#prefix' => '<div id="things">',
    '#suffix' => '</div>',
);

This value is set correctly once the form loads, but after a call to a AJAX field (managed_file), the form is refreshed and I get the wrong value in arg(2).

I have found arg(1) changed when doing AJAX, which doesn't give me any solution.

Is there any alternative that can be used, or can we skip the hidden field in the AJAX re-build? Is it possible to use a managed_file element without the AJAX submit?

  • Are you implementing a new content type, or are you simply building a form? – kiamlaluno Sep 10 '15 at 7:47
  • actually the hook_form is being used for a Custom Entity... – naxrohan Sep 10 '15 at 8:45
  • That is because the form is rebuilt from the Ajax path, which in this case, systen/ajax. You can store the slide ID in form_state, and check if it exists before overwriting (if it is already set, do not overwrite which means the form is rebuilt from Ajax callback). – AyeshK Sep 10 '15 at 9:11
  • hook_form() is the hook used by Drupal for content type, which means it is implemented from a module that creates a new content type (for example, the Blog or the Forum module). In the other cases, you are not implementing hook_form(), but you are simply writing a form builder: a function that build a form using the form element provided from the form API. – kiamlaluno Sep 10 '15 at 11:04
  • I am explaining this because many users confuse the two concepts, which are completely different. In fact, the parameters provided to hook_form() are totally different from the parameters provided to a form builder, or other hooks. – kiamlaluno Sep 10 '15 at 11:08
1

As suggested by @AyeshK, I checked the $form_state in the hook_form before i set the value from arg()

 $form['slideid'] = array(
  '#type' => 'hidden',
  '#value' => (!empty($form_state['input']['slideid']) ? $form_state['input']['slideid'] : arg(2))
);

This could also be done in the hook_form_submit ...

function hook_form_submit($form, &$form_state){

    if($form_state['values']['slideid'] != $form_state['input']['slideid']){
        $form_state['values']['slideid'] = (int)$form_state['input']['slideid'];
    }
   //====save the form data...

}

It looks like a crude fix, but it works for me.

Thank you for your help @AyeshK & @kiamlaluno

0

If you want to use ajax submit you need to set callback path using #ajax['path'] otherwise it will automatically set to system/ajax.

Your Answer

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

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