diff --git a/index.php b/index.php index 7464902..b9ea55c 100644 --- a/index.php +++ b/index.php @@ -28,9 +28,9 @@ echo (new Form('index.php')) ->persistWith($_POST) // data to repopulate ->shortTags(['pl' => 'placeholder']) // replace all pl in element attribute as placeholder - ->checkbox($name, 'Check', $attributes_arr) + ->checkbox($name, $attributes_arr, 'Check') ->hidden($name, $attributes_arr) - ->email($name, $attributes_arr, $label)->val('foo@bar.com') + ->email('email_address', $attributes_arr, $label)->val('foo@bar.com') ->password($name, $attributes_arr, $label) ->text($name, $attributes_arr, $label) ->color($name, $attributes_arr, $label) @@ -40,7 +40,7 @@ ->image($name, $source, $attributes_arr, $label) ->month($name, $attributes_arr, $label) ->number($name, $attributes_arr, $label) - ->radio($name, 'Click', $attributes_arr) + ->radio($name, $attributes_arr, 'Click') ->range($name, $attributes_arr, $label) ->reset($attributes_arr) ->search($name, $attributes_arr, $label) @@ -52,8 +52,14 @@ ->datalist($name, range(1, 10), $attributes_arr) ->textarea($name, $attributes_arr, $label) ->output($name, $attributes_arr, $label) + ->repeatable(2, function (Form $form) { + $form->html('
') + ->text('name[]') + ->select('range[]', range(1, 3), [], true, true) + ->html('
'); + }) ->label('custom', 'your custom input') ->text('remove') - ->progress($value, $max_val)->move('email') + ->progress($value, $max_val)->move('email_address') ->remove('remove') ->bSubmit("Submit"); diff --git a/src/Form.php b/src/Form.php index 172b240..4b49975 100644 --- a/src/Form.php +++ b/src/Form.php @@ -41,6 +41,10 @@ namespace Bittr; +use Closure; +use Exception; +use Throwable; + class Form { /** @var array */ @@ -90,7 +94,7 @@ public function __construct(string $action = null, string $method = null, array * Set behaviour attributes. * * @param array $persist_value - * @return \Form + * @return Form */ public function persistWith(array $persist_value): Form { @@ -317,7 +321,7 @@ public function month(string $name, array $attr = [], bool $label = true): Form * @param array $options * @param array $attr * @param bool $label - * @return \Form + * @return Form */ public function datalist(string $name, array $options, array $attr = [], bool $label = true): Form { @@ -478,6 +482,7 @@ public function week(string $name, array $attr = [], bool $label = true): Form * @param array $options * @param array $attr * @param bool $label + * @param bool $use_key_as_value * @return Form */ public function select( @@ -503,11 +508,11 @@ public function select( } $this->buffer[] = $attr + [ - 'name' => $name, - 'tag' => 'select', - 'options' => [$options, $selected, $disabled, $use_key_as_value], - 'l' => $label - ]; + 'name' => $name, + 'tag' => 'select', + 'options' => [$options, $selected, $disabled, $use_key_as_value], + 'l' => $label + ]; return $this; } @@ -674,7 +679,7 @@ public function html(string $element): Form * Adds short replacement for HTML attributes. * * @param array $array - * @return \Form + * @return Form */ public function shortTags(array $array): Form { @@ -683,6 +688,23 @@ public function shortTags(array $array): Form return $this; } + /** + * Repeats a set of methods for a giving multiplier. + * + * @param int $multiplier + * @param \Closure $repeat + * @return Form + */ + public function repeatable(int $multiplier, Closure $repeat): Form + { + while ($multiplier--) + { + $repeat($this); + } + + return $this; + } + /** * Make defined selected option attribute from array. * @@ -702,10 +724,11 @@ private function makeOpt(array $params): string } $attr .= "\n\t