I am working on a node form in which my users can add sport-teams (which I'll handle through a taxonomy) that are related to the node (say it's a blog-entry about your local rugby-team and you want to relate it to the team's taxonomy entry). To each team entry, I also have a db-table with a corresponding zip-code of the teams headquarter (say your local rugby team comes from cardiff).

Now the users can also add teams, that are currently not in the taxonomy list (through the use of taxonomy-autocomplete (i.e. tagging)). When that happens, during form_validate, I fire a form_set_error() and ask the user to type in the City the team is coming from.

In effect, I want to add a field to the form during form_validate. But how do I do that?

I tried, to no avail, adding the following to the $form variable in the form_validate function:

$form['field_new_team_location'][$i] = array(
'#type' => 'textfield',
'#title' => t('new team location'),
'#default_value' => $node->title,
'#size' => 60,
'#maxlength' => 128,
'#required' => TRUE,
'#description' => t('description')

EDIT Alas, although Charlie S' answer helped me to put up a field during validation (read: the html gets displayed; I can write in them), but it seems I cannot access it in a follow-up validation. In fact, when I hit save again, neither $form nor $form_state have the newly created fields in them! Any help accessing my fields is much appreciated!

2nd EDIT With what Charlie S said, it works. One thing that holding me up was that the form only gets rebuilt when no error messages are being cast. In other words, every required field has to be supplied before the form gets rebuilt and the user is able to input something in the new fields.

2 Answers 2


In form_validate(), make sure your arguments are being passed by reference:

myform_validate(&$form, &$form_state)

Then the last thing you should have in _validate() is:

$form_state['rebuild'] = TRUE;

Edit: You will want to handle the actual adding of the element in the form builder. This is tested and working:

function customform_custom_form($form, $form_state) {
  $form['field_a'] = array(
    '#type' => 'textfield',
    '#title' => 'Field A',
    '#required' => TRUE,
    '#weight' => 1,

  // Add field b depending on the value of field a.
  if (isset($form_state['input']) && !empty($form_state['input']['field_a'])) {
    $form['field_b'] = array(
      '#type' => 'textfield',
      '#title' => 'Field B',
      '#weight' => 2,

  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => 'Submit',
    '#weight' => 99,

  return $form;

function customform_custom_form_validate(&$form, &$form_state) {
  if (empty($form_state['values']['field_b'])) {
    form_set_error('field_b', 'Please enter something in Field B.');
    $form_state['rebuild'] = TRUE;

function customform_custom_form_submit(&$form, &$form_state) {
  drupal_set_message('You entered ' . $form_state['values']['field_b'] . ' for Field B.');
  • not exactly the complete answer, but it certainly pushed me in the right direction: I had to append my field to $form_state['complete form'], instead of $form. But apart from that, good job! thanks!
    – alex
    Commented Feb 28, 2012 at 14:04
  • That makes sense, I will update the answer above. Glad it's working! Commented Feb 28, 2012 at 14:35
  • I am very, very sorry Charlie, but I have to revoke your answer-tag, because I have a serious follow-up question, please read the edit part of the question. thanks.
    – alex
    Commented Feb 28, 2012 at 15:03
  • You need to have something registered for this form element when the form is first built. I will add the full code structure to my post above. Commented Feb 28, 2012 at 16:14
  • that means I have to have all the fields registered as soon as the form has been built for the first time? Or, in other words, I cannot add fields, I can only make them appear to the user? That would hurt quite a bit, since then I'd have to limit the number of values the user can submit.
    – alex
    Commented Feb 28, 2012 at 19:13

I'm not sure you can actually add fields to the form on validate? Have you looked at using ajax for this instead? You can define your own submit function and check the inputted tags here. To make it easier, maybe you could add the field_new_team_location when you build your form and just set $form['field_new_team_location']['#access'] = FALSE and then enable it when you need it.

  • well, it is possible, but nonetheless your suggestions were quite creative so +1 for that!
    – alex
    Commented Feb 28, 2012 at 14:07

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.