1

I have a node form on which two fields are displaying. One is a list of radio buttons which is required field.

I am getting a value from url using arg(3) and setting the default value of that field

$form['field_test'][LANGUAGE_NONE]['#default_value'] = arg(3);

I have a link field which is a multi valued field. It means this field as add another button.

When I click on this add another button and then submit the form, I see an error of Test field is required, and that default value of test field gone.

Without clicking the add another button everything works fine.

How can I set a default value of the test field in this case?

Code:

if (!$form['nid']['#value']) {
  if (arg(3)) {
    // Select the default value.
    $tg = arg(3);
    $form['field_test'][LANGUAGE_NONE]['#default_value'] = $tg;
  }
}

  // Make the field readonly.
  $form['field_test'][LANGUAGE_NONE]['#attributes']['disabled'] = 'disabled';
  • Looks like you are ignoring delta? Also please post the form code. – Kevin Jan 10 at 14:46
  • Added the code. – Ahmad Jan 11 at 5:13
0

Ok. I found the solution. Let me share my code here.

I stored the argument in the $form_state['storage] so that it can be preserved during the ajax call.

if (arg(0) == 'node' && arg(3)) {
    $form_state['storage']['field_test_value'] = arg(3);
}

// Only on node creation.
if (!$form['nid']['#value'] && isset($form_state['storage'])) {
    $form['field_test'][LANGUAGE_NONE]['#default_value'] = $form_state['storage']['field_test_value'];
}

$form['field_test'][LANGUAGE_NONE]['#attributes']['disabled'] = 'disabled';

Hope this will help someone.

Your Answer

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.