Skip to content

Commit 6e81c0c

Browse files
abhilal007paul-m
authored andcommitted
Issue #2930038 by jlbellido, Lal_, cosolom, No Sssweat, andeersg, ccshannon, monymirza: AJAX Wizard example is broken
1 parent 34d1f69 commit 6e81c0c

File tree

2 files changed

+248
-137
lines changed

2 files changed

+248
-137
lines changed

ajax_example/src/Form/Wizard.php

Lines changed: 124 additions & 137 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
use Drupal\Core\Form\FormStateInterface;
77
use Drupal\Core\Url;
88
use Drupal\Core\Link;
9-
use Drupal\Core\Ajax\AjaxResponse;
10-
use Drupal\Core\Ajax\HtmlCommand;
119

1210
/**
1311
* AJAX example wizard.
@@ -44,35 +42,96 @@ public function buildForm(array $form, FormStateInterface $form_state, $no_js_us
4442
];
4543

4644
$form['step'] = [
47-
'#type' => 'hidden',
45+
'#type' => 'value',
4846
'#value' => !empty($form_state->getValue('step')) ? $form_state->getValue('step') : 1,
4947
];
50-
print_r($form_state->getValue('step'));
5148

52-
if ($form['step']['#value'] == 1) {
53-
$form['step1'] = [
54-
'#type' => 'fieldset',
55-
'#title' => $this->t('Step 1: Personal details'),
56-
];
57-
$form['step1']['name'] = [
58-
'#type' => 'textfield',
59-
'#title' => $this->t('Your name'),
60-
'#default_value' => empty($form_state->getValue([
61-
'step1',
62-
'name',
63-
]) ? '' : $form_state->getValue(['step1', 'name'])),
64-
'#required' => TRUE,
65-
];
49+
switch ($form['step']['#value']) {
50+
case 1:
51+
$limit_validation_errors = [['step']];
52+
$form['step1'] = [
53+
'#type' => 'fieldset',
54+
'#title' => $this->t('Step 1: Personal details'),
55+
];
56+
$form['step1']['name'] = [
57+
'#type' => 'textfield',
58+
'#title' => $this->t('Your name'),
59+
'#default_value' => $form_state->hasValue(['step1', 'name']) ? $form_state->getValue(['step1', 'name']) : '',
60+
'#required' => TRUE,
61+
];
62+
break;
6663

67-
$form['next'] = [
64+
case 2:
65+
$limit_validation_errors = [['step'], ['step1']];
66+
$form['step1'] = [
67+
'#type' => 'value',
68+
'#value' => $form_state->getValue('step1'),
69+
];
70+
$form['step2'] = [
71+
'#type' => 'fieldset',
72+
'#title' => t('Step 2: Street address info'),
73+
];
74+
$form['step2']['address'] = [
75+
'#type' => 'textfield',
76+
'#title' => $this->t('Your street address'),
77+
'#default_value' => $form_state->hasValue(['step2', 'address']) ? $form_state->getValue(['step2', 'address']) : '',
78+
'#required' => TRUE,
79+
];
80+
break;
81+
82+
case 3:
83+
$limit_validation_errors = [['step'], ['step1'], ['step2']];
84+
$form['step1'] = [
85+
'#type' => 'value',
86+
'#value' => $form_state->getValue('step1'),
87+
];
88+
$form['step2'] = [
89+
'#type' => 'value',
90+
'#value' => $form_state->getValue('step2'),
91+
];
92+
$form['step3'] = [
93+
'#type' => 'fieldset',
94+
'#title' => $this->t('Step 3: City info'),
95+
];
96+
$form['step3']['city'] = [
97+
'#type' => 'textfield',
98+
'#title' => $this->t('Your city'),
99+
'#default_value' => $form_state->hasValue(['step3', 'city']) ? $form_state->getValue(['step3', 'city']) : '',
100+
'#required' => TRUE,
101+
];
102+
break;
103+
}
104+
105+
$form['actions'] = ['#type' => 'actions'];
106+
if ($form['step']['#value'] > 1) {
107+
$form['actions']['prev'] = [
108+
'#type' => 'submit',
109+
'#value' => $this->t('Previous step'),
110+
'#limit_validation_errors' => $limit_validation_errors,
111+
'#submit' => ['::prevSubmit'],
112+
'#ajax' => [
113+
'wrapper' => 'ajax-example-wizard-wrapper',
114+
'callback' => '::prompt',
115+
],
116+
];
117+
}
118+
if ($form['step']['#value'] != 3) {
119+
$form['actions']['next'] = [
68120
'#type' => 'submit',
69121
'#value' => $this->t('Next step'),
122+
'#submit' => ['::nextSubmit'],
70123
'#ajax' => [
71-
'wrapper' => 'ajax-example-wizard',
124+
'wrapper' => 'ajax-example-wizard-wrapper',
72125
'callback' => '::prompt',
73126
],
74127
];
75128
}
129+
if ($form['step']['#value'] == 3) {
130+
$form['actions']['submit'] = [
131+
'#type' => 'submit',
132+
'#value' => $this->t("Submit your information"),
133+
];
134+
}
76135

77136
// This simply allows us to demonstrate no-javascript use without
78137
// actually turning off javascript in the browser. Removing the #ajax
@@ -82,10 +141,13 @@ public function buildForm(array $form, FormStateInterface $form_state, $no_js_us
82141
if ($no_js_use) {
83142
// Remove the #ajax from the above, so ajax.js won't be loaded.
84143
// For demonstration only.
85-
unset($form['next']['#ajax']);
86-
unset($form['prev']['#ajax']);
144+
unset($form['actions']['next']['#ajax']);
145+
unset($form['actions']['prev']['#ajax']);
87146
}
88147

148+
$form['#prefix'] = '<div id="ajax-example-wizard-wrapper">';
149+
$form['#suffix'] = '</div>';
150+
89151
return $form;
90152
}
91153

@@ -104,125 +166,50 @@ public function prompt(array $form, FormStateInterface $form_state) {
104166
return $form;
105167
}
106168

169+
/**
170+
* Ajax callback that moves the form to the next step and rebuild the form.
171+
*
172+
* @param array $form
173+
* The Form API form.
174+
* @param \Drupal\Core\Form\FormStateInterface $form_state
175+
* The FormState object.
176+
*
177+
* @return array
178+
* The Form API form.
179+
*/
180+
public function nextSubmit(array $form, FormStateInterface $form_state) {
181+
$form_state->setValue('step', $form_state->getValue('step') + 1);
182+
$form_state->setRebuild();
183+
return $form;
184+
}
185+
186+
/**
187+
* Ajax callback that moves the form to the previous step.
188+
*
189+
* @param array $form
190+
* The Form API form.
191+
* @param \Drupal\Core\Form\FormStateInterface $form_state
192+
* The FormState object.
193+
*
194+
* @return array
195+
* The Form API form.
196+
*/
197+
public function prevSubmit(array $form, FormStateInterface $form_state) {
198+
$form_state->setValue('step', $form_state->getValue('step') - 1);
199+
$form_state->setRebuild();
200+
return $form;
201+
}
202+
107203
/**
108204
* Save away the current information.
109205
*/
110206
public function submitForm(array &$form, FormStateInterface $form_state) {
111-
if ($form_state->getTriggeringElement()['#value'] == $this->t('Submit your information')) {
112-
$value_message = $this->t('Your information has been submitted:') . ' ';
113-
foreach ($form_state->getValue('value') as $step => $values) {
114-
$value_message .= "$step: ";
115-
foreach ($values as $key => $value) {
116-
$value_message .= "$key=$value, ";
117-
}
118-
}
119-
$this->messenger()->addMessage($value_message);
120-
$form_state->setRebuild(FALSE);
121-
// Redirect to #action, else return.
122-
return;
123-
}
124-
else {
125-
$step = $form_state->getValue('step');
126-
// Increment or decrement the step as needed. Recover values if they
127-
// exist.
128-
if ($form_state->getTriggeringElement()['#value']->__toString() == $this->t('Next step')) {
129-
$step++;
130-
}
131-
elseif ($form_state->getTriggeringElement()['#value']->__toString() == $this->t('Previous step')) {
132-
$step--;
133-
}
134-
135-
switch ($step) {
136-
case 1:
137-
$form['step1'] = [
138-
'#type' => 'fieldset',
139-
'#title' => $this->t('Step 1: Personal details'),
140-
];
141-
$form['step1']['name'] = [
142-
'#type' => 'textfield',
143-
'#title' => $this->t('Your name'),
144-
'#default_value' => empty($form_state->getValue([
145-
'step1',
146-
'name',
147-
]) ? '' : $form_state->getValue(['step1', 'name'])),
148-
'#required' => TRUE,
149-
];
150-
$form_state->setValue('step', 1);
151-
break;
152-
153-
case 2:
154-
unset($form['step1']);
155-
unset($form['next']);
156-
$form['step2'] = [
157-
'#type' => 'fieldset',
158-
'#title' => $this->t('Step 2: Street address info'),
159-
];
160-
$form['step2']['address'] = [
161-
'#type' => 'textfield',
162-
'#title' => $this->t('Your street address'),
163-
'#default_value' => empty($form_state->getValue([
164-
'step2',
165-
'address',
166-
]) ? '' : $form_state->getValue(['step2', 'address'])),
167-
'#required' => TRUE,
168-
];
169-
$form_state->setValue('step', $step);
170-
break;
171-
172-
case 3:
173-
174-
$form['step3'] = [
175-
'#type' => 'fieldset',
176-
'#title' => $this->t('Step 3: City info'),
177-
];
178-
$form['step3']['city'] = [
179-
'#type' => 'textfield',
180-
'#title' => $this->t('Your city'),
181-
'#default_value' => empty($form_state->getValue([
182-
'step3',
183-
'city',
184-
]) ? '' : $form_state->getValue(['step3', 'city'])),
185-
'#required' => TRUE,
186-
];
187-
$form_state->setValue('step', $step);
188-
break;
189-
}
190-
if ($step == 3) {
191-
192-
$form['submit'] = [
193-
'#type' => 'submit',
194-
'#value' => $this->t("Submit your information"),
195-
];
196-
}
197-
if ($step > 1 && !isset($form['prev'])) {
198-
$form['prev'] = [
199-
'#type' => 'submit',
200-
'#value' => $this->t("Previous step"),
201-
// Since all info will be discarded, don't validate on 'prev'.
202-
'#limit_validation_errors' => [],
203-
// #submit is required to use #limit_validation_errors.
204-
'#submit' => ['ajax_example_wizard_submit'],
205-
'#ajax' => [
206-
'wrapper' => 'ajax-example-wizard',
207-
'callback' => '::prompt',
208-
],
209-
];
210-
}
211-
if ($step < 3 && !isset($form['next'])) {
212-
$form['next'] = [
213-
'#type' => 'submit',
214-
'#value' => $this->t('Next step'),
215-
'#limit_validation_errors' => [],
216-
'#ajax' => [
217-
'wrapper' => 'ajax-example-wizard',
218-
'callback' => '::prompt',
219-
],
220-
];
221-
}
222-
$response = new AjaxResponse();
223-
$response->addCommand(new HtmlCommand('#ajax-example-wizard', $form));
224-
return $response;
225-
}
207+
208+
$messenger = \Drupal::messenger();
209+
$messenger->addMessage($this->t('Your information has been submitted:'));
210+
$messenger->addMessage($this->t('Name: @name', ['@name' => $form_state->getValue(['step1', 'name'])]));
211+
$messenger->addMessage($this->t('Address: @address', ['@address' => $form_state->getValue(['step2', 'address'])]));
212+
$messenger->addMessage($this->t('City: @city', ['@city' => $form_state->getValue(['step3', 'city'])]));
226213

227214
}
228215

0 commit comments

Comments
 (0)