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.
$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?