Skip to content

Commit e9bf1cb

Browse files
committed
Merge branch 'addons'
2 parents 9b92e78 + 21d062f commit e9bf1cb

19 files changed

+314
-149
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# 1.3.0
2+
3+
## Features
4+
5+
- Form: support addons: prefix and suffix.
6+
17
# 1.2.4
28

39
## Breaking changes

docs/index.php

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,6 @@
9595
<?php include('overview/objects.php'); ?>
9696
</div>
9797

98-
<h2 id="controls">Supported controls</h2>
99-
<p>The basic form of all fields is <code>$f->field($key, $label, $options);</code></p>
100-
<?php display('overview/controls.php'); ?>
101-
<div class="example">
102-
<?php include('overview/controls.php'); ?>
103-
</div>
104-
10598
<h2 id="nested" >Nested data/"forms"</h2>
10699
<p>Forms can be nested by using <code>fields_for</code> to allow either sending to unrelated objects or multiple instances of the same class.</p>
107100
<?php display('overview/nested.php'); ?>

docs/layout.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@
1010
'*' => 'layout/groups.php',
1111
],
1212
'Checkboxes' => 'layout/checkboxes.php',
13+
'Addons' => 'layout/addons.php',
1314
'Layout hints' => [
1415
'table' => 'layout/layout_hints.php',
1516
'*' => false,
1617
],
18+
'Supported controls' => 'layout/smoke.php',
1719
];
1820

1921
require '../vendor/autoload.php';

docs/layout/addons.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php /*~*/
2+
use NitroXy\PHPForms\Form;
3+
global $layout;
4+
$data = ['a' => 'Value 1', 'b' => 'Value 2', 'c' => 'Value 3', 'd' => 'Value 4'];
5+
?>
6+
<?php
7+
Form::from_array('example_prefix_suffix', $data, function($f){
8+
$f->text_field('a', 'Text 1');
9+
$f->text_field('b', 'Text 2', ['prefix' => 'Prefix']);
10+
$f->text_field('c', 'Text 3', ['suffix' => 'Suffix']);
11+
$f->text_field('d', 'Text 4', ['prefix' => 'Prefix', 'suffix' => 'Suffix']);
12+
}, ['layout' => $layout]);

docs/overview/controls.php renamed to docs/layout/smoke.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
<?php /*~*/
22
use NitroXy\PHPForms\Form;
33
use NitroXy\PHPForms\FormSelect;
4+
global $layout;
45
?>
56
<?php
67

7-
Form::create('example5', function($f){
8+
Form::create('smoketest', function($f){
89
$f->hidden_field('name', 'Hidden (this wont show at all)', ['value' => '8']);
910
$f->text_field('text_field', 'Text', ['hint' => 'Use the "type" option to use custom type such as number.']);
1011
$f->password_field('password_field', 'Password', ['hint' => 'Passwords are not persistent if autocomplete is off', 'autocomplete' => 'off']);
@@ -33,4 +34,4 @@
3334
$f->text_field('text2', 'Input 2');
3435
$f->text_field('text3', 'Input 3');
3536
});
36-
}, ['layout' => 'bootstrap']);
37+
}, ['layout' => $layout]);

docs/style.css

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@
1414

1515
.form.table .form-field {
1616
padding-right: 10px;
17+
width: 200px;
18+
}
19+
20+
.form.table input:not([type="radio"]):not([type="checkbox"]) {
21+
width: 100%;
1722
}
1823

1924
.form.table .form-hint {
@@ -26,6 +31,17 @@
2631
width: 100%;
2732
}
2833

