Skip to content

Commit 068d229

Browse files
committed
move rest of fields to separate files
1 parent 7aa094a commit 068d229

File tree

6 files changed

+145
-130
lines changed

6 files changed

+145
-130
lines changed

src/Form.php

Lines changed: 0 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -376,133 +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 get_addons(){ return [false, false]; }
500-
public function layout_hints(){ return 0; }
501-
public function get_id() { return false; }
502-
public function set_container($container){ $this->container = $container; }
503-
public function get_container(){ return $this->container; }
504-
505-
public function attribute($key, $default=false){
506-
return $default; /* no sane way to get attributes from manual fields */
507-
}
508-
}

src/lib/HintField.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace NitroXy\PHPForms;
4+
5+
class HintField implements FormField {
6+
private $text = null;
7+
private $label = null;
8+
private $attr = array('class' => 'form-hint');
9+
protected $container = null;
10+
11+
public function __construct($text, $label, $attr){
12+
$this->text = $text;
13+
$this->label = $label;
14+
$this->attr = array_merge($this->attr, $attr);
15+
}
16+
17+
public function render($layout, $res) {
18+
$layout->render_hint($this);
19+
}
20+
21+
public function get_hint(){ return false; }
22+
public function get_content(){ return $this->text; }
23+
public function get_label(){ return $this->label; }
24+
public function layout_hints(){ return 0; }
25+
public function get_id() { return false; }
26+
public function set_container($container){ $this->container = $container; }
27+
public function get_container(){ return $this->container; }
28+
29+
public function attribute($key, $default=false){
30+
return array_key_exists($key, $this->attr) ? $this->attr[$key] : $default;
31+
}
32+
}

src/lib/LinkField.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace NitroXy\PHPForms;
4+
5+
class LinkField extends StaticField {
6+
protected $href = null;
7+
8+
public function __construct($label, $attr){
9+
$this->pop_attr('text', $attr, $text);
10+
parent::__construct($text, $label, $attr);
11+
}
12+
13+
public function render($layout, $res) {
14+
$layout->render_static($this);
15+
}
16+
17+
public function get_content(array $extra_attr = array()){
18+
$attr = array_merge_recursive($extra_attr, $this->attr);
19+
$this->pop_attr('icon', $attr, $icon); /* layout reads icon data, puts html back into attr */
20+
return "<a " . $this->serialize_attr($attr) . ">$icon{$this->text}</a>";
21+
}
22+
23+
public function get_hint(){ return false; }
24+
public function get_label(){ return $this->label; }
25+
public function layout_hints(){ return 0; }
26+
public function get_id() { return false; }
27+
}

src/lib/ManualField.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace NitroXy\PHPForms;
4+
5+
class ManualField implements FormField {
6+
private $key = null;
7+
private $label = null;
8+
private $content = null;
9+
private $hint = null;
10+
protected $container = null;
11+
12+
public function __construct($key, $label, $content, $hint){
13+
$this->key = $key;
14+
$this->label = $label;
15+
$this->content = $content;
16+
$this->hint = $hint;
17+
}
18+
19+
public function render($layout, $res) {
20+
$layout->render_field($this, $this->get_error($res));
21+
}
22+
23+
public function get_error($res){
24+
if ( !($this->key && isset($res->errors[$this->key])) ) return false;
25+
return ucfirst($res->errors[$this->key][0]); /* get first error only */
26+
}
27+
28+
public function get_name(){ return $this->key; }
29+
public function get_label(){ return $this->label; }
30+
public function get_content(){ return $this->content; }
31+
public function get_hint(){ return $this->hint; }
32+
public function get_addons(){ return [false, false]; }
33+
public function layout_hints(){ return 0; }
34+
public function get_id() { return false; }
35+
public function set_container($container){ $this->container = $container; }
36+
public function get_container(){ return $this->container; }
37+
38+
public function attribute($key, $default=false){
39+
return $default; /* no sane way to get attributes from manual fields */
40+
}
41+
}

src/lib/StaticField.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace NitroXy\PHPForms;
4+
5+
class StaticField extends FormInput {
6+
protected $text = null;
7+
8+
public function __construct($text, $label, $attr){
9+
parent::__construct(false, false, false, null, null, $label, $attr);
10+
$this->text = $text;
11+
}
12+
13+
public function render($layout, $res) {
14+
$layout->render_static($this);
15+
}
16+
17+
public function get_content(array $extra_attr = array()){
18+
$attr = array_merge_recursive($extra_attr, $this->attr);
19+
$this->pop_attr('icon', $attr, $icon); /* layout reads icon data, puts html back into attr */
20+
return $icon . $this->text;
21+
}
22+
23+
public function get_hint(){ return false; }
24+
public function get_label(){ return $this->label; }
25+
public function layout_hints(){ return 0; }
26+
public function get_id() { return false; }
27+
}

src/lib/TextAreaField.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace NitroXy\PHPForms;
4+
5+
class TextAreaField extends FormInput {
6+
private $value = '';
7+
8+
public function __construct($key, $id, $name, $value, $label, $attr) {
9+
parent::__construct($key, $id, $name, null, null, $label, $attr);
10+
$this->value = $value;
11+
}
12+
13+
public function get_content(array $extra_attr = array()){
14+
$attr = array_merge_recursive($extra_attr, $this->attr);
15+
$value = htmlspecialchars($this->value);
16+
return "<textarea " . $this->serialize_attr($attr) . " >{$value}</textarea>";
17+
}
18+
}

0 commit comments

Comments
 (0)