Skip to content

Commit

Permalink
Actually this is the version that supports checks
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Herold committed Jan 16, 2015
1 parent b1bee41 commit 2014ee9
Show file tree
Hide file tree
Showing 15 changed files with 245 additions and 30 deletions.
4 changes: 2 additions & 2 deletions src/Abstract_/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
9 changes: 6 additions & 3 deletions src/Abstract_/FormElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.'
);
}
}

Expand All @@ -59,4 +58,8 @@ public function getHtmlName(array $extraKeys = []) {
'_' . $this->getName() . implode('_', $extraKeys);
}

public function getValueUser() {
return $this->getForm()->getValueUser($this->getHtmlName());
}

}
23 changes: 21 additions & 2 deletions src/Abstract_/FormField.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

}
14 changes: 6 additions & 8 deletions src/Abstract_/FormFieldDefault.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
Expand Down Expand Up @@ -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);
}

}
2 changes: 0 additions & 2 deletions src/Abstract_/FormFieldInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,6 @@ public function fill() {
return $template;
}



public function describe() {
return sprint('INPUT(%s)', strtoupper($this->getInputType()));
}
Expand Down
72 changes: 72 additions & 0 deletions src/Check.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

/*
* Copyright (C) 2015 Michael Herold <quabla@hemio.de>
*
* 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 <http://www.gnu.org/licenses/>.
*/

namespace hemio\form;

/**
* Description of ValidityCheck
*
* @author Michael Herold <quabla@hemio.de>
*/
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);
}

}
35 changes: 35 additions & 0 deletions src/CheckCustom.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

/*
* Copyright (C) 2015 Michael Herold <quabla@hemio.de>
*
* 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 <http://www.gnu.org/licenses/>.
*/

namespace hemio\form;

/**
* Description of CheckCustom
*
* @author Michael Herold <quabla@hemio.de>
*/
class CheckCustom extends Check {

public function __construct($id, callable $check, $message = null) {
$this->id = $id;
$this->check = $check;
$this->message = $message;
}

}
40 changes: 40 additions & 0 deletions src/CheckMaxLength.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

/*
* Copyright (C) 2015 Michael Herold <quabla@hemio.de>
*
* 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 <http://www.gnu.org/licenses/>.
*/

namespace hemio\form;

/**
* Description of CheckLength
*
* @author Michael Herold <quabla@hemio.de>
*/
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;
}

}
40 changes: 40 additions & 0 deletions src/CheckMinLength.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

/*
* Copyright (C) 2015 Michael Herold <quabla@hemio.de>
*
* 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 <http://www.gnu.org/licenses/>.
*/

namespace hemio\form;

/**
* Description of CheckMinLength
*
* @author Michael Herold <quabla@hemio.de>
*/
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;
}

}
2 changes: 1 addition & 1 deletion src/FieldEmail.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public function getInputType() {
return 'email';
}

public function inputValid() {
public function inputValidOld() {
$strAddress = $this->getValueUser($this->getHtmlName());

$arrExplode = explode('@', $strAddress);
Expand Down
2 changes: 1 addition & 1 deletion src/FieldPassword.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public function getValueStored() {
return '';
}

public function getValueUser() {
public function getValueToUse() {
return '';
}

Expand Down
16 changes: 14 additions & 2 deletions src/FieldSelect.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

}
7 changes: 2 additions & 5 deletions src/FieldText.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

class FieldText extends Abstract_\FormFieldInput {


public function getInputType() {
return 'text';
}
Expand All @@ -23,18 +22,16 @@ public function setPlaceholder($placeholder) {
*
* @param boolean $allow
*/
public function setAllowMultiple($allow) {
public function setAllowMultiple($allow = true) {
$this->getControlElement()->setAttribute('multiple', (boolean) $allow);
}

/**
*
* @param boolean $required
*/
public function setIsRequired($required) {
public function setRequired($required = true) {
$this->getControlElement()->setAttribute('required', (boolean) $required);
}



}
8 changes: 5 additions & 3 deletions src/FormPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
1 change: 0 additions & 1 deletion src/InputHidden.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down

0 comments on commit 2014ee9

Please sign in to comment.