|
1 | | -<?php |
2 | | - |
3 | | -namespace Gustiawan\FormBuilder; |
4 | | - |
5 | | -class Form |
6 | | -{ |
7 | | - // |
8 | | -} |
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Gustiawan\FormBuilder; |
| 4 | + |
| 5 | +class Form |
| 6 | +{ |
| 7 | + public $action; |
| 8 | + public $method; |
| 9 | + public $fields = []; |
| 10 | + public $button = [ |
| 11 | + "label" => "Submit", |
| 12 | + "color" => "bg-blue-400" |
| 13 | + ]; |
| 14 | + |
| 15 | + /** |
| 16 | + * Create Form |
| 17 | + * |
| 18 | + * @param string $action action of form |
| 19 | + * @param string $method method of form |
| 20 | + * @return Form |
| 21 | + */ |
| 22 | + public function __construct($action, $method) |
| 23 | + { |
| 24 | + $this->action = $action; |
| 25 | + // need checker for method list |
| 26 | + $this->method = $method; |
| 27 | + |
| 28 | + return $this; |
| 29 | + } |
| 30 | + |
| 31 | + public function handle() |
| 32 | + { |
| 33 | + // |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * Create Input Field |
| 38 | + * |
| 39 | + * @param string $name name |
| 40 | + * @param any $value value |
| 41 | + * @param string $type type |
| 42 | + * @return Form |
| 43 | + */ |
| 44 | + public function text($name, $label, $value="") |
| 45 | + { |
| 46 | + $this->fields[] = [ |
| 47 | + "label" => $label, |
| 48 | + "type" => "text", |
| 49 | + "name" => $name, |
| 50 | + "value" => $value |
| 51 | + ]; |
| 52 | + |
| 53 | + return $this; |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Create Password Field |
| 58 | + * |
| 59 | + * @param string $name name |
| 60 | + * @param any $value value |
| 61 | + * @param string $type type |
| 62 | + * @return Form |
| 63 | + */ |
| 64 | + public function password($name, $label, $value="") |
| 65 | + { |
| 66 | + $this->fields[] = [ |
| 67 | + "label" => $label, |
| 68 | + "type" => "password", |
| 69 | + "name" => $name, |
| 70 | + "value" => $value |
| 71 | + ]; |
| 72 | + |
| 73 | + return $this; |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Create Password Field |
| 78 | + * |
| 79 | + * @param string $name name |
| 80 | + * @param any $value value |
| 81 | + * @param string $type type |
| 82 | + * @return Form |
| 83 | + */ |
| 84 | + public function date($name, $label, $value="") |
| 85 | + { |
| 86 | + $this->fields[] = [ |
| 87 | + "label" => $label, |
| 88 | + "type" => "date", |
| 89 | + "name" => $name, |
| 90 | + "value" => $value |
| 91 | + ]; |
| 92 | + |
| 93 | + return $this; |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Create Password Field |
| 98 | + * |
| 99 | + * @param string $name name |
| 100 | + * @param any $value value |
| 101 | + * @param string $type type |
| 102 | + * @return Form |
| 103 | + */ |
| 104 | + public function textArea($name, $label, $value="") |
| 105 | + { |
| 106 | + $this->fields[] = [ |
| 107 | + "label" => $label, |
| 108 | + "type" => "textarea", |
| 109 | + "name" => $name, |
| 110 | + "value" => $value |
| 111 | + ]; |
| 112 | + |
| 113 | + return $this; |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Create Password Field |
| 118 | + * |
| 119 | + * @param string $name name |
| 120 | + * @param any $value value |
| 121 | + * @param string $type type |
| 122 | + * @return Form |
| 123 | + */ |
| 124 | + public function select($name, $label, $choices, $value="") |
| 125 | + { |
| 126 | + $this->fields[] = [ |
| 127 | + "label" => $label, |
| 128 | + "type" => "select", |
| 129 | + "name" => $name, |
| 130 | + "value" => $value, |
| 131 | + "choices" => $choices |
| 132 | + ]; |
| 133 | + |
| 134 | + return $this; |
| 135 | + } |
| 136 | + |
| 137 | + public function radio($name, $label, $choices, $value="") |
| 138 | + { |
| 139 | + $this->fields[] = [ |
| 140 | + "label" => $label, |
| 141 | + "type" => "radio", |
| 142 | + "name" => $name, |
| 143 | + "value" => $value, |
| 144 | + "choices" => $choices |
| 145 | + ]; |
| 146 | + |
| 147 | + return $this; |
| 148 | + } |
| 149 | + |
| 150 | + public function checkBox($name, $label, $choices, array $value=[]) |
| 151 | + { |
| 152 | + $this->fields[] = [ |
| 153 | + "label" => $label, |
| 154 | + "type" => "checkbox", |
| 155 | + "name" => $name, |
| 156 | + "value" => $value, |
| 157 | + "choices" => $choices |
| 158 | + ]; |
| 159 | + |
| 160 | + return $this; |
| 161 | + } |
| 162 | + |
| 163 | + public function button($label="Submit", $color="bg-blue-500") |
| 164 | + { |
| 165 | + $this->button["label"] = $label; |
| 166 | + $this->button["color"] = $color; |
| 167 | + } |
| 168 | + |
| 169 | + /** |
| 170 | + * check if form need csrf |
| 171 | + * |
| 172 | + * @return boolean |
| 173 | + */ |
| 174 | + public function isNeedCsrf() |
| 175 | + { |
| 176 | + if ($this->method == "GET") { |
| 177 | + return false; |
| 178 | + } |
| 179 | + |
| 180 | + return true; |
| 181 | + } |
| 182 | +} |
0 commit comments