0

I created a form and designed it by referred this link. It worked fine. I used the following code to display a textbox in my form

public function buildForm(array $form, FormStateInterface $form_state) {    
    $form['searchtext'] = array(
          '#type' => 'textfield',
          '#title' => t('Keyword Search:'),
          //'#default_value' => (isset($record['searchtext'])) ? $record['searchtext']:'',
          '#attributes' => array('placeholder' => t('Enter keyword to assist locating notices relevant to your vehicle')),
        );
    $form['#theme'] = ['notices_search_form'];
    //echo "before submit: <pre>";print_r($form);echo "</pre>";
    return $form;
}

and it is rendered correctly as i expected as below (it has name, id and everything)

<input placeholder="Enter keyword to assist locating notices relevant to your vehicle" data-drupal-selector="edit-searchtext" type="text" id="edit-searchtext" name="searchtext" value="" size="60" maxlength="128" class="form-text">

But after submitting the form, I stop the redirection and load the form with the value entered while submitting form using following code

public function submitForm(array &$form, FormStateInterface $form_state) {
    $fieldvalues=$form_state->getValues();
    $form['searchtext']['#value'] = (isset($fieldvalues['searchtext'])) ? $fieldvalues['searchtext']:'';
    $form_state->disableRedirect();
    return $form;
  }

But this time form loads without name or id as below

<input placeholder="Enter keyword to assist locating notices relevant to your vehicle" type="text" value="sdfdf" size="60" maxlength="128" class="form-text">

I am new to drupal. Someone suggest me what am i missing?

Edit 1

I tried to place the name in the array like below and it is worked fine.

$form['searchtext'] = array(
      '#name' => 'searchtext',
      '#type' => 'textfield',
      '#title' => t('Keyword Search:'),
      //'#default_value' => (isset($record['searchtext'])) ? $record['searchtext']:'',
      '#attributes' => array('placeholder' => t('Enter keyword to assist locating notices relevant to your vehicle')),
    );

But setting 'true' to "#default_value" for checkbox field is not checking while rendering it.

  • Your submit handler is wrong. You have to use ajax. See drupal.stackexchange.com/questions/188730/… – user21641 Sep 18 '17 at 7:17
  • @IvanJaros, the submit handler looks a bit odd, because normally it doesn't return $form, but in general it's OK to rebuild forms without ajax. – 4k4 Sep 18 '17 at 7:39
  • Actually i never had to do anything. After removing $form['searchtext']['#value'] = (isset($fieldvalues['searchtext'])) ? $fieldvalues['searchtext']:''; this line it works perfectly. Form takes the submitted value by default. But still have a question. After an attribute has been added to an existing element, why the form does not render the name of that element? – siddiq Sep 18 '17 at 7:54

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.

Browse other questions tagged or ask your own question.