Skip to content

Commit

Permalink
Merge pull request #62 from systopia/checkbox-required-initially-unch…
Browse files Browse the repository at this point in the history
…ecked

Checkbox: Allow required checkboxes that are initially unchecked
  • Loading branch information
dontub authored Jul 9, 2024
2 parents e7cf3a6 + 24dbd8c commit 2e92c7b
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 3 deletions.
12 changes: 10 additions & 2 deletions src/Form/Control/CheckboxArrayFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,19 @@ public function createFormArray(
): array {
Assertion::isInstanceOf($definition, ControlDefinition::class);
/** @var \Drupal\json_forms\JsonForms\Definition\Control\ControlDefinition $definition */
return [
$form = [
'#type' => 'checkbox',
'#required' => FALSE,
'#required' => TRUE === $definition->getConst(),
'#value_callback' => CheckboxValueCallback::class . '::convert',
] + BasicFormPropertiesFactory::createFieldProperties($definition, $formState);

if (FALSE === $definition->getDefault() && TRUE === $definition->getConst()) {
// Don't initially check checkbox, if it is required, but its default is
// false.
unset($form['#value']);
}

return $form;
}

public function supportsDefinition(DefinitionInterface $definition): bool {
Expand Down
45 changes: 44 additions & 1 deletion tests/src/Unit/Form/Control/CheckboxArrayFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public function testCreateFormArraySimple(): void {
static::assertEquals($expected, $form);
}

public function testCreateFormArray(): void {
public function testCreateFormArrayRequired(): void {
$jsonSchema = (object) [
'type' => 'object',
'properties' => (object) [
Expand All @@ -99,6 +99,7 @@ public function testCreateFormArray(): void {
'foo' => (object) [
'type' => 'boolean',
'description' => 'Test description',
'const' => TRUE,
],
],
'required' => ['foo'],
Expand All @@ -123,6 +124,7 @@ public function testCreateFormArray(): void {
'#value_callback' => CheckboxValueCallback::class . '::convert',
'#disabled' => TRUE,
'#required' => TRUE,
'#value' => TRUE,
'#parents' => ['test', 'foo'],
'#title' => 'Test',
// phpcs:disable DrupalPractice.General.DescriptionT.DescriptionT
Expand All @@ -135,6 +137,47 @@ public function testCreateFormArray(): void {
static::assertEquals($expected, $form);
}

public function testCreateFormArrayRequiredDefaultFalse(): void {
$jsonSchema = (object) [
'type' => 'object',
'properties' => (object) [
'test' => (object) [
'type' => 'object',
'properties' => (object) [
'foo' => (object) [
'type' => 'boolean',
'const' => TRUE,
'default' => FALSE,
],
],
'required' => ['foo'],
],
],
];

$uiSchema = (object) [
'type' => 'Control',
'scope' => '#/properties/test/properties/foo',
];

$definition = ControlDefinition::fromJsonSchema($uiSchema, $jsonSchema, FALSE);
$form = $this->factory->createFormArray($definition, $this->formState, $this->formArrayFactoryMock);

$expected = [
'#type' => 'checkbox',
'#value_callback' => CheckboxValueCallback::class . '::convert',
'#disabled' => FALSE,
'#required' => TRUE,
'#default_value' => FALSE,
'#parents' => ['test', 'foo'],
'#title' => 'Foo',
'#tree' => TRUE,
'#limit_validation_errors' => [],
'#_scope' => '#/properties/test/properties/foo',
];
static::assertEquals($expected, $form);
}

public function testSupportsDefinition(): void {
$jsonSchema = (object) [
'type' => 'object',
Expand Down

0 comments on commit 2e92c7b

Please sign in to comment.