Skip to content

Commit 13c3b3d

Browse files
committed
upload: test and fix upload current key
1 parent 9ab258c commit 13c3b3d

File tree

5 files changed

+30
-3
lines changed

5 files changed

+30
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
## Bugfixes
1111

12+
- File upload: `current` option using correct key.
1213
- Table: fixed group rendering for forms with only groups.
1314

1415
# 1.2.2

docs/options.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,18 @@
4444
<dd>Description of the field</dd>
4545
<dt>confirm</dt>
4646
<dd>Buttons: adds a javascript confirmation prompt before submit/click</dd>
47-
<dt>remove</dt>
48-
<dd>Upload: adds a removal checkbox (e.g. a user avatar which is set if file is uploaded but retained if nothing is sent and removed if checkbox is checked)</dd>
4947
<dt>Other</dt>
5048
<dd>All other attributes is passed directly to field, allowing custom attributes such as <code>placeholder</code>, <code>title</code>, etc.</dd>
5149
</dl>
5250

51+
<h3 id="upload-field">Upload field</h3>
52+
<dl>
53+
<dt>remove</dt>
54+
<dd>Adds a removal checkbox (e.g. a user avatar which is set if file is uploaded but retained if nothing is sent and removed if checkbox is checked)</dd>
55+
<dt>current</dt>
56+
<dd>HTML to preview the current uploaded data, e.g. <code>&lt;img src="/user/123/avatar.png"/&gt;</code></dd>
57+
</dl>
58+
5359
<h2>Serialization</h2>
5460
<p>Most attributes is serialized using these rules:</p>
5561
<ul>

src/Form.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,7 @@ public function get_error($res){
486486
return ucfirst($res->errors[$this->key][0]); /* get first error only */
487487
}
488488

489+
public function get_name(){ return $this->key; }
489490
public function get_label(){ return $this->label; }
490491
public function get_content(){ return $this->content; }
491492
public function get_hint(){ return $this->hint; }

src/lib/FormContainer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ public function upload_field($key, $label=null, array $attr=array()) {
127127
if ( $current !== false ){
128128
$attr = array();
129129
list($id, $name, $value) = $this->generate_data($key . '_current', $attr);
130-
$this->fields[] = new ManualField("{$key}_remove", '', "<label>$current</label>", false);
130+
$this->fields[] = new ManualField("{$key}_current", '', "<label>$current</label>", false);
131131
}
132132

133133
if ( $remove ){

tests/FieldTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,23 @@ public function testCustomField(){
3939
$this->assertArrayHasKey('foo', $mock->field);
4040
$this->assertEquals('email', $mock->field['foo']->attribute('type'));
4141
}
42+
43+
public function testUploadField(){
44+
$mock = new MockLayout();
45+
$form = Form::create('id', function($f){
46+
$f->upload_field('foo', 'Label');
47+
$f->upload_field('bar', 'Label', ['remove' => true]);
48+
$f->upload_field('baz', 'Label', ['current' => 'Preview']);
49+
}, ['layout' => $mock]);
50+
$this->assertArrayHasKey('foo', $mock->field);
51+
$this->assertArrayNotHasKey('foo_remove', $mock->field);
52+
$this->assertArrayNotHasKey('foo_current', $mock->field);
53+
$this->assertArrayHasKey('bar', $mock->field);
54+
$this->assertArrayHasKey('bar_remove', $mock->field);
55+
$this->assertArrayNotHasKey('bar_current', $mock->field);
56+
$this->assertArrayHasKey('baz', $mock->field);
57+
$this->assertArrayNotHasKey('baz_remove', $mock->field);
58+
$this->assertArrayHasKey('baz_current', $mock->field);
59+
$this->assertEquals('file', $mock->field['foo']->attribute('type'));
60+
}
4261
}

0 commit comments

Comments
 (0)