34+
.form.table .form-addon {
35+
display: inline-table;
36+
}
37+
38+
.form.table .form-suffix, .form.table .form-prefix {
39+
display: table-cell;
40+
width: auto;
41+
background: #eee;
42+
padding: 0 8px;
43+
}
44+
2945
.required label:after {
3046
content: '*';
3147
color: #a00;

docs/usage.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,34 @@ static protected function default_options(){
8787
margin-right: 15px;
8888
}
8989
EOT
90+
, 'css'); ?></td>
91+
</tr>
92+
<tr>
93+
<td><code>.form-group</code></td>
94+
<td>Class added to row wrapper for for groups.</td>
95+
<td></td>
96+
</tr>
97+
<tr>
98+
<td><code>.form-addon</code></td>
99+
<td>Class wrapping addons and field when addons are used.</td>
100+
<td><?php code(<<<EOT
101+
/* suggested structural style */
102+
.form-addon {
103+
display: inline-table;
104+
}
105+
EOT
106+
, 'css'); ?></td>
107+
</tr>
108+
<tr>
109+
<td><code>.form-prefix, .form-suffix</code></td>
110+
<td>Class added to addons.</td>
111+
<td><?php code(<<<EOT
112+
/* suggested structural style */
113+
.form-prefix, .form-suffix {
114+
display: table-cell;
115+
width: auto;
116+
}
117+
EOT
90118
, 'css'); ?></td>
91119
</tr>
92120
<tr>

src/Form.php

Lines changed: 0 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -376,132 +376,3 @@ public function generate_data($key, array &$attr){
376376
return array($id, $name, $value);
377377
}
378378
}
379-
380-
class TextAreaField extends FormInput {
381-
private $value = '';
382-
383-
public function __construct($key, $id, $name, $value, $label, $attr) {
384-
parent::__construct($key, $id, $name, null, null, $label, $attr);
385-
$this->value = $value;
386-
}
387-
388-
public function get_content(array $extra_attr = array()){
389-
$attr = array_merge_recursive($extra_attr, $this->attr);
390-
$value = htmlspecialchars($this->value);
391-
return "<textarea " . $this->serialize_attr($attr) . " >{$value}</textarea>";
392-
}
393-
}
394-
395-
class StaticField extends FormInput {
396-
protected $text = null;
397-
398-
public function __construct($text, $label, $attr){
399-
parent::__construct(false, false, false, null, null, $label, $attr);
400-
$this->text = $text;
401-
}
402-
403-
public function render($layout, $res) {
404-
$layout->render_static($this);
405-
}
406-
407-
public function get_content(array $extra_attr = array()){
408-
$attr = array_merge_recursive($extra_attr, $this->attr);
409-
$this->pop_attr('icon', $attr, $icon); /* layout reads icon data, puts html back into attr */
410-
return $icon . $this->text;
411-
}
412-
413-
public function get_hint(){ return false; }
414-
public function get_label(){ return $this->label; }
415-
public function layout_hints(){ return 0; }
416-
public function get_id() { return false; }
417-
}
418-
419-
class LinkField extends StaticField {
420-
protected $href = null;
421-
422-
public function __construct($label, $attr){
423-
$this->pop_attr('text', $attr, $text);
424-
parent::__construct($text, $label, $attr);
425-
}
426-
427-
public function render($layout, $res) {
428-
$layout->render_static($this);
429-
}
430-
431-
public function get_content(array $extra_attr = array()){
432-
$attr = array_merge_recursive($extra_attr, $this->attr);
433-
$this->pop_attr('icon', $attr, $icon); /* layout reads icon data, puts html back into attr */
434-
return "<a " . $this->serialize_attr($attr) . ">$icon{$this->text}</a>";
435-
}
436-
437-
public function get_hint(){ return false; }
438-
public function get_label(){ return $this->label; }
439-
public function layout_hints(){ return 0; }
440-
public function get_id() { return false; }
441-
}
442-
443-
class HintField implements FormField {
444-
private $text = null;
445-
private $label = null;
446-
private $attr = array('class' => 'form-hint');
447-
protected $container = null;
448-
449-
public function __construct($text, $label, $attr){
450-
$this->text = $text;
451-
$this->label = $label;
452-
$this->attr = array_merge($this->attr, $attr);
453-
}
454-
455-
public function render($layout, $res) {
456-
$layout->render_hint($this);
457-
}
458-
459-
public function get_hint(){ return false; }
460-
public function get_content(){ return $this->text; }
461-
public function get_label(){ return $this->label; }
462-
public function layout_hints(){ return 0; }
463-
public function get_id() { return false; }
464-
public function set_container($container){ $this->container = $container; }
465-
public function get_container(){ return $this->container; }
466-
467-
public function attribute($key, $default=false){
468-
return array_key_exists($key, $this->attr) ? $this->attr[$key] : $default;
469-
}
470-
}
471-
472-
class ManualField implements FormField {
473-
private $key = null;
474-
private $label = null;
475-
private $content = null;
476-
private $hint = null;
477-
protected $container = null;
478-
479-
public function __construct($key, $label, $content, $hint){
480-
$this->key = $key;
481-
$this->label = $label;
482-
$this->content = $content;
483-
$this->hint = $hint;
484-
}
485-
486-
public function render($layout, $res) {
487-
$layout->render_field($this, $this->get_error($res));
488-
}
489-
490-
public function get_error($res){
491-
if ( !($this->key && isset($res->errors[$this->key])) ) return false;
492-
return ucfirst($res->errors[$this->key][0]); /* get first error only */
493-
}
494-
495-
public function get_name(){ return $this->key; }
496-
public function get_label(){ return $this->label; }
497-
public function get_content(){ return $this->content; }
498-
public function get_hint(){ return $this->hint; }
499-
public function layout_hints(){ return 0; }
500-
public function get_id() { return false; }
501-
public function set_container($container){ $this->container = $container; }
502-
public function get_container(){ return $this->container; }
503-
504-
public function attribute($key, $default=false){
505-
return $default; /* no sane way to get attributes from manual fields */
506-
}
507-
}

