By Ahmad


2019-01-10 12:52:34 8 Comments

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';

1 comments

@Ahmad 2019-01-11 11:56:15

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.

Related Questions

Sponsored Content

3 Answered Questions

[SOLVED] How to theme a radio button field on node edit form?

2 Answered Questions

[SOLVED] Form ajax add more button added via another ajax callback not working

  • 2016-06-28 17:17:57
  • Nabajit Roy
  • 631 View
  • 1 Score
  • 2 Answer
  • Tags:   7 forms ajax

1 Answered Questions

[SOLVED] How to get the default value of radio button from database?

  • 2015-08-24 14:46:10
  • Sugandh Khanna
  • 1418 View
  • 2 Score
  • 1 Answer
  • Tags:   forms

0 Answered Questions

How to disable radio button when Commerce Stock = 0?

2 Answered Questions

[SOLVED] How to assign default values ​​for an edit form?

  • 2014-02-25 10:35:37
  • Djik
  • 251 View
  • 0 Score
  • 2 Answer
  • Tags:   forms

1 Answered Questions

1 Answered Questions

[SOLVED] How can i change the form default value but not via hook_form_alter

  • 2012-07-20 03:30:43
  • cobenash
  • 2465 View
  • 0 Score
  • 1 Answer
  • Tags:   nodes forms

3 Answered Questions

[SOLVED] Get the node ID from the "node/add" menu item and hook_form_alter()

1 Answered Questions

[SOLVED] Can I make the second button in a form the default button?

  • 2011-11-16 16:25:50
  • ianmjones
  • 471 View
  • 0 Score
  • 1 Answer
  • Tags:   6 forms

Sponsored Content