0

Somewhere between buildForm and validateForm the $form_state's 'input' becomes the 'values'. The values are listed as 'null'. For some reason, some sort of validation from core seems to be preventing only SOME of my values to be left behind, and there's no reason I can come up with, because the data is identical. Both are textfields representing URLs. My form is kind of complicated, so I can't post it unfortunately.

I have a feeling the problem is involving ajax somehow. The fields which are failing are fields which are added to the form by ajax. Sometimes the values turn to null, and other times the values turn to an empty string.

Where should I look in core to figure out what validation might be getting mad at my fields? Do you know of any typical thing that might prevent a user's input from passing to values?

EDIT: Here are the contents of my buildForm:

$items['redirect']['old'] = [
  '#type' => 'textfield',
  '#title' => t('Old'),
  '#default_value' => '',
  '#size' => 40,
  '#maxlength' => 502,
  '#description' => t('Old URL.'),
  '#required' => TRUE,
];

$items['redirect']['new'] = [
  '#type' => 'textfield',
  '#title' => t('New'),
  '#default_value' => '',
  '#size' => 40,
  '#maxlength' => 502,
  '#description' => t('Redirect URL.'),
  '#required' => TRUE,
];

$defs = $this->grabDefaultValues();

FormHelper::elementClone($form, $form_state, $items['redirect'], ['old'=>$defs['olds'],'new'=>$defs['news']]);

$form['submit'] = array(
  '#type' => 'submit',
  '#value' => 'Submit',
);

return $form;

The FormHelper part is just some reusable code for cloning the two elements together, so I get old1, old2, old3, etc. old1 always works, but old2 and old3 mess up sometimes. The thing is, I have used this code before, and it worked, so I don't know what's happening in this case.

1
  • Can you post some of your code, and what you've attempted?
    – Amy
    Jan 12, 2018 at 20:38

1 Answer 1

0

Apparently, around line 1162 of FormBuilder.php, Drupal checks to see if there is a #name element to the field element you're using. If there's not, it uses the parent's name, which seems to be the previous form element's name.

The clone function wasn't setting unique #name's. I could have sworn it worked before, but maybe not.

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.