src/lib/FormContainer.php

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,16 @@ public function __construct($form){
1212

1313
/**
1414
* Generates any kind of input, used by most other fields. Note that
15-
* this function should not be called directly, use
16-
* <code>custom_field()</code> instead. Common options for all
17-
* fields:
15+
* this function should not be called directly, use
16+
* <code>custom_field()</code> instead. Common options for all
17+
* fields:
1818
*
1919
* @option 'value' {string} If value is set as an attribute it
2020
* forces the value (irregardless of the object).
21-
* @option 'required' {boolean} Set field to required (using HTML
21+
* @option 'required' {boolean} Set field to required (using HTML
2222
* <code>required</code> attribute).
23+
* @option 'prefix' {string} Add a prefix addon.
24+
* @option 'suffix' {string} Add a suffix addon.
2325
*/
2426
public function factory($type, $key, $label=null, array $attr=array()){
2527
if ( $label && !is_string($label) ){
@@ -129,7 +131,7 @@ public function manual($key, $label, $content, $hint){
129131
*
130132
* @option remove {boolean} If true a checkbox to remove the current value will be added.
131133
* @option current {html} If set to non-false the content will be displayed as the
132-
* current value, e.g can be set to <img ..> to display the
134+
* current value, e.g can be set to &lt;img ..&gt; to display the
133135
* current uploaded image.
134136
*/
135137
public function upload_field($key, $label=null, array $attr=array()) {
@@ -225,10 +227,10 @@ public function link($text, $href, $label=false, array $attr=array()){
225227
* Create textarea.
226228
*
227229
* @option 'tworow' {boolean} Layout hint to use two rows having the
228-
* label on one row label and the textfield below.
230+
* label on one row label and the textfield below.
229231
* @option 'fill' {boolean} Layout hint used together with
230-
* <code>'tworow'</code> to fill the entire row (not just
231-
* label + content).
232+
* <code>'tworow'</code> to fill the entire row (not just
233+
* label + content).
232234
*/
233235
public function textarea($key, $label=null, array $attr=array()){
234236
$this->fields[] = $this->factory('textarea', $key, $label, $attr);

src/lib/FormInput.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ class FormInput implements FormField {
1010
protected $tworow = 0;
1111
protected $fill = 0;
1212
protected $icon = false;
13+
protected $prefix = false;
14+
protected $suffix = false;
1315
protected $container = null;
1416
protected $attr = array();
1517

@@ -34,6 +36,8 @@ public function __construct($key, $id, $name, $value, $type, $label, $attr) {
3436
$this->pop_attr('tworow', $attr, $this->tworow);
3537
$this->pop_attr('fill', $attr, $this->fill);
3638
$this->pop_attr('icon', $attr, $this->icon);
39+
$this->pop_attr('prefix', $attr, $this->prefix);
40+
$this->pop_attr('suffix', $attr, $this->suffix);
3741

3842
if ( $value !== null ) $attr['value'] = $value;
3943
if ( $id != null ) $attr['id'] = $id;
@@ -84,6 +88,10 @@ public function get_icon(){
8488
return $this->icon;
8589
}
8690

91+
public function get_addons(){
92+
return [$this->prefix, $this->suffix];
93+
}
94+
8795
public function get_value(){
8896
return array_key_exists('value', $this->attr) ? $this->attr['value'] : null;
8997
}

src/lib/FormLayoutBootstrap.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ public function render_field($field, $error){
1111
$hint = $field->get_hint();
1212
$required = $field->attribute('required');
1313

14+
/* addons */
15+
list($prefix, $suffix) = $field->get_addons();
16+
$have_addon = (boolean)($prefix || $suffix);
17+
if ( $prefix ) $prefix = "<div class=\"input-group-addon\">{$prefix}</div>";
18+
if ( $suffix ) $suffix = "<div class=\"input-group-addon\">{$suffix}</div>";
19+
1420
$class = ['form-group'];
1521
if ( $error ){
1622
$class[] = 'has-error';
@@ -40,7 +46,11 @@ public function render_field($field, $error){
4046
if ( $label ){
4147
echo " <label for=\"$id\" class=\"control-label\">{$label}</label>";
4248
}
43-
echo " $content";
49+
if ( $have_addon ){
50+
echo " <div class=\"input-group\">{$prefix}{$content}{$suffix}</div>";
51+
} else {
52+
echo " $content";
53+
}
4454
if ( $hint ){
4555
echo " <span class=\"help-block\">$hint</span>";
4656
}

src/lib/FormLayoutPlain.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,22 @@ public function render_field($field, $error){
1717

1818
$row_attr = FormUtils::serialize_attr(['class' => $class]);
1919

20+
if ( $field instanceof FormInput ){
21+
list($prefix, $suffix) = $field->get_addons();
22+
$have_addon = (boolean)($prefix || $suffix);
23+
if ( $prefix ) $prefix = "<span class=\"form-prefix\">{$prefix}</span>";
24+
if ( $suffix ) $suffix = "<span class=\"form-suffix\">{$suffix}</span>";
25+
} else {
26+
$have_addon = false;
27+
}
28+
2029
echo "<div {$row_attr}>\n";
2130
if ( $label !== false ) echo "<span class=\"form-label\">$label</span>\n";
22-
if ( $content !== false) echo "<span class=\"form-field\">$content</span>\n";
31+
if ( $have_addon ){
32+
echo "<span class=\"form-field\"><span class=\"form-addon\">{$prefix}{$content}{$suffix}</span></span>\n";
33+
} else if ( $content !== false ){
34+
echo "<span class=\"form-field\">$content</span>\n";
35+
}
2336
if($error !== false) echo "<span class=\"form-error\">$error</span>\n";
2437
if($hint !== false) echo "<span class=\"form-hint\">$hint</span>\n";
2538
echo '</div>';

0 commit comments

Comments
 (0)