<\/div>/i", $this->suffix)) {
121 | $this->suffix = "
" .
122 | (($this->with_val == true) ? "
{$text}
" : '') .
123 | $this->suffix;
124 | }
125 | return parent::renderField($form);
126 | }
127 | }
128 |
--------------------------------------------------------------------------------
/src/classes/fields/Spinner.php:
--------------------------------------------------------------------------------
1 |
9 | * @license MIT https://opensource.org/licenses/mit-license.php
10 | * @link https://github.com/degami/php-forms-api
11 | */
12 | /* #########################################################
13 | #### FIELDS ####
14 | ######################################################### */
15 |
16 | namespace Degami\PHPFormsApi\Fields;
17 |
18 | use Degami\PHPFormsApi\Form;
19 |
20 | /**
21 | * The spinner number input field class
22 | */
23 | class Spinner extends Number
24 | {
25 | /**
26 | * {@inheritdoc}
27 | *
28 | * @param Form $form form object
29 | */
30 | public function preRender(Form $form)
31 | {
32 | if ($this->pre_rendered == true) {
33 | return;
34 | }
35 | $id = $this->getHtmlId();
36 |
37 | $js_options = '';
38 | if (is_numeric($this->min) && is_numeric($this->max) && $this->max >= $this->min) {
39 | $js_options = "{min: $this->min, max: $this->max, step: $this->step}";
40 | }
41 |
42 | $this->addJs("\$('#{$id}','#{$form->getId()}').attr('type','text').spinner({$js_options});");
43 |
44 | parent::preRender($form);
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/src/classes/fields/Submit.php:
--------------------------------------------------------------------------------
1 |
9 | * @license MIT https://opensource.org/licenses/mit-license.php
10 | * @link https://github.com/degami/php-forms-api
11 | */
12 | /* #########################################################
13 | #### FIELDS ####
14 | ######################################################### */
15 |
16 | namespace Degami\PHPFormsApi\Fields;
17 |
18 | use Degami\Basics\Html\BaseElement;
19 | use Degami\PHPFormsApi\Form;
20 | use Degami\Basics\Html\TagElement;
21 | use Degami\PHPFormsApi\Abstracts\Fields\Clickable;
22 |
23 | /**
24 | * The submit input type field class
25 | */
26 | class Submit extends Clickable
27 | {
28 | /**
29 | * {@inheritdoc}
30 | *
31 | * @param Form $form form object
32 | * @return string|BaseElement the element html
33 | */
34 | public function renderField(Form $form)
35 | {
36 | $id = $this->getHtmlId();
37 | if (empty($this->value)) {
38 | $this->value = 'Submit';
39 | }
40 | if ($this->disabled == true) {
41 | $this->attributes['disabled']='disabled';
42 | }
43 |
44 | $tag = new TagElement([
45 | 'tag' => 'input',
46 | 'type' => 'submit',
47 | 'id' => $id,
48 | 'name' => $this->name,
49 | 'value' => $this->getText($this->getValues()),
50 | 'attributes' => $this->attributes,
51 | ]);
52 | return $tag;
53 | }
54 | }
55 |
--------------------------------------------------------------------------------
/src/classes/fields/Switchbox.php:
--------------------------------------------------------------------------------
1 |
9 | * @license MIT https://opensource.org/licenses/mit-license.php
10 | * @link https://github.com/degami/php-forms-api
11 | */
12 | /* #########################################################
13 | #### FIELDS ####
14 | ######################################################### */
15 |
16 | namespace Degami\PHPFormsApi\Fields;
17 |
18 | use Degami\Basics\Html\BaseElement;
19 | use Degami\PHPFormsApi\Form;
20 | use Degami\Basics\Html\TagElement;
21 |
22 | /**
23 | * The switch selection field class
24 | */
25 | class Switchbox extends Radios
26 | {
27 |
28 | /** @var mixed "no" value */
29 | protected $no_value;
30 |
31 | /** @var string "no" label */
32 | protected $no_label;
33 |
34 | /** @var mixed "yes" value */
35 | protected $yes_value;
36 |
37 | /** @var string "yes" label */
38 | protected $yes_label;
39 |
40 | /**
41 | * Class constructor
42 | *
43 | * @param array $options build options
44 | * @param ?string $name field name
45 | */
46 | public function __construct(array $options = [], ?string $name = null)
47 | {
48 | $this->no_value = 0;
49 | $this->no_label = $this->getText('No');
50 | $this->yes_value = 1;
51 | $this->yes_label = $this->getText('Yes');
52 |
53 | // labels and values can be overwritten
54 | parent::__construct($options, $name);
55 |
56 | // "options" is overwritten
57 | $this->options = [
58 | $this->no_value => $this->no_label,
59 | $this->yes_value => $this->yes_label,
60 | ];
61 | }
62 |
63 | /**
64 | * {@inheritdoc}
65 | *
66 | * @param Form $form form object
67 | */
68 | public function preRender(Form $form)
69 | {
70 | if ($this->pre_rendered == true) {
71 | return;
72 | }
73 | $id = $this->getHtmlId();
74 |
75 |
76 | foreach ($this->options as $key => $value) {
77 | $this->addJs(
78 | "\$('#{$id}-{$key}','#{$form->getId()}')
79 | .click(function(evt){
80 | \$(this).closest('label').addClass('ui-state-active');
81 | \$('#{$id} input[type=\"radio\"]').not(this).closest('label').removeClass('ui-state-active');
82 | });"
83 | );
84 | }
85 |
86 | $this->addCss(
87 | "#{$id} .label-switch{
88 | text-align: center;
89 | display: inline-block;
90 | width: 50%;
91 | padding-top: 10px;
92 | padding-bottom: 10px;
93 | box-sizing: border-box;
94 | }"
95 | );
96 | $this->addJs(
97 | "\$('#{$id}','#{$form->getId()}').find('input[type=\"radio\"]:checked')
98 | .closest('label').addClass('ui-state-active');"
99 | );
100 | //$this->add_css("#{$id} .label-switch input{ display: none; }");
101 | $this->addJs("\$('#{$id} input[type=\"radio\"]','#{$form->getId()}').hide();");
102 | parent::preRender($form);
103 | }
104 |
105 | /**
106 | * {@inheritdoc}
107 | *
108 | * @param Form $form form object
109 | *
110 | * @return string|BaseElement the element html
111 | */
112 | public function renderField(Form $form)
113 | {
114 | $id = $this->getHtmlId();
115 | $tag = new TagElement([
116 | 'tag' => 'div',
117 | 'id' => $id,
118 | 'attributes' => ['class' => 'options ui-widget-content ui-corner-all'],
119 | ]);
120 |
121 | if ($this->disabled == true) {
122 | $this->attributes['disabled']='disabled';
123 | }
124 |
125 | foreach ($this->options as $key => $value) {
126 | $attributes = $this->attributes;
127 | if (is_array($value) && isset($value['attributes'])) {
128 | $attributes = $value['attributes'];
129 | }
130 | if (is_array($value)) {
131 | $value = $value['value'];
132 | }
133 |
134 | $tag_label = new TagElement([
135 | 'tag' => 'label',
136 | 'attributes' => [
137 | 'id' => "{$id}-{$key}-button",
138 | 'for' => "{$id}-{$key}",
139 | 'class' => "label-switch ui-widget ui-state-default"
140 | ],
141 | ]);
142 | $tag_label->addChild(new TagElement([
143 | 'tag' => 'input',
144 | 'type' => 'radio',
145 | 'id' => "{$id}-{$key}",
146 | 'name' => "{$this->name}",
147 | 'value' => $key,
148 | 'attributes' => array_merge(
149 | $attributes,
150 | (($this->getValues() == $key) ? ['checked' => 'checked'] : [])
151 | ),
152 | 'text' => $this->getText($value),
153 | ]));
154 | $tag->addChild($tag_label);
155 | }
156 | return $tag;
157 | }
158 | }
159 |
--------------------------------------------------------------------------------
/src/classes/fields/Tel.php:
--------------------------------------------------------------------------------
1 |
9 | * @license MIT https://opensource.org/licenses/mit-license.php
10 | * @link https://github.com/degami/php-forms-api
11 | */
12 | /* #########################################################
13 | #### FIELDS ####
14 | ######################################################### */
15 |
16 | namespace Degami\PHPFormsApi\Fields;
17 |
18 | use Degami\PHPFormsApi\Form;
19 | use Degami\PHPFormsApi\Abstracts\Base\Field;
20 | use Degami\Basics\Html\TagElement;
21 |
22 | /**
23 | * The tel input field class
24 | */
25 | class Tel extends Field
26 | {
27 | /**
28 | * {@inheritdoc}
29 | *
30 | * @param Form $form form object
31 | *
32 | * @return string the element html
33 | */
34 | public function renderField(Form $form)
35 | {
36 | $id = $this->getHtmlId();
37 |
38 | if (!isset($this->attributes['class'])) {
39 | $this->attributes['class'] = '';
40 | }
41 | if ($this->hasErrors()) {
42 | $this->attributes['class'] .= ' has-errors';
43 | }
44 | if ($this->disabled == true) {
45 | $this->attributes['disabled']='disabled';
46 | }
47 | if (is_array($this->value)) {
48 | $this->value = '';
49 | }
50 |
51 | return new TagElement([
52 | 'tag' => 'input',
53 | 'type' => 'tel',
54 | 'id' => $id,
55 | 'name' => $this->name,
56 | 'value' => htmlspecialchars($this->getValues()),
57 | 'attributes' => $this->attributes + ['size' => $this->size],
58 | ]);
59 | }
60 |
61 | /**
62 | * {@inheritdoc}
63 | *
64 | * @return boolean this is a value
65 | */
66 | public function isAValue() : bool
67 | {
68 | return true;
69 | }
70 | }
71 |
--------------------------------------------------------------------------------
/src/classes/fields/Textarea.php:
--------------------------------------------------------------------------------
1 |
9 | * @license MIT https://opensource.org/licenses/mit-license.php
10 | * @link https://github.com/degami/php-forms-api
11 | */
12 | /* #########################################################
13 | #### FIELDS ####
14 | ######################################################### */
15 |
16 | namespace Degami\PHPFormsApi\Fields;
17 |
18 | use Degami\PHPFormsApi\Form;
19 | use Degami\PHPFormsApi\Abstracts\Base\Field;
20 | use Degami\Basics\Html\TagElement;
21 |
22 | /**
23 | * The textarea field class
24 | */
25 | class Textarea extends Field
26 | {
27 | /**
28 | * Element maxlenght
29 | *
30 | * @var integer
31 | */
32 | protected $maxlength = null;
33 |
34 | /**
35 | * Element minlength
36 | *
37 | * @var integer
38 | */
39 | protected $minlength = null;
40 |
41 | /**
42 | * rows
43 | *
44 | * @var integer
45 | */
46 | protected $rows = 5;
47 |
48 | /**
49 | * resizable flag
50 | *
51 | * @var boolean
52 | */
53 | protected $resizable = false;
54 |
55 | /**
56 | * {@inheritdoc}
57 | *
58 | * @param Form $form form object
59 | */
60 | public function preRender(Form $form)
61 | {
62 | if ($this->pre_rendered == true) {
63 | return;
64 | }
65 | $id = $this->getHtmlId();
66 | if ($this->resizable == true) {
67 | $this->addJs("\$('#{$id}','#{$form->getId()}').resizable({handles:\"se\"});");
68 | }
69 | parent::preRender($form);
70 | }
71 |
72 | /**
73 | * {@inheritdoc}
74 | *
75 | * @param Form $form form object
76 | * @return string the element html
77 | */
78 | public function renderField(Form $form)
79 | {
80 | $id = $this->getHtmlId();
81 |
82 | if (!isset($this->attributes['class'])) {
83 | $this->attributes['class'] = '';
84 | }
85 | $errors = $this->getErrors();
86 | if (!empty($errors)) {
87 | $this->attributes['class'] .= ' has-errors';
88 | }
89 | if ($this->disabled == true) {
90 | $this->attributes['disabled']='disabled';
91 | }
92 |
93 | return new TagElement([
94 | 'tag' => 'textarea',
95 | 'id' => $id,
96 | 'name' => $this->name,
97 | 'text' => $this->getValues(),
98 | 'attributes' => $this->attributes + ['cols' => $this->size, 'rows' => $this->rows],
99 | 'has_close' => true,
100 | ]);
101 | }
102 |
103 | /**
104 | * {@inheritdoc}
105 | *
106 | * @return boolean this is a value
107 | */
108 | public function isAValue() : bool
109 | {
110 | return true;
111 | }
112 | }
113 |
--------------------------------------------------------------------------------
/src/classes/fields/Textfield.php:
--------------------------------------------------------------------------------
1 |
9 | * @license MIT https://opensource.org/licenses/mit-license.php
10 | * @link https://github.com/degami/php-forms-api
11 | */
12 | /* #########################################################
13 | #### FIELDS ####
14 | ######################################################### */
15 |
16 | namespace Degami\PHPFormsApi\Fields;
17 |
18 | use Degami\Basics\Html\BaseElement;
19 | use Degami\PHPFormsApi\Form;
20 | use Degami\PHPFormsApi\Abstracts\Base\Field;
21 | use Degami\Basics\Html\TagElement;
22 |
23 | /**
24 | * The text input field class
25 | */
26 | class Textfield extends Field
27 | {
28 | /**
29 | * Element maxlenght
30 | *
31 | * @var integer
32 | */
33 | protected $maxlength = null;
34 |
35 | /**
36 | * Element minlength
37 | *
38 | * @var integer
39 | */
40 | protected $minlength = null;
41 |
42 | /**
43 | * {@inheritdoc}
44 | *
45 | * @param Form $form form object
46 | *
47 | * @return string|BaseElement the element html
48 | */
49 | public function renderField(Form $form)
50 | {
51 | $id = $this->getHtmlId();
52 |
53 | if (!isset($this->attributes['class'])) {
54 | $this->attributes['class'] = '';
55 | }
56 | if ($this->hasErrors()) {
57 | $this->attributes['class'] .= ' has-errors';
58 | }
59 | if ($this->disabled == true) {
60 | $this->attributes['disabled']='disabled';
61 | }
62 | if (is_array($this->value)) {
63 | $this->value = '';
64 | }
65 |
66 | return new TagElement([
67 | 'tag' => 'input',
68 | 'type' => 'text',
69 | 'id' => $id,
70 | 'name' => $this->name,
71 | 'value' => htmlspecialchars((string) $this->getValues()),
72 | 'attributes' => $this->attributes + ['size' => $this->size],
73 | ]);
74 | }
75 |
76 | /**
77 | * {@inheritdoc}
78 | *
79 | * @return boolean this is a value
80 | */
81 | public function isAValue() : bool
82 | {
83 | return true;
84 | }
85 | }
86 |
--------------------------------------------------------------------------------
/src/classes/fields/Time.php:
--------------------------------------------------------------------------------
1 |
9 | * @license MIT https://opensource.org/licenses/mit-license.php
10 | * @link https://github.com/degami/php-forms-api
11 | */
12 | /* #########################################################
13 | #### FIELDS ####
14 | ######################################################### */
15 |
16 | namespace Degami\PHPFormsApi\Fields;
17 |
18 | use Degami\Basics\Html\BaseElement;
19 | use Degami\PHPFormsApi\Form;
20 | use Degami\PHPFormsApi\Abstracts\Base\Field;
21 | use Degami\Basics\Html\TagElement;
22 |
23 | /**
24 | * The time field class
25 | */
26 | class Time extends Field
27 | {
28 | /**
29 | * Class constructor
30 | *
31 | * @param array $options build options
32 | * @param ?string $name field name
33 | */
34 | public function __construct(array $options = [], ?string $name = null)
35 | {
36 | $this->default_value = '00:00';
37 | parent::__construct($options, $name);
38 | }
39 |
40 | /**
41 | * {@inheritdoc}
42 | *
43 | * @param Form $form form object
44 | *
45 | * @return string|BaseElement the element html
46 | */
47 | public function renderField(Form $form)
48 | {
49 | $id = $this->getHtmlId();
50 |
51 | if (!isset($this->attributes['class'])) {
52 | $this->attributes['class'] = '';
53 | }
54 | if ($this->hasErrors()) {
55 | $this->attributes['class'] .= ' has-errors';
56 | }
57 | if ($this->disabled == true) {
58 | $this->attributes['disabled']='disabled';
59 | }
60 | if (is_array($this->value)) {
61 | $this->value = '';
62 | }
63 |
64 | return new TagElement([
65 | 'tag' => 'input',
66 | 'type' => 'time',
67 | 'id' => $id,
68 | 'name' => $this->name,
69 | 'value' => htmlspecialchars($this->getValues()),
70 | 'attributes' => $this->attributes + ['size' => $this->size],
71 | ]);
72 | }
73 |
74 | /**
75 | * {@inheritdoc}
76 | *
77 | * @return boolean this is a value
78 | */
79 | public function isAValue() : bool
80 | {
81 | return true;
82 | }
83 | }
84 |
--------------------------------------------------------------------------------
/src/classes/fields/Tinymce.php:
--------------------------------------------------------------------------------
1 |
9 | * @license MIT https://opensource.org/licenses/mit-license.php
10 | * @link https://github.com/degami/php-forms-api
11 | */
12 | /* #########################################################
13 | #### FIELDS ####
14 | ######################################################### */
15 |
16 | namespace Degami\PHPFormsApi\Fields;
17 |
18 | use Degami\PHPFormsApi\Form;
19 | use \stdClass;
20 |
21 | /**
22 | * tinymce beautified textarea
23 | */
24 | class Tinymce extends Textarea
25 | {
26 | /**
27 | * tinymce options
28 | *
29 | * @var array
30 | */
31 | protected $tinymce_options = [];
32 |
33 | /**
34 | * Get tinymce options array
35 | *
36 | * @return array tinymce options
37 | */
38 | public function &getTinymceOptions(): array
39 | {
40 | return $this->tinymce_options;
41 | }
42 |
43 | /**
44 | * Set tinymce options array
45 | *
46 | * @param array $options array of valid tinymce options
47 | * @return self
48 | */
49 | public function setTinymceOptions(array $options): Tinymce
50 | {
51 | $options = array_filter($options, [$this, 'isValidTinymceOption']);
52 | $this->tinymce_options = $options;
53 |
54 | return $this;
55 | }
56 |
57 | /**
58 | * {@inheritdoc}
59 | *
60 | * @param Form $form form object
61 | */
62 | public function preRender(Form $form)
63 | {
64 | if ($this->pre_rendered == true) {
65 | return;
66 | }
67 | $id = $this->getHtmlId();
68 | $this->tinymce_options['selector'] = "#{$id}";
69 | $tinymce_options = new stdClass;
70 | foreach ($this->tinymce_options as $key => $value) {
71 | if (! $this->isValidTinymceOption($key)) {
72 | continue;
73 | }
74 | $tinymce_options->{$key} = $value;
75 | }
76 | $this->addJs("tinymce.init(".json_encode($tinymce_options).");");
77 | $this->addJs("
78 | document.querySelector('form').addEventListener('submit', function() {
79 | const editor = tinymce.get('$id');
80 | const content = editor.getBody().innerHTML;
81 | const textarea = document.querySelector('textarea#$id');
82 | if (textarea) {
83 | textarea.value = content;
84 | }
85 | });
86 | ");
87 | parent::preRender($form);
88 | }
89 |
90 | /**
91 | * filters valid tinymce options
92 | *
93 | * @param string $propertyname property name
94 | * @return boolean TRUE if is a valid tinymce option
95 | */
96 | private function isValidTinymceOption(string $propertyname): bool
97 | {
98 | // could be used to filter elements
99 | return true;
100 | }
101 | }
102 |
--------------------------------------------------------------------------------
/src/classes/fields/Url.php:
--------------------------------------------------------------------------------
1 |
9 | * @license MIT https://opensource.org/licenses/mit-license.php
10 | * @link https://github.com/degami/php-forms-api
11 | */
12 | /* #########################################################
13 | #### FIELDS ####
14 | ######################################################### */
15 |
16 | namespace Degami\PHPFormsApi\Fields;
17 |
18 | use Degami\PHPFormsApi\Form;
19 | use Degami\PHPFormsApi\Abstracts\Base\Field;
20 | use Degami\Basics\Html\TagElement;
21 |
22 | /**
23 | * The url input field class
24 | */
25 | class Url extends Field
26 | {
27 |
28 | /**
29 | * Class constructor
30 | *
31 | * @param array $options build options
32 | * @param ?string $name field name
33 | */
34 | public function __construct(array $options = [], ?string $name = null)
35 | {
36 | parent::__construct($options, $name);
37 |
38 | // ensure is url validator is present
39 | $this->getValidate()->addElement('url');
40 | }
41 |
42 | /**
43 | * {@inheritdoc}
44 | *
45 | * @param Form $form form object
46 | *
47 | * @return string the element html
48 | */
49 | public function renderField(Form $form)
50 | {
51 | $id = $this->getHtmlId();
52 |
53 | if (!isset($this->attributes['class'])) {
54 | $this->attributes['class'] = '';
55 | }
56 | if ($this->hasErrors()) {
57 | $this->attributes['class'] .= ' has-errors';
58 | }
59 | if ($this->disabled == true) {
60 | $this->attributes['disabled']='disabled';
61 | }
62 | if (is_array($this->value)) {
63 | $this->value = '';
64 | }
65 |
66 | return new TagElement([
67 | 'tag' => 'input',
68 | 'type' => 'url',
69 | 'id' => $id,
70 | 'name' => $this->name,
71 | 'value' => htmlspecialchars($this->getValues()),
72 | 'attributes' => $this->attributes + ['size' => $this->size],
73 | ]);
74 | }
75 |
76 | /**
77 | * {@inheritdoc}
78 | *
79 | * @return boolean this is a value
80 | */
81 | public function isAValue() : bool
82 | {
83 | return true;
84 | }
85 | }
86 |
--------------------------------------------------------------------------------
/src/classes/fields/Value.php:
--------------------------------------------------------------------------------
1 |
9 | * @license MIT https://opensource.org/licenses/mit-license.php
10 | * @link https://github.com/degami/php-forms-api
11 | */
12 | /* #########################################################
13 | #### FIELDS ####
14 | ######################################################### */
15 |
16 | namespace Degami\PHPFormsApi\Fields;
17 |
18 | use Degami\Basics\Html\BaseElement;
19 | use Degami\PHPFormsApi\Form;
20 | use Degami\PHPFormsApi\Abstracts\Base\Field;
21 |
22 | /**
23 | * The value field class
24 | * this field is not rendered as part of the form, but the value is passed on form submission
25 | */
26 | class Value extends Field
27 | {
28 |
29 | /**
30 | * Class constructor
31 | *
32 | * @param array $options build options
33 | * @param ?string $name field name
34 | */
35 | public function __construct(array $options = [], ?string $name = null)
36 | {
37 | $this->container_tag = '';
38 | $this->container_class = '';
39 | parent::__construct($options, $name);
40 | if (isset($options['value'])) {
41 | $this->value = $options['value'];
42 | }
43 | }
44 |
45 | /**
46 | * {@inheritdoc}
47 | *
48 | * @param Form $form form object
49 | *
50 | * @return string|BaseElement an empty string
51 | */
52 | public function renderField(Form $form)
53 | {
54 | return '';
55 | }
56 |
57 | /**
58 | * validate function
59 | *
60 | * @return boolean this field is always valid
61 | */
62 | public function isValid() : bool
63 | {
64 | return true;
65 | }
66 |
67 | /**
68 | * {@inheritdoc}
69 | *
70 | * @return boolean this is a value
71 | */
72 | public function isAValue() : bool
73 | {
74 | return true;
75 | }
76 | }
77 |
--------------------------------------------------------------------------------
/src/classes/traits/Containers.php:
--------------------------------------------------------------------------------
1 |
9 | * @license MIT https://opensource.org/licenses/mit-license.php
10 | * @link https://github.com/degami/php-forms-api
11 | */
12 | /* #########################################################
13 | #### TRAITS ####
14 | ######################################################### */
15 |
16 | namespace Degami\PHPFormsApi\Traits;
17 |
18 | use Degami\PHPFormsApi\Abstracts\Base\Element;
19 | use Degami\PHPFormsApi\Abstracts\Base\Field;
20 | use Degami\PHPFormsApi\Abstracts\Base\FieldsContainer;
21 | use Degami\PHPFormsApi\Abstracts\Fields\ComposedField;
22 | use Degami\PHPFormsApi\Exceptions\FormException;
23 |
24 | /**
25 | * containers specific functions
26 | */
27 | trait Containers
28 | {
29 |
30 | /**
31 | * keeps fields insert order
32 | *
33 | * @var array
34 | */
35 | protected $insert_field_order = [];
36 |
37 | /**
38 | * Element fields
39 | *
40 | * @var array
41 | */
42 | protected $fields = [];
43 |
44 | /**
45 | * Get the fields array by reference
46 | *
47 | * @return array the array of field elements
48 | */
49 | public function &getFields(): array
50 | {
51 | return $this->fields;
52 | }
53 |
54 | /**
55 | * Get parent namespace
56 | *
57 | * @return string parent namespace
58 | */
59 | private function parentNameSpace(): string
60 | {
61 | $namespaceParts = explode('\\', __NAMESPACE__);
62 | return implode("\\", array_slice($namespaceParts, 0, -1));
63 | }
64 |
65 | /**
66 | * Returns a field object instance
67 | *
68 | * @param string $name field name
69 | * @param mixed $field field to add, can be an array or a field subclass
70 | * @return Field instance
71 | * @throws FormException
72 | */
73 | public function getFieldObj(string $name, $field): Field
74 | {
75 | if (is_array($field)) {
76 | $parentNS = $this->parentNameSpace();
77 | $element_type = isset($field['type']) ?
78 | $this->snakeCaseToPascalCase($field['type']) :
79 | 'textfield';
80 |
81 | $field_type = $parentNS . "\\Fields\\" . $element_type;
82 | $container_type = $parentNS . "\\Containers\\" . $element_type;
83 | $root_type = $parentNS . "\\" . $element_type;
84 |
85 | if (!class_exists($field_type) && !class_exists($container_type) && !class_exists($root_type)) {
86 | throw new FormException(
87 | "Error adding field. Class \"$field_type\", \"$container_type\", \"$root_type\" not found",
88 | 1
89 | );
90 | }
91 |
92 | if (class_exists($field_type)) {
93 | $type = $field_type;
94 | } elseif (class_exists($container_type)) {
95 | $type = $container_type;
96 | } else {
97 | $type = $root_type;
98 | }
99 |
100 | if (is_subclass_of($type, 'Degami\PHPFormsApi\Abstracts\Base\Field')) {
101 | /** @var Field $type */
102 | $field = $type::getInstance($field, $name);
103 | } else {
104 | $field = new $type($field, $name);
105 | }
106 | } elseif ($field instanceof Field) {
107 | $field->setName($name);
108 | } else {
109 | throw new FormException("Error adding field. Array or field subclass expected, ".gettype($field)." given", 1);
110 | }
111 |
112 | return $field;
113 | }
114 |
115 | /**
116 | * Check if field is a field container
117 | *
118 | * @param Field $field field instance
119 | * @return boolean true if field is a field container
120 | */
121 | public function isFieldContainer(Field $field): bool
122 | {
123 | return $field instanceof FieldsContainer && !($field instanceof ComposedField);
124 | }
125 |
126 | /**
127 | * add markup helper
128 | *
129 | * @param string $markup markup to add
130 | * @param array $options
131 | * @return Element
132 | */
133 | public function addMarkup(string $markup, array $options = []): Element
134 | {
135 | static $lastMarkupIndex = 0;
136 | return $this->addField('_markup_'.time().'_'.$lastMarkupIndex++, [
137 | 'type' => 'markup',
138 | 'container_tag' => null,
139 | 'value' => $markup,
140 | ] + $options);
141 | }
142 | }
143 |
--------------------------------------------------------------------------------
/src/fonts/Lato-Regular.ttf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/degami/php-forms-api/fec01af913d6f0591c8a151f0eb16f9915f1a076/src/fonts/Lato-Regular.ttf
--------------------------------------------------------------------------------
/src/interfaces/FieldInterface.php:
--------------------------------------------------------------------------------
1 |
9 | * @license MIT https://opensource.org/licenses/mit-license.php
10 | * @link https://github.com/degami/php-forms-api
11 | */
12 | /* #########################################################
13 | #### FIELD INTERFACE ####
14 | ######################################################### */
15 |
16 | namespace Degami\PHPFormsApi\Interfaces;
17 |
18 | use Degami\Basics\Html\BaseElement;
19 | use Degami\Basics\Html\TagElement;
20 | use Degami\PHPFormsApi\Form;
21 |
22 | /**
23 | * field interface
24 | */
25 | interface FieldInterface
26 | {
27 |
28 | /**
29 | * this function tells to the form if this element is a value that needs to be
30 | * included into parent values() function call result
31 | *
32 | * @return boolean include_me
33 | */
34 | public function isAValue() : bool; // tells if component value is passed on the parent values() function call
35 |
36 | /**
37 | * Pre-render hook
38 | *
39 | * @param Form $form form object
40 | */
41 | public function preRender(Form $form);
42 |
43 | /**
44 | * The function that actually renders the html field
45 | *
46 | * @param Form $form form object
47 | *
48 | * @return string|BaseElement the field html
49 | */
50 | public function renderField(Form $form); // renders html
51 |
52 | /**
53 | * Process / set field value
54 | *
55 | * @param mixed $value value to set
56 | */
57 | public function processValue($value);
58 |
59 | /**
60 | * Check element validity
61 | *
62 | * @return boolean TRUE if element is valid
63 | */
64 | public function isValid() : bool;
65 |
66 | /**
67 | * Return form elements values into this element
68 | *
69 | * @return mixed form values
70 | */
71 | public function getValues();
72 |
73 | /**
74 | * which element should return the add_field() function
75 | *
76 | * @return string one of 'parent' or 'this'
77 | */
78 | public function onAddReturn() : string;
79 | }
80 |
--------------------------------------------------------------------------------
/src/interfaces/FieldsContainerInterface.php:
--------------------------------------------------------------------------------
1 |
9 | * @license MIT https://opensource.org/licenses/mit-license.php
10 | * @link https://github.com/degami/php-forms-api
11 | */
12 | /* #########################################################
13 | #### FIELD INTERFACE ####
14 | ######################################################### */
15 |
16 | namespace Degami\PHPFormsApi\Interfaces;
17 |
18 | use Degami\PHPFormsApi\Abstracts\Base\Element;
19 | use Degami\PHPFormsApi\Abstracts\Base\FieldsContainer;
20 | use Degami\PHPFormsApi\Form;
21 |
22 | /**
23 | * fields container interface
24 | */
25 | interface FieldsContainerInterface extends FieldInterface
26 | {
27 |
28 | /**
29 | * Add field to form
30 | *
31 | * @param string $name field name
32 | * @param mixed $field field to add, can be an array or a field subclass
33 | * @return FieldsContainer
34 | */
35 | public function addField(string $name, $field) : Element;
36 |
37 | /**
38 | * remove field from form
39 | *
40 | * @param string $name field name
41 | * @return FieldsContainer
42 | */
43 | public function removeField(string $name) : FieldsContainer;
44 |
45 | /**
46 | * on_add_return overload
47 | *
48 | * @return string 'this'
49 | */
50 | public function onAddReturn(): string;
51 | }
52 |
--------------------------------------------------------------------------------
/src/php_forms_api_bootstrap.php:
--------------------------------------------------------------------------------
1 |
9 | * @license MIT https://opensource.org/licenses/mit-license.php
10 | * @link https://github.com/degami/php-forms-api
11 | */
12 |
13 | if ((function_exists('session_status') && session_status() != PHP_SESSION_NONE) || trim(session_id()) != '') {
14 | @ini_set('session.gc_maxlifetime', FORMS_SESSION_TIMEOUT);
15 | @session_set_cookie_params(FORMS_SESSION_TIMEOUT);
16 | }
17 |
--------------------------------------------------------------------------------
/src/php_forms_api_defines.php:
--------------------------------------------------------------------------------
1 |
9 | * @license MIT https://opensource.org/licenses/mit-license.php
10 | * @link https://github.com/degami/php-forms-api
11 | */
12 |
13 | /*
14 | * Turn on error reporting during development
15 | */
16 | // error_reporting(E_ALL);
17 | // ini_set('display_errors', TRUE);
18 | // ini_set('display_startup_errors', TRUE);
19 |
20 | namespace Degami\PHPFormsApi;
21 |
22 | /*
23 | * PHP Forms API library configuration
24 | */
25 |
26 | if (!defined('FORMS_DEFAULT_FORM_CONTAINER_TAG')) {
27 | define('FORMS_DEFAULT_FORM_CONTAINER_TAG', 'div');
28 | }
29 | if (!defined('FORMS_DEFAULT_FORM_CONTAINER_CLASS')) {
30 | define('FORMS_DEFAULT_FORM_CONTAINER_CLASS', 'form-container');
31 | }
32 | if (!defined('FORMS_DEFAULT_FIELD_CONTAINER_TAG')) {
33 | define('FORMS_DEFAULT_FIELD_CONTAINER_TAG', 'div');
34 | }
35 | if (!defined('FORMS_DEFAULT_FIELD_CONTAINER_CLASS')) {
36 | define('FORMS_DEFAULT_FIELD_CONTAINER_CLASS', 'form-item');
37 | }
38 | if (!defined('FORMS_DEFAULT_FIELD_LABEL_CLASS')) {
39 | define('FORMS_DEFAULT_FIELD_LABEL_CLASS', '');
40 | }
41 | if (!defined('FORMS_FIELD_ADDITIONAL_CLASS')) {
42 | define('FORMS_FIELD_ADDITIONAL_CLASS', '');
43 | }
44 | if (!defined('FORMS_VALIDATE_EMAIL_DNS')) {
45 | define('FORMS_VALIDATE_EMAIL_DNS', true);
46 | }
47 | if (!defined('FORMS_VALIDATE_EMAIL_BLOCKED_DOMAINS')) {
48 | define('FORMS_VALIDATE_EMAIL_BLOCKED_DOMAINS', 'mailinator.com|guerrillamail.com');
49 | }
50 | if (!defined('FORMS_BASE_PATH')) {
51 | define('FORMS_BASE_PATH', '');
52 | }
53 | if (!defined('FORMS_XSS_ALLOWED_TAGS')) {
54 | define('FORMS_XSS_ALLOWED_TAGS', 'a|em|strong|cite|code|ul|ol|li|dl|dt|dd');
55 | }
56 | if (!defined('FORMS_SESSION_TIMEOUT')) {
57 | define('FORMS_SESSION_TIMEOUT', 7200);
58 | }
59 | if (!defined('FORMS_ERRORS_ICON')) {
60 | define(
61 | 'FORMS_ERRORS_ICON',
62 | '
'
63 | );
64 | }
65 | if (!defined('FORMS_ERRORS_TEMPLATE')) {
66 | define(
67 | 'FORMS_ERRORS_TEMPLATE',
68 | '
' . FORMS_ERRORS_ICON . '
'
69 | );
70 | }
71 | if (!defined('FORMS_HIGHLIGHTS_ICON')) {
72 | define(
73 | 'FORMS_HIGHLIGHTS_ICON',
74 | '
'
75 | );
76 | }
77 | if (!defined('FORMS_HIGHLIGHTS_TEMPLATE')) {
78 | define(
79 | 'FORMS_HIGHLIGHTS_TEMPLATE',
80 | '
' . FORMS_HIGHLIGHTS_ICON . '
'
81 | );
82 | }
83 |
--------------------------------------------------------------------------------