Skip to content

Commit c635d82

Browse files
eojthebravepaul-m
eojthebrave
authored andcommitted
Issue #2933259 by eojthebrave: Add some additional information to form_api_example #states demo
1 parent 1b02a4b commit c635d82

File tree

2 files changed

+64
-5
lines changed

2 files changed

+64
-5
lines changed

Diff for: form_api_example/src/Form/StateDemo.php

+19-5
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,17 @@
77
/**
88
* Implements the state demo form controller.
99
*
10-
* This example demonstrates using the #state property to bind the visibility of
11-
* a form element to the value of another element in the form. In the example,
12-
* when the user checks the "Need Special Accommodation" checkbox, additional
13-
* form elements are made visible.
10+
* This example demonstrates using the #states property to bind the visibility
11+
* of a form element to the value of another element in the form. In the
12+
* example, when the user checks the "Need Special Accommodation" checkbox,
13+
* additional form elements are made visible.
1414
*
1515
* The submit handler for this form is implemented by the
1616
* \Drupal\form_api_example\Form\DemoBase class.
1717
*
1818
* @see \Drupal\Core\Form\FormBase
1919
* @see \Drupal\form_api_example\Form\DemoBase
20+
* @see drupal_process_states()
2021
*/
2122
class StateDemo extends DemoBase {
2223

@@ -43,9 +44,22 @@ public function buildForm(array $form, FormStateInterface $form_state) {
4344
'#attributes' => [
4445
'class' => 'accommodation',
4546
],
47+
// #states is an associative array. Each key is the name of a state to
48+
// apply to the element, such as 'visible'. Each value is another array,
49+
// making a list of conditions that denote when the state should be
50+
// applied. Every condition is a key/value pair, whose key is a jQuery
51+
// selector that denotes another element on the page, and whose value is
52+
// an array of conditions, which must bet met on in order for the state to
53+
// be applied.
54+
//
55+
// For additional documentation on the #states property including a list
56+
// of valid states and conditions see drupal_process_states().
4657
'#states' => [
58+
// The state being affected is "invisible".
4759
'invisible' => [
48-
'input[name="needs_accommodation"]' => ['checked' => FALSE],
60+
// Drupal will only apply this state when the element that satisfies
61+
// the selector input[name="needs_accommodation"] is un-checked.
62+
':input[name="needs_accommodation"]' => ['checked' => FALSE],
4963
],
5064
],
5165
];
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace Drupal\Tests\form_api_example\FunctionalJavascript;
4+
5+
use Drupal\FunctionalJavascriptTests\JavascriptTestBase;
6+
use Drupal\Core\Url;
7+
8+
/**
9+
* @group form_api_example
10+
*
11+
* @ingroup form_api_example
12+
*/
13+
class StateDemoTest extends JavascriptTestBase {
14+
15+
/**
16+
* Our module dependencies.
17+
*
18+
* @var string[]
19+
*/
20+
static public $modules = ['form_api_example'];
21+
22+
/**
23+
* Functional tests for the StateDemo example form.
24+
*/
25+
public function testStateForm() {
26+
// Visit form route.
27+
$route = Url::fromRoute('form_api_example.state_demo');
28+
$this->drupalGet($route);
29+
30+
// Get Mink stuff.
31+
$page = $this->getSession()->getPage();
32+
33+
// Verify we can find the diet restrictions textfield, and that by default
34+
// it is not visible.
35+
$this->assertNotEmpty($checkbox = $page->find('css', 'input[name="diet"]'));
36+
$this->assertFalse($checkbox->isVisible(), 'Diet restrictions field is not visible.');
37+
38+
// Check the needs special accommodation checkbox.
39+
$page->checkField('needs_accommodation');
40+
41+
// Verify the textfield is visible now.
42+
$this->assertTrue($checkbox->isVisible(), 'Diet restrictions field is visible.');
43+
}
44+
45+
}

0 commit comments

Comments
 (0)