Skip to content

Commit 2636c4e

Browse files
author
Wazabii
committed
Radio and chckbox type
1 parent f8548ce commit 2636c4e

File tree

3 files changed

+85
-84
lines changed

3 files changed

+85
-84
lines changed

AbstractFormFields.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,11 +139,10 @@ public function radio(): string
139139
{
140140
return $this->container(function () {
141141
$out = "";
142-
$typeAdd = (isset($this->attrArr['type']) ? null : "type=\"radio\" ");
143142
foreach ($this->items as $val => $item) {
144143
$checked = ($this->isChecked($val)) ? "checked=\"checked\" " : null;
145144
$out .= "<label class=\"radio item small\">";
146-
$out .= "<input {$checked}{$typeAdd}{$this->attr}name=\"{$this->name}\" value=\"{$val}\"><span class=\"title\">{$item}</span>";
145+
$out .= "<input {$checked}type=\"radio\" {$this->attr}name=\"{$this->name}\" value=\"{$val}\"><span class=\"title\">{$item}</span>";
147146
$out .= "</label>";
148147
}
149148
return $out;
@@ -160,13 +159,12 @@ public function checkbox(): string
160159

161160
$out = "";
162161
$length = count($this->items);
163-
$typeAdd = (isset($this->attrArr['type']) ? null : "type=\"checkbox\" ");
164162

165163
foreach ($this->items as $val => $item) {
166164
$name = ($length > 1) ? "{$this->name}[]" : $this->name;
167165
$checked = ($this->isChecked($val)) ? "checked=\"checked\" " : null;
168166
$out .= "<label class=\"checkbox item small\">";
169-
$out .= "<input {$checked}{$typeAdd}{$this->attr}name=\"{$name}\" value=\"{$val}\"><span class=\"title\">{$item}</span>";
167+
$out .= "<input {$checked}type=\"checkbox\" {$this->attr}name=\"{$name}\" value=\"{$val}\"><span class=\"title\">{$item}</span>";
170168
$out .= "</label>";
171169
}
172170
return $out;

Fields.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ public function getValues(): mixed
7676

7777
/**
7878
* Create form
79-
* @param string $name Form name
8079
* @param array $fields
80+
* @param string $name Form name
8181
*/
8282
public function add($fields, ?string $name = null): self
8383
{

README.md

Lines changed: 82 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ $fields->[FIELD_TYPE]->[ARG1]->[ARG2]->get();
1515
**FIELD_TYPE:** Method name from Form\Templates\Fields
1616
**ARG:** Chainable arguments like input name, fields attributes, validations and so on.
1717
```php
18-
echo $fields->text()->name("email")->attr(["type" => "email"])->get();
18+
echo $fields->text()->name("email")->label("Email address")->attr([
19+
"type" => "email",
20+
"placeholder" => "Input your email..."
21+
])->get();
1922
```
2023
## Advance:
2124
Use the form compiler for advance consistent form creation and validation. Works really great in frameworks and large applications.
@@ -91,89 +94,89 @@ Pass on custom data for a custom field.
9194
#### 1. Create form with array
9295
Build a whole form with array as bellow
9396
```php
94-
$fields->add("userForm", [
95-
"firstname" => [
96-
"type" => "text", // Set form type (input text or textarea and so on.)
97-
"label" => "First name",
98-
"validate" => [
99-
"length" => [1, 80]
100-
]
101-
],
102-
"lastname" => [
103-
"type" => "text",
104-
"label" => "Last name",
105-
"validate" => [
106-
"length" => [1, 120]
107-
]
108-
],
109-
"email" => [
110-
"type" => "text",
111-
"label" => "Email",
112-
"description" => "We need you email so that we can contact you.",
113-
"attr" => [
114-
"type" => "email",
115-
"placeholder" => "john.doe@hotmail.com"
116-
],
117-
"validate" => [
118-
"length" => [1, 120]
119-
"!email" => NULL
120-
]
121-
],
122-
"nested,item1" => [
123-
"type" => "radio",
124-
"label" => "Question 1",
125-
"validate" => [
126-
"length" => [1],
127-
],
128-
"items" => [
129-
1 => "Yes",
130-
0 => "No"
131-
],
132-
"value" => 1 // Default value
133-
],
134-
"nested,item2" => [
135-
"type" => "radio",
136-
"label" => "Question 2",
137-
"validate" => [
138-
"length" => [1],
139-
],
140-
"items" => [
141-
1 => "Yes",
142-
0 => "No"
143-
],
144-
"value" => 1 // Default value
145-
],
146-
"message" => [
147-
"type" => "textarea",
148-
"label" => "Message",
149-
"validate" => [
150-
"length" => [0, 2000]
151-
]
152-
],
153-
"gdpr" => [
154-
"type" => "checkbox",
155-
//"label" => "GDPR",
156-
"validate" => [
157-
"length" => [1, 1],
158-
"!equal" => [1]
159-
],
160-
"items" => [
161-
1 => "I accept that my data will be saved according to GDPR"
162-
]
163-
]
164-
97+
$fields->add([
98+
"firstname" => [
99+
"type" => "text", // Set form type (input text or textarea and so on.)
100+
"label" => "First name",
101+
"validate" => [
102+
"length" => [1, 80]
103+
]
104+
],
105+
"lastname" => [
106+
"type" => "text",
107+
"label" => "Last name",
108+
"validate" => [
109+
"length" => [1, 120]
110+
]
111+
],
112+
"email" => [
113+
"type" => "text",
114+
"label" => "Email",
115+
"description" => "We need you email so that we can contact you.",
116+
"attr" => [
117+
"type" => "email",
118+
"placeholder" => "john.doe@hotmail.com"
119+
],
120+
"validate" => [
121+
"length" => [1, 120],
122+
"!email" => NULL
123+
]
124+
],
125+
"nested,item1" => [
126+
"type" => "radio",
127+
"label" => "Question 1",
128+
"validate" => [
129+
"length" => [1],
130+
],
131+
"items" => [
132+
1 => "Yes",
133+
0 => "No"
134+
],
135+
"value" => 1 // Default value
136+
],
137+
"nested,item2" => [
138+
"type" => "radio",
139+
"label" => "Question 2",
140+
"validate" => [
141+
"length" => [1],
142+
],
143+
"items" => [
144+
1 => "Yes",
145+
0 => "No"
146+
],
147+
"value" => 1 // Default value
148+
],
149+
"message" => [
150+
"type" => "textarea",
151+
"label" => "Message",
152+
"validate" => [
153+
"length" => [0, 2000]
154+
]
155+
],
156+
"gdpr" => [
157+
"type" => "checkbox",
158+
//"label" => "GDPR",
159+
"validate" => [
160+
"length" => [1, 1],
161+
"!equal" => [1]
162+
],
163+
"items" => [
164+
1 => "I accept that my data will be saved according to GDPR"
165+
]
166+
]
167+
165168
]);
166169
```
167170
#### 2. Set values if you want
168171
If you have values from for example the database (accepts multidimensional array and object)
169172
```php
170173
$fields->setValues([
171-
"firstname" => "John",
172-
"lastname" => "John",
173-
"nested" => [
174-
"item1" => 0,
175-
"item2" => 1,
176-
]
174+
"firstname" => "John",
175+
"lastname" => "John",
176+
"nested" => [
177+
"item1" => 0,
178+
"item2" => 1,
179+
]
177180
]);
178181

179182
```
@@ -186,7 +189,7 @@ $fields->build();
186189
Now you can read the form.
187190
```php
188191
echo '<form action="index.php" method="post">';
189-
echo $fields->getForm("userForm");
192+
echo $fields->getForm();
190193
echo "</form>";
191194
```
192195
#### 5. Validate form

0 commit comments

Comments
 (0)