From 2014ee90fe4ce5a6b687f99df9af0cfe5afda9fa Mon Sep 17 00:00:00 2001 From: Michael Herold Date: Fri, 16 Jan 2015 21:14:26 +0100 Subject: [PATCH] Actually this is the version that supports checks --- src/Abstract_/Form.php | 4 +- src/Abstract_/FormElement.php | 9 ++-- src/Abstract_/FormField.php | 23 +++++++++- src/Abstract_/FormFieldDefault.php | 14 +++--- src/Abstract_/FormFieldInput.php | 2 - src/Check.php | 72 ++++++++++++++++++++++++++++++ src/CheckCustom.php | 35 +++++++++++++++ src/CheckMaxLength.php | 40 +++++++++++++++++ src/CheckMinLength.php | 40 +++++++++++++++++ src/FieldEmail.php | 2 +- src/FieldPassword.php | 2 +- src/FieldSelect.php | 16 ++++++- src/FieldText.php | 7 +-- src/FormPost.php | 8 ++-- src/InputHidden.php | 1 - 15 files changed, 245 insertions(+), 30 deletions(-) create mode 100644 src/Check.php create mode 100644 src/CheckCustom.php create mode 100644 src/CheckMaxLength.php create mode 100644 src/CheckMinLength.php diff --git a/src/Abstract_/Form.php b/src/Abstract_/Form.php index 68d8a24..0055758 100644 --- a/src/Abstract_/Form.php +++ b/src/Abstract_/Form.php @@ -74,8 +74,8 @@ public function getSingleControlTemplate() { * @return boolean */ public function dataValid() { - foreach (new \RecursiveIteratorIterator($this) as $child) { - if ($child instanceof Abstract_\FormElement && !$child->dataValid()) { + foreach (new \RecursiveIteratorIterator($this, \RecursiveIteratorIterator::SELF_FIRST) as $child) { + if ($child instanceof FormElement && !$child->dataValid()) { return false; } } diff --git a/src/Abstract_/FormElement.php b/src/Abstract_/FormElement.php index 1acbca3..a438249 100644 --- a/src/Abstract_/FormElement.php +++ b/src/Abstract_/FormElement.php @@ -34,10 +34,9 @@ public function getForm() { if ($this->existsInheritableAppendage('_FORM')) return $this->getInheritableAppendage('_FORM'); else { - #print_r(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS)); - throw new exception\NotLazyEnough( - 'No Form for FormElement found. Maybe not yet a child of a Form.'); + 'No Form for FormElement found. Maybe not yet a child of a Form.' + ); } } @@ -59,4 +58,8 @@ public function getHtmlName(array $extraKeys = []) { '_' . $this->getName() . implode('_', $extraKeys); } + public function getValueUser() { + return $this->getForm()->getValueUser($this->getHtmlName()); + } + } diff --git a/src/Abstract_/FormField.php b/src/Abstract_/FormField.php index e979c95..1284764 100644 --- a/src/Abstract_/FormField.php +++ b/src/Abstract_/FormField.php @@ -17,10 +17,29 @@ public function changed() { /** * - * @todo not implemented */ public function dataValid() { - return true; + $allValid = true; + foreach ($this->checks as $key => $check) { + $valid = $check($this->getValueUser()); + $allValid = $allValid && $valid; + } + + return $allValid; + } + + /** + * + * @var array[Check] + */ + public $checks = []; + + /** + * + * @param Check $check + */ + public function addValidityCheck(\hemio\form\Check $check) { + $this->checks[$check->getId()] = $check; } } diff --git a/src/Abstract_/FormFieldDefault.php b/src/Abstract_/FormFieldDefault.php index 59bcc86..78ef386 100644 --- a/src/Abstract_/FormFieldDefault.php +++ b/src/Abstract_/FormFieldDefault.php @@ -74,14 +74,6 @@ public function getValueToUse() { return $this->getValueDefault(); } - public function getValueUser() { - $value = $this->getForm()->getValueUser($this->getHtmlName()); - if ($value === null) - return null; - else - return $this->getFiltered($value); - } - public function getValueStored() { return $this->getFiltered( $this->getForm()->getValueStored($this->getName())); @@ -140,4 +132,10 @@ public function __toString() { } abstract public function fill(); + + public function setForm(Form $form) { + $this->control->setAttribute('form', $form->getHtmlName()); + $this->addInheritableAppendage('_FORM', $form); + } + } diff --git a/src/Abstract_/FormFieldInput.php b/src/Abstract_/FormFieldInput.php index 4e454af..b8db5f2 100644 --- a/src/Abstract_/FormFieldInput.php +++ b/src/Abstract_/FormFieldInput.php @@ -66,8 +66,6 @@ public function fill() { return $template; } - - public function describe() { return sprint('INPUT(%s)', strtoupper($this->getInputType())); } diff --git a/src/Check.php b/src/Check.php new file mode 100644 index 0000000..052bd6c --- /dev/null +++ b/src/Check.php @@ -0,0 +1,72 @@ + + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +namespace hemio\form; + +/** + * Description of ValidityCheck + * + * @author Michael Herold + */ +abstract class Check { + + /** + * + * @var string + */ + protected $id; + + /** + * + * @var callable + */ + protected $check; + + /** + * + * @var string + */ + protected $message; + + /** + * @return string Id + */ + public function getId() { + return $this->id; + } + + /** + * + * @return string + */ + public function getMessage() { + return $this->message; + } + + /** + * + * @param mixed $value + * @return boolean + */ + public function check($value) { + $check = $this->check; + return $check($value); + } + +} diff --git a/src/CheckCustom.php b/src/CheckCustom.php new file mode 100644 index 0000000..87a4e9a --- /dev/null +++ b/src/CheckCustom.php @@ -0,0 +1,35 @@ + + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +namespace hemio\form; + +/** + * Description of CheckCustom + * + * @author Michael Herold + */ +class CheckCustom extends Check { + + public function __construct($id, callable $check, $message = null) { + $this->id = $id; + $this->check = $check; + $this->message = $message; + } + +} diff --git a/src/CheckMaxLength.php b/src/CheckMaxLength.php new file mode 100644 index 0000000..6e34d7c --- /dev/null +++ b/src/CheckMaxLength.php @@ -0,0 +1,40 @@ + + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +namespace hemio\form; + +/** + * Description of CheckLength + * + * @author Michael Herold + */ +class CheckMaxLength extends Check { + + protected $maxLength; + + public function __construct($maxLength) { + $this->maxLength = $maxLength; + $this->check = $this; + } + + public function __invoke($value) { + return strlen($value) <= $this->maxLength; + } + +} diff --git a/src/CheckMinLength.php b/src/CheckMinLength.php new file mode 100644 index 0000000..21e752a --- /dev/null +++ b/src/CheckMinLength.php @@ -0,0 +1,40 @@ + + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +namespace hemio\form; + +/** + * Description of CheckMinLength + * + * @author Michael Herold + */ +class CheckMinLength extends Check { + + protected $minLength; + + public function __construct($maxLength) { + $this->minLength = $maxLength; + $this->check = $this; + } + + public function __invoke($value) { + return strlen($value) >= $this->minLength; + } + +} diff --git a/src/FieldEmail.php b/src/FieldEmail.php index 5f37f91..b74390c 100644 --- a/src/FieldEmail.php +++ b/src/FieldEmail.php @@ -11,7 +11,7 @@ public function getInputType() { return 'email'; } - public function inputValid() { + public function inputValidOld() { $strAddress = $this->getValueUser($this->getHtmlName()); $arrExplode = explode('@', $strAddress); diff --git a/src/FieldPassword.php b/src/FieldPassword.php index 62c7808..1ce9629 100644 --- a/src/FieldPassword.php +++ b/src/FieldPassword.php @@ -13,7 +13,7 @@ public function getValueStored() { return ''; } - public function getValueUser() { + public function getValueToUse() { return ''; } diff --git a/src/FieldSelect.php b/src/FieldSelect.php index c406870..18aa967 100644 --- a/src/FieldSelect.php +++ b/src/FieldSelect.php @@ -50,8 +50,20 @@ public function fill() { return $template; } - public function addOption($value, $content) { - $this->getControlElement()->addChild(new html\Option($value, new html\String($content))); + /** + * + * @param mixed $value + * @param mixed $content + * @return html\Option + */ + public function addOption($value, $content = null) { + if ($content === null) + $content = $value; + + $option = new html\Option($value, new html\String($content)); + $this->getControlElement()->addChild($option); + + return $option; } } diff --git a/src/FieldText.php b/src/FieldText.php index 134d1f4..b8c05cd 100644 --- a/src/FieldText.php +++ b/src/FieldText.php @@ -6,7 +6,6 @@ class FieldText extends Abstract_\FormFieldInput { - public function getInputType() { return 'text'; } @@ -23,7 +22,7 @@ public function setPlaceholder($placeholder) { * * @param boolean $allow */ - public function setAllowMultiple($allow) { + public function setAllowMultiple($allow = true) { $this->getControlElement()->setAttribute('multiple', (boolean) $allow); } @@ -31,10 +30,8 @@ public function setAllowMultiple($allow) { * * @param boolean $required */ - public function setIsRequired($required) { + public function setRequired($required = true) { $this->getControlElement()->setAttribute('required', (boolean) $required); } - - } diff --git a/src/FormPost.php b/src/FormPost.php index 3c61904..3d06360 100644 --- a/src/FormPost.php +++ b/src/FormPost.php @@ -11,16 +11,18 @@ class FormPost extends Abstract_\Form { public function __construct($name, array $post = null, array $get = null, array $stored = []) { parent::__construct($name, $post, $get, $stored); - $this->addChild(new InputHidden($this->getIdentifierName(), '')); + + $this['form_identifier'] = new InputHidden($this->getIdentifierName(), ''); + $this->setAttribute('method', 'post'); } public function submitted() { - return $this->getPost($this->getIdentifierName()) !== null; + return $this['form_identifier']->getValueUser() !== null; } public function getIdentifierName() { - return $this->name . '___form_identifier'; + return '__form_identifier'; } public function correctSubmitted() { diff --git a/src/InputHidden.php b/src/InputHidden.php index dcaf166..c1cdcc5 100644 --- a/src/InputHidden.php +++ b/src/InputHidden.php @@ -13,7 +13,6 @@ public function __construct($name, $value) { $this->name = $name; $this['INPUT'] = new html\Input('hidden'); - $this['INPUT']->setAttribute('type', 'hidden'); $this['INPUT']->setAttribute('value', $value); }