Skip to content

Commit 0b50e01

Browse files
committed
added explanation for radios in a form
1 parent a5484cd commit 0b50e01

File tree

1 file changed

+79
-1
lines changed

1 file changed

+79
-1
lines changed

docs/forms.md

Lines changed: 79 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ In the template:
7777
</form>
7878
```
7979

80-
Each element in the form can be rendered as required by the developer. Internally, [Phalcon\Tag][tag] is used to produce the correct HTML for each element, and you can pass additional HTML attributes as the second parameter of `render()`:
80+
Each element in the form can be rendered as required by the developer. Internally, [Phalcon\Html\TagFactory][tagfactory] is used to produce the correct HTML for each element, and you can pass additional HTML attributes as the second parameter of `render()`:
8181

8282
```php
8383
<p>
@@ -737,6 +737,84 @@ Because of the way forms work and interact with elements, certain names are rese
737737

738738
These names correspond to getters in the `Form` object or are properties coming from the Di container.
739739

740+
### Radios
741+
Radio elements are a bit different from other elements. A radio element represents a group of options where only one option can be selected. The `Phalcon\Forms\Element\Radio` element is used to create a single radio button. To create a group of radio buttons, you will need to add multiple `Phalcon\Forms\Element\Radio` elements to your form with the same name but different values:
742+
743+
One thing to note is that the name of the element (first parameter for the `Radio` class) is not the name attribute that will be rendered in the HTML. It is merely a way to identify the specific `Radio` element in the form.
744+
745+
Assume you have to create two radio elements in your form:
746+
747+
```html
748+
<input type="radio" id="dt-single" name="dateRange" value="1" checked="checked" />
749+
<label for="dt-single" class="control-label">Single Date</label>
750+
&nbsp;&nbsp;&nbsp;
751+
<input type="radio" id="dt-range" name="dateRange" value="2" />
752+
<label for="dt-range" class="control-label">Range</label>
753+
```
754+
755+
The form must be constructed as follows:
756+
757+
```php
758+
<?php
759+
760+
use Phalcon\Forms\Element\Radio;
761+
use Phalcon\Forms\Form;
762+
763+
class DatesForm extends Form
764+
{
765+
/**
766+
* Radio buttons
767+
*/
768+
$element = new Radio(
769+
'first-dt-radio', // identifier for the element in the form
770+
[
771+
'value' => '1',
772+
'checked' => '1',
773+
'name' => 'dateRange', // group name
774+
'id' => 'dt-single', // HTML id attribute
775+
]
776+
);
777+
778+
$element->setLabel('Single Date');
779+
$this->add($element);
780+
781+
$element = new Radio(
782+
'second-dt-radio', // identifier for the element in the form
783+
[
784+
'value' => '2',
785+
'checked' => '',
786+
'name' => 'dateRange', // group name
787+
'id' => 'dt-range', // HTML id attribute
788+
]
789+
);
790+
791+
$element->setLabel('Range');
792+
$this->add($element);
793+
}
794+
}
795+
```
796+
797+
Now when it is time to render the form, you can do it as follows:
798+
799+
```php
800+
// Prints the first radio button
801+
echo $form->render('first-dt-radio');
802+
echo $form->get('first-dt-radio')->label(['class' : 'control-label']);
803+
echo '&nbsp;&nbsp;&nbsp';
804+
echo $form->render('second-dt-radio');
805+
echo $form->get('second-dt-radio')->label(['class' : 'control-label']);
806+
```
807+
808+
or when using Volt:
809+
810+
```twig
811+
{{ form.render('first-dt-radio') }}
812+
{{ form.get('first-dt-radio').label(['class' : 'control-label']) }}
813+
&nbsp;&nbsp;&nbsp;
814+
{{ form.render('second-dt-radio') }}
815+
{{ form.get('second-dt-radio').label(['class' : 'control-label']) }}
816+
```
817+
740818
## Filtering
741819
A form is also able to filter data before it is validated. You can set filters in each element:
742820

0 commit comments

Comments
 (0)