Skip to content

Commit e902ca1

Browse files
committed
form: arbitrary options
1 parent a4c4133 commit e902ca1

File tree

5 files changed

+25
-6
lines changed

5 files changed

+25
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@
1010

1111
## Features
1212

13+
- Form: allow arbitrary attributes to be passed to form.
1314
- Styling: checkboxes uses `form-checkbox` class.
1415
- Styling: all layouts now adds `form-group` class.
1516
- Styling: required fields get a `required` class.
1617

17-
1818
# 1.2.3
1919

2020
## Features

docs/options.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
<dd>Form layout ("plain", "table", "bootstrap" or an instance of <code>FormLayout</code>)</dd>
3131
<dt>prefix</dt>
3232
<dd>Use custom prefix when generating names and ID</dd>
33+
<dt>attr</dt>
34+
<dd>Array with arbitrary attributes to pass directly to form.</dd>
3335
<dt>Other</dt>
3436
<dd>Form also accepts <code>style</code>, <code>class</code> and <code>data</code> which is just passed directly to the form.</dd>
3537
</dl>

src/Form.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@ class Form extends FormContainer {
1414
'layout' => 'table', /* layout engine, one of {plain, table, bootstrap, unbuffered} or a class extending FormLayout. If unbuffered fields is written directly. */
1515
'prefix' => false, /* use a custom prefix in front of all names, default is nothing for arrays and class name for objects */
1616
'style' => '', /* add custom style to form, e.g. width */
17-
'class' => array(), /* additional classes (accepts string or array) */
18-
'data' => array(), /* extra data attributes */
17+
'class' => [], /* additional classes (accepts string or array) */
18+
'data' => [], /* extra data attributes */
19+
'attr' => [], /* extra arbitrary attributes */
1920
);
2021

2122
public $id = "";
22-
public $attr = array('class' => array('form'));
23+
public $attr = ['class' => ['form']];
2324

2425
private $res = null;
2526
private $name_pattern = '%s';
@@ -165,6 +166,11 @@ private function parse_options($user){
165166
if ( $options['prefix'] ) $this->name_pattern = $options['prefix'] . '[%s]';
166167
if ( $options['enctype'] ) $this->attr['enctype'] = $options['enctype'];
167168
if ( $options['data'] ) $this->attr['data'] = $options['data'];
169+
170+
/* arbitrary options */
171+
if ( $options['attr'] ){
172+
$this->attr = array_merge($this->attr, $options['attr']);
173+
}
168174
}
169175

170176
private function parse_method($method){

tests/DOMParser.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ protected function onNotSuccessfulTest(Exception $e){
2121
throw $e;
2222
}
2323

24-
protected function generate($func){
24+
protected function generate($func, array $options=[]){
2525
ob_start();
26-
Form::create('id', $func, ['layout' => 'table']);
26+
Form::create('id', $func, array_merge(['layout' => 'table'], $options));
2727
$this->html = ob_get_contents();
2828
ob_end_clean();
2929
}

tests/LayoutTableTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,17 @@ public function testHiddenField(){
1414
]);
1515
}
1616

17+
public function testFormArbitraryAttribute(){
18+
$this->generate(function($f){
19+
$f->hidden_field('foo', '1');
20+
}, ['attr' => ['foo' => 'bar']]);
21+
$this->validate([
22+
['form_start', ['foo' => 'bar']],
23+
['input', ['type' => 'hidden', 'name' => 'foo', 'value' => 1]],
24+
['form_end'],
25+
]);
26+
}
27+
1728
public function testTextField(){
1829
$this->generate(function($f){
1930
$f->text_field('foo', 'Test field');

0 commit comments

Comments
 (0)