generated from yiisoft/package-template
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix bug in
Checkbox::class
, document all cases (#95)
- Loading branch information
1 parent
a00c7b7
commit 4019bc8
Showing
5 changed files
with
246 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,196 @@ | ||
# Checkbox widget | ||
|
||
[Checkbox](https://www.w3.org/TR/2012/WD-html-markup-20120329/input.checkbox.html#input.checkbox) is an input element with a type attribute whose value is "checkbox" represents a state or option that can be toggled. | ||
|
||
## Usage | ||
|
||
The `Checkbox::class` widget is designed to return the status of the checkbox. Generally it returns two values, by default it is `0` when no value is selected, and` 1` otherwise. | ||
|
||
You can also configure the widget to return the value only when the check box is selected. | ||
|
||
### Example Checkbox with default values `0` and `1`: | ||
|
||
TestForm form: | ||
|
||
```php | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Form; | ||
|
||
use Yiisoft\Form\FormModel; | ||
|
||
final class TestForm extends FormModel | ||
{ | ||
public bool $active = false; | ||
} | ||
``` | ||
|
||
Widgets view: | ||
|
||
```php | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Yiisoft\Form\Widget\Checkbox; | ||
use Yiisoft\Form\Widget\Field; | ||
use Yiisoft\Form\Widget\Form; | ||
use Yiisoft\Form\Widget\Label; | ||
?> | ||
|
||
<?= Form::widget()->action('widgets')->csrf($csrf)->begin() ?> | ||
<?= Checkbox::widget()->config($data, 'active', [])->render() ?> | ||
<hr class="mt-3"> | ||
<?= Field::widget()->submitButton(['class' => 'button is-block is-info is-fullwidth', 'value' => 'Save']); ?> | ||
<?= Form::end() ?> | ||
``` | ||
|
||
### Code generated by `Checkbox::class` Widget | ||
|
||
```html | ||
<form action="widgets" method="POST" _csrf="H4r91n-qkv8GzgEU8E3hhn2vSSu5jnkguqemRBysYa1OwJWHR8Txmk6kUSWGHdfnT54zYPXoDkqN1v4CefZR2w=="> | ||
<input type="hidden" name="_csrf" value="H4r91n-qkv8GzgEU8E3hhn2vSSu5jnkguqemRBysYa1OwJWHR8Txmk6kUSWGHdfnT54zYPXoDkqN1v4CefZR2w=="> | ||
<input type="hidden" name="TestForm[active]" value="0"> | ||
<label><input type="checkbox" id="testform-active" name="TestForm[active]" value="1">Active</label> | ||
<hr class="mt-3"> | ||
<div> | ||
<input type="submit" id="submit-181408062514001" class="button is-block is-info is-fullwidth" name="submit-181408062514001" value="Save"> | ||
</div> | ||
</form> | ||
``` | ||
|
||
As we can see in the code we have the value `0` for the hidden input and `1` for the input checkbox tag, correctly. | ||
|
||
### Example Checkbox with default values `1` when the checkbox is checked: | ||
|
||
TestForm form: | ||
|
||
```php | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Form; | ||
|
||
use Yiisoft\Form\FormModel; | ||
|
||
final class TestForm extends FormModel | ||
{ | ||
public bool $active = false; | ||
} | ||
``` | ||
|
||
Widgets view: | ||
|
||
```php | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Yiisoft\Form\Widget\Checkbox; | ||
use Yiisoft\Form\Widget\Field; | ||
use Yiisoft\Form\Widget\Form; | ||
use Yiisoft\Form\Widget\Label; | ||
?> | ||
|
||
<?= Form::widget()->action('widgets')->csrf($csrf)->begin() ?> | ||
<?= Checkbox::widget()->config($data, 'active', ['forceUncheckedValue' => null])->render() ?> | ||
<hr class="mt-3"> | ||
<?= Field::widget()->submitButton(['class' => 'button is-block is-info is-fullwidth', 'value' => 'Save']); ?> | ||
<?= Form::end() ?> | ||
``` | ||
|
||
### Code generated by `Checkbox::class` Widget | ||
|
||
```html | ||
<form action="widgets" method="POST" _csrf="Oq_3knkum-2MVDBcu5v_Zbdv4j4NQWKyueB0wrSmExhr5Z_DQUD4iMQ-YG3Ny8kEhV6YdUEnFdiOkSyE0fwjbg=="> | ||
<input type="hidden" name="_csrf" value="Oq_3knkum-2MVDBcu5v_Zbdv4j4NQWKyueB0wrSmExhr5Z_DQUD4iMQ-YG3Ny8kEhV6YdUEnFdiOkSyE0fwjbg=="> | ||
<label><input type="checkbox" id="testform-active" name="TestForm[active]" value="1"> Active</label> | ||
<hr class="mt-3"> | ||
<div> | ||
<input type="submit" id="submit-186398933261001" class="button is-block is-info is-fullwidth" name="submit-186398933261001" value="Save"> | ||
</div> | ||
</form> | ||
``` | ||
|
||
As we can see there is no hidden input, only `1` for the input checkbox tag, correctly. | ||
|
||
### Example Checkbox with values `inactive` and `active` values: | ||
|
||
TestForm form: | ||
|
||
```php | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Form; | ||
|
||
use Yiisoft\Form\FormModel; | ||
|
||
final class TestForm extends FormModel | ||
{ | ||
public string $active = 'inactive'; | ||
} | ||
``` | ||
|
||
Widgets view: | ||
|
||
```php | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Yiisoft\Form\Widget\Checkbox; | ||
use Yiisoft\Form\Widget\Field; | ||
use Yiisoft\Form\Widget\Form; | ||
use Yiisoft\Form\Widget\Label; | ||
?> | ||
|
||
<?= Form::widget()->action('widgets')->csrf($csrf)->begin() ?> | ||
<?= Checkbox::widget()->config($data, 'active', ['forceUncheckedValue' => 'inactive', 'value' => 'active'])->render() ?> | ||
<hr class="mt-3"> | ||
<?= Field::widget()->submitButton(['class' => 'button is-block is-info is-fullwidth', 'value' => 'Save']); ?> | ||
<?= Form::end() ?> | ||
``` | ||
|
||
### Code generated by `Checkbox::class` Widget | ||
|
||
```html | ||
<form action="widgets" method="POST" _csrf="ifAHTGhruJd9LvvzfoU3xmftdLx3ReD89w21FZpDXajYum8dUAXb8jVEq8II1QGnVdwO9zsjl5bAfO1T_xlt3g=="> | ||
<input type="hidden" name="_csrf" value="ifAHTGhruJd9LvvzfoU3xmftdLx3ReD89w21FZpDXajYum8dUAXb8jVEq8II1QGnVdwO9zsjl5bAfO1T_xlt3g=="> | ||
<input type="hidden" name="TestForm[active]" value="inactive"> | ||
<label><input type="checkbox" id="testform-active" name="TestForm[active]" value="active"> Active</label> | ||
<hr class="mt-3"> | ||
<div> | ||
<input type="submit" id="submit-171128066299001" class="button is-block is-info is-fullwidth" name="submit-171128066299001" value="Save"> | ||
</div> | ||
</form> | ||
``` | ||
|
||
As we can see in the code we have the value `inactive` for the hidden input and `active` for the input checkbox tag, correctly. | ||
|
||
### `Checkbox::class` methods: | ||
|
||
Method | Description | Default | ||
-------|-------------|--------- | ||
`enclosedByLabel(bool $value = true)` | If the widget should be enclosed by label. | `true` | ||
`label(string $value)` | The label text. | `''` | ||
`labelAttributes(array $attributes = [])` | The HTML attributes for the label tag. | `[]` | ||
|
||
### Common methods: | ||
|
||
Method | Description | Default | ||
-------|-------------|--------- | ||
`charset(string $value)` | Sets the charset attribute | `UTF-8` | ||
`config(FormModelInterface $formModel, string $attribute, array $attributes = [])` | Configures the widget. | | ||
`autofocus(bool $value = true)` | Sets the autofocus attribute | `false` | ||
`disabled(bool $value = true)` | Sets the disabled attribute | `false` | ||
`form(string $value)` | Sets the form attribute | `` | ||
`id(string $value)` | Sets the id attribute | `''` | ||
`required(bool $value = true)` | Sets the required attribute | `false` | ||
`readonly()` | Sets the readonly attribute | `false` | ||
`tabIndex(int $value = 0)` | Sets the tabindex attribute | `0` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.