Refactored Form.
This commit is contained in:
parent
1fdf9a7ab1
commit
cfe93434ad
45 changed files with 58 additions and 83 deletions
|
|
@ -4,10 +4,6 @@ namespace PHPCensor;
|
|||
|
||||
use Symfony\Component\Yaml\Parser as YamlParser;
|
||||
|
||||
if (!defined('B8_PATH')) {
|
||||
define('B8_PATH', dirname(__DIR__) . '/B8Framework/');
|
||||
}
|
||||
|
||||
class Config
|
||||
{
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -2,8 +2,7 @@
|
|||
|
||||
namespace PHPCensor\Controller;
|
||||
|
||||
use b8;
|
||||
use b8\Form;
|
||||
use PHPCensor\Form;
|
||||
use PHPCensor\Controller;
|
||||
use PHPCensor\Http\Response\RedirectResponse;
|
||||
use PHPCensor\Model\ProjectGroup;
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@
|
|||
namespace PHPCensor\Controller;
|
||||
|
||||
use PHPCensor\Exception\HttpException\NotFoundException;
|
||||
use b8\Form;
|
||||
use PHPCensor\Form;
|
||||
use JasonGrimes\Paginator;
|
||||
use PHPCensor;
|
||||
use PHPCensor\BuildFactory;
|
||||
|
|
|
|||
|
|
@ -2,7 +2,6 @@
|
|||
|
||||
namespace PHPCensor\Controller;
|
||||
|
||||
use b8;
|
||||
use PHPCensor\Helper\Email;
|
||||
use PHPCensor\Helper\Lang;
|
||||
use PHPCensor\Controller;
|
||||
|
|
@ -117,42 +116,42 @@ class SessionController extends Controller
|
|||
}
|
||||
}
|
||||
|
||||
$form = new b8\Form();
|
||||
$form = new \PHPCensor\Form();
|
||||
$form->setMethod('POST');
|
||||
$form->setAction(APP_URL . 'session/login');
|
||||
|
||||
$email = new b8\Form\Element\Text('email');
|
||||
$email = new \PHPCensor\Form\Element\Text('email');
|
||||
$email->setLabel(Lang::get('login'));
|
||||
$email->setRequired(true);
|
||||
$email->setContainerClass('form-group');
|
||||
$email->setClass('form-control');
|
||||
$form->addField($email);
|
||||
|
||||
$pwd = new b8\Form\Element\Password('password');
|
||||
$pwd = new \PHPCensor\Form\Element\Password('password');
|
||||
$pwd->setLabel(Lang::get('password'));
|
||||
$pwd->setRequired(true);
|
||||
$pwd->setContainerClass('form-group');
|
||||
$pwd->setClass('form-control');
|
||||
$form->addField($pwd);
|
||||
|
||||
$remember = b8\Form\Element\Checkbox::create('remember_me', Lang::get('remember_me'), false);
|
||||
$remember = \PHPCensor\Form\Element\Checkbox::create('remember_me', Lang::get('remember_me'), false);
|
||||
$remember->setContainerClass('form-group');
|
||||
$remember->setCheckedValue(1);
|
||||
$remember->setValue(0);
|
||||
$form->addField($remember);
|
||||
|
||||
$pwd = new b8\Form\Element\Submit();
|
||||
$pwd = new \PHPCensor\Form\Element\Submit();
|
||||
$pwd->setValue(Lang::get('log_in'));
|
||||
$pwd->setClass('btn-success');
|
||||
$form->addField($pwd);
|
||||
|
||||
$tokenValue = $this->generateToken();
|
||||
$_SESSION['login_token'] = $tokenValue;
|
||||
$token = new b8\Form\Element\Hidden('token');
|
||||
$token = new \PHPCensor\Form\Element\Hidden('token');
|
||||
$token->setValue($tokenValue);
|
||||
$form->addField($token);
|
||||
|
||||
$this->view->form = $form->render();
|
||||
$this->view->form = $form->render();
|
||||
$this->view->failed = $isLoginFailure;
|
||||
|
||||
return $this->view->render();
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ namespace PHPCensor\Controller;
|
|||
|
||||
use PHPCensor\Config;
|
||||
use PHPCensor\Exception\HttpException\NotFoundException;
|
||||
use b8\Form;
|
||||
use PHPCensor\Form;
|
||||
use PHPCensor\Controller;
|
||||
use PHPCensor\Helper\Lang;
|
||||
use PHPCensor\Http\Response\RedirectResponse;
|
||||
|
|
|
|||
69
src/PHPCensor/Form.php
Normal file
69
src/PHPCensor/Form.php
Normal file
|
|
@ -0,0 +1,69 @@
|
|||
<?php
|
||||
|
||||
namespace PHPCensor;
|
||||
|
||||
use PHPCensor\Form\FieldSet;
|
||||
|
||||
class Form extends FieldSet
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $action = '';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $method = 'POST';
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getAction()
|
||||
{
|
||||
return $this->action;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $action
|
||||
*/
|
||||
public function setAction($action)
|
||||
{
|
||||
$this->action = $action;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getMethod()
|
||||
{
|
||||
return $this->method;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $method
|
||||
*/
|
||||
public function setMethod($method)
|
||||
{
|
||||
$this->method = $method;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param View $view
|
||||
*/
|
||||
protected function onPreRender(View &$view)
|
||||
{
|
||||
$view->action = $this->getAction();
|
||||
$view->method = $this->getMethod();
|
||||
|
||||
parent::onPreRender($view);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function __toString()
|
||||
{
|
||||
return $this->render();
|
||||
}
|
||||
}
|
||||
7
src/PHPCensor/Form/ControlGroup.php
Normal file
7
src/PHPCensor/Form/ControlGroup.php
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
<?php
|
||||
|
||||
namespace PHPCensor\Form;
|
||||
|
||||
class ControlGroup extends FieldSet
|
||||
{
|
||||
}
|
||||
199
src/PHPCensor/Form/Element.php
Normal file
199
src/PHPCensor/Form/Element.php
Normal file
|
|
@ -0,0 +1,199 @@
|
|||
<?php
|
||||
|
||||
namespace PHPCensor\Form;
|
||||
|
||||
use PHPCensor\View;
|
||||
|
||||
abstract class Element
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $name;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $id;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $label;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $class;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $containerClass;
|
||||
|
||||
/**
|
||||
* @var Element
|
||||
*/
|
||||
protected $parent;
|
||||
|
||||
/**
|
||||
* @param string|null $name
|
||||
*/
|
||||
public function __construct($name = null)
|
||||
{
|
||||
if (!is_null($name)) {
|
||||
$this->setName($name);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getName()
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setName($name)
|
||||
{
|
||||
$this->name = strtolower(preg_replace('/([^a-zA-Z0-9_\-%])/', '', $name));
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getId()
|
||||
{
|
||||
return !$this->id
|
||||
? ('element-' . $this->name)
|
||||
: $this->id;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $id
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setId($id)
|
||||
{
|
||||
$this->id = $id;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getLabel()
|
||||
{
|
||||
return $this->label;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $label
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setLabel($label)
|
||||
{
|
||||
$this->label = $label;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getClass()
|
||||
{
|
||||
return $this->class;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $class
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setClass($class)
|
||||
{
|
||||
$this->class = $class;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getContainerClass()
|
||||
{
|
||||
return $this->containerClass;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $class
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setContainerClass($class)
|
||||
{
|
||||
$this->containerClass = $class;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Element $parent
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setParent(Element $parent)
|
||||
{
|
||||
$this->parent = $parent;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $viewFile
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function render($viewFile = null)
|
||||
{
|
||||
$viewPath = SRC_DIR . 'View/';
|
||||
|
||||
if (is_null($viewFile)) {
|
||||
$class = explode('\\', get_called_class());
|
||||
$viewFile = end($class);
|
||||
}
|
||||
|
||||
if (file_exists($viewPath . 'Form/' . $viewFile . '.phtml')) {
|
||||
$view = new View('Form/' . $viewFile);
|
||||
} else {
|
||||
$view = new View($viewFile, SRC_DIR . 'Form/View/');
|
||||
}
|
||||
|
||||
$view->name = $this->getName();
|
||||
$view->id = $this->getId();
|
||||
$view->label = $this->getLabel();
|
||||
$view->class = $this->getClass();
|
||||
$view->containerClass = $this->getContainerClass();
|
||||
$view->parent = $this->parent;
|
||||
|
||||
$this->onPreRender($view);
|
||||
|
||||
return $view->render();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param View $view
|
||||
*/
|
||||
abstract protected function onPreRender(View &$view);
|
||||
}
|
||||
27
src/PHPCensor/Form/Element/Button.php
Normal file
27
src/PHPCensor/Form/Element/Button.php
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
<?php
|
||||
|
||||
namespace PHPCensor\Form\Element;
|
||||
|
||||
use PHPCensor\Form\Input;
|
||||
use PHPCensor\View;
|
||||
|
||||
class Button extends Input
|
||||
{
|
||||
/**
|
||||
* @return boolean
|
||||
*/
|
||||
public function validate()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param View $view
|
||||
*/
|
||||
protected function onPreRender(View &$view)
|
||||
{
|
||||
parent::onPreRender($view);
|
||||
|
||||
$view->type = 'button';
|
||||
}
|
||||
}
|
||||
67
src/PHPCensor/Form/Element/Checkbox.php
Normal file
67
src/PHPCensor/Form/Element/Checkbox.php
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
<?php
|
||||
|
||||
namespace PHPCensor\Form\Element;
|
||||
|
||||
use PHPCensor\View;
|
||||
use PHPCensor\Form\Input;
|
||||
|
||||
class Checkbox extends Input
|
||||
{
|
||||
/**
|
||||
* @var boolean
|
||||
*/
|
||||
protected $checked;
|
||||
|
||||
/**
|
||||
* @var mixed
|
||||
*/
|
||||
protected $checkedValue;
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getCheckedValue()
|
||||
{
|
||||
return $this->checkedValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $value
|
||||
*/
|
||||
public function setCheckedValue($value)
|
||||
{
|
||||
$this->checkedValue = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $value
|
||||
*/
|
||||
public function setValue($value)
|
||||
{
|
||||
if (is_bool($value) && $value === true) {
|
||||
$this->value = $this->getCheckedValue();
|
||||
$this->checked = true;
|
||||
return;
|
||||
}
|
||||
|
||||
if ($value == $this->getCheckedValue()) {
|
||||
$this->value = $this->getCheckedValue();
|
||||
$this->checked = true;
|
||||
return;
|
||||
}
|
||||
|
||||
$this->value = $value;
|
||||
$this->checked = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param View $view
|
||||
*/
|
||||
public function onPreRender(View &$view)
|
||||
{
|
||||
parent::onPreRender($view);
|
||||
|
||||
$view->checkedValue = $this->getCheckedValue();
|
||||
$view->checked = $this->checked;
|
||||
}
|
||||
}
|
||||
9
src/PHPCensor/Form/Element/CheckboxGroup.php
Normal file
9
src/PHPCensor/Form/Element/CheckboxGroup.php
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
<?php
|
||||
|
||||
namespace PHPCensor\Form\Element;
|
||||
|
||||
use PHPCensor\Form\FieldSet;
|
||||
|
||||
class CheckboxGroup extends FieldSet
|
||||
{
|
||||
}
|
||||
38
src/PHPCensor/Form/Element/Csrf.php
Normal file
38
src/PHPCensor/Form/Element/Csrf.php
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
<?php
|
||||
|
||||
namespace PHPCensor\Form\Element;
|
||||
|
||||
use PHPCensor\View;
|
||||
|
||||
class Csrf extends Hidden
|
||||
{
|
||||
/**
|
||||
* @var integer
|
||||
*/
|
||||
protected $rows = 4;
|
||||
|
||||
/**
|
||||
* @return boolean
|
||||
*/
|
||||
public function validate()
|
||||
{
|
||||
if ($this->value != $_COOKIE[$this->getName()]) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param View $view
|
||||
*/
|
||||
protected function onPreRender(View &$view)
|
||||
{
|
||||
parent::onPreRender($view);
|
||||
|
||||
$csrf = md5(microtime(true));
|
||||
$view->csrf = $csrf;
|
||||
|
||||
setcookie($this->getName(), $csrf);
|
||||
}
|
||||
}
|
||||
28
src/PHPCensor/Form/Element/Email.php
Normal file
28
src/PHPCensor/Form/Element/Email.php
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
<?php
|
||||
|
||||
namespace PHPCensor\Form\Element;
|
||||
|
||||
use PHPCensor\View;
|
||||
|
||||
class Email extends Text
|
||||
{
|
||||
/**
|
||||
* @param string $viewFile
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function render($viewFile = null)
|
||||
{
|
||||
return parent::render(($viewFile ? $viewFile : 'Text'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param View $view
|
||||
*/
|
||||
protected function onPreRender(View &$view)
|
||||
{
|
||||
parent::onPreRender($view);
|
||||
|
||||
$view->type = 'email';
|
||||
}
|
||||
}
|
||||
9
src/PHPCensor/Form/Element/Hidden.php
Normal file
9
src/PHPCensor/Form/Element/Hidden.php
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
<?php
|
||||
|
||||
namespace PHPCensor\Form\Element;
|
||||
|
||||
use PHPCensor\Form\Input;
|
||||
|
||||
class Hidden extends Input
|
||||
{
|
||||
}
|
||||
28
src/PHPCensor/Form/Element/Password.php
Normal file
28
src/PHPCensor/Form/Element/Password.php
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
<?php
|
||||
|
||||
namespace PHPCensor\Form\Element;
|
||||
|
||||
use PHPCensor\View;
|
||||
|
||||
class Password extends Text
|
||||
{
|
||||
/**
|
||||
* @param string $viewFile
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function render($viewFile = null)
|
||||
{
|
||||
return parent::render(($viewFile ? $viewFile : 'Text'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param View $view
|
||||
*/
|
||||
protected function onPreRender(View &$view)
|
||||
{
|
||||
parent::onPreRender($view);
|
||||
|
||||
$view->type = 'password';
|
||||
}
|
||||
}
|
||||
7
src/PHPCensor/Form/Element/Radio.php
Normal file
7
src/PHPCensor/Form/Element/Radio.php
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
<?php
|
||||
|
||||
namespace PHPCensor\Form\Element;
|
||||
|
||||
class Radio extends Select
|
||||
{
|
||||
}
|
||||
32
src/PHPCensor/Form/Element/Select.php
Normal file
32
src/PHPCensor/Form/Element/Select.php
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
<?php
|
||||
|
||||
namespace PHPCensor\Form\Element;
|
||||
|
||||
use PHPCensor\View;
|
||||
use PHPCensor\Form\Input;
|
||||
|
||||
class Select extends Input
|
||||
{
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $options = [];
|
||||
|
||||
/**
|
||||
* @param array $options
|
||||
*/
|
||||
public function setOptions(array $options)
|
||||
{
|
||||
$this->options = $options;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param View $view
|
||||
*/
|
||||
protected function onPreRender(View &$view)
|
||||
{
|
||||
parent::onPreRender($view);
|
||||
|
||||
$view->options = $this->options;
|
||||
}
|
||||
}
|
||||
33
src/PHPCensor/Form/Element/Submit.php
Normal file
33
src/PHPCensor/Form/Element/Submit.php
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
<?php
|
||||
|
||||
namespace PHPCensor\Form\Element;
|
||||
|
||||
use PHPCensor\View;
|
||||
|
||||
class Submit extends Button
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $value = 'Submit';
|
||||
|
||||
/**
|
||||
* @param string $viewFile
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function render($viewFile = null)
|
||||
{
|
||||
return parent::render(($viewFile ? $viewFile : 'Button'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param View $view
|
||||
*/
|
||||
protected function onPreRender(View &$view)
|
||||
{
|
||||
parent::onPreRender($view);
|
||||
|
||||
$view->type = 'submit';
|
||||
}
|
||||
}
|
||||
19
src/PHPCensor/Form/Element/Text.php
Normal file
19
src/PHPCensor/Form/Element/Text.php
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
<?php
|
||||
|
||||
namespace PHPCensor\Form\Element;
|
||||
|
||||
use PHPCensor\Form\Input;
|
||||
use PHPCensor\View;
|
||||
|
||||
class Text extends Input
|
||||
{
|
||||
/**
|
||||
* @param View $view
|
||||
*/
|
||||
protected function onPreRender(View &$view)
|
||||
{
|
||||
parent::onPreRender($view);
|
||||
|
||||
$view->type = 'text';
|
||||
}
|
||||
}
|
||||
39
src/PHPCensor/Form/Element/TextArea.php
Normal file
39
src/PHPCensor/Form/Element/TextArea.php
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
<?php
|
||||
|
||||
namespace PHPCensor\Form\Element;
|
||||
|
||||
use PHPCensor\View;
|
||||
|
||||
class TextArea extends Text
|
||||
{
|
||||
/**
|
||||
* @var integer
|
||||
*/
|
||||
protected $rows = 4;
|
||||
|
||||
/**
|
||||
* @return integer
|
||||
*/
|
||||
public function getRows()
|
||||
{
|
||||
return $this->rows;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param integer $rows
|
||||
*/
|
||||
public function setRows($rows)
|
||||
{
|
||||
$this->rows = $rows;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param View $view
|
||||
*/
|
||||
protected function onPreRender(View &$view)
|
||||
{
|
||||
parent::onPreRender($view);
|
||||
|
||||
$view->rows = $this->getRows();
|
||||
}
|
||||
}
|
||||
28
src/PHPCensor/Form/Element/Url.php
Normal file
28
src/PHPCensor/Form/Element/Url.php
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
<?php
|
||||
|
||||
namespace PHPCensor\Form\Element;
|
||||
|
||||
use PHPCensor\View;
|
||||
|
||||
class Url extends Text
|
||||
{
|
||||
/**
|
||||
* @param string $viewFile
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function render($viewFile = null)
|
||||
{
|
||||
return parent::render(($viewFile ? $viewFile : 'Text'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param View $view
|
||||
*/
|
||||
protected function onPreRender(View &$view)
|
||||
{
|
||||
parent::onPreRender($view);
|
||||
|
||||
$view->type = 'url';
|
||||
}
|
||||
}
|
||||
118
src/PHPCensor/Form/FieldSet.php
Normal file
118
src/PHPCensor/Form/FieldSet.php
Normal file
|
|
@ -0,0 +1,118 @@
|
|||
<?php
|
||||
|
||||
namespace PHPCensor\Form;
|
||||
|
||||
use PHPCensor\View;
|
||||
|
||||
class FieldSet extends Element
|
||||
{
|
||||
/**
|
||||
* @var Element[]
|
||||
*/
|
||||
protected $children = [];
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getValues()
|
||||
{
|
||||
$rtn = [];
|
||||
foreach ($this->children as $field) {
|
||||
if ($field instanceof FieldSet) {
|
||||
$fieldName = $field->getName();
|
||||
|
||||
if (empty($fieldName)) {
|
||||
$rtn = array_merge($rtn, $field->getValues());
|
||||
} else {
|
||||
$rtn[$fieldName] = $field->getValues();
|
||||
}
|
||||
} elseif ($field instanceof Input) {
|
||||
if ($field->getName()) {
|
||||
$rtn[$field->getName()] = $field->getValue();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $rtn;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $values
|
||||
*/
|
||||
public function setValues(array $values)
|
||||
{
|
||||
foreach ($this->children as $field) {
|
||||
if ($field instanceof FieldSet) {
|
||||
$fieldName = $field->getName();
|
||||
|
||||
if (empty($fieldName) || !isset($values[$fieldName])) {
|
||||
$field->setValues($values);
|
||||
} else {
|
||||
$field->setValues($values[$fieldName]);
|
||||
}
|
||||
} elseif ($field instanceof Input) {
|
||||
$fieldName = $field->getName();
|
||||
|
||||
if (isset($values[$fieldName])) {
|
||||
$field->setValue($values[$fieldName]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Element $field
|
||||
*/
|
||||
public function addField(Element $field)
|
||||
{
|
||||
$this->children[$field->getName()] = $field;
|
||||
$field->setParent($this);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return boolean
|
||||
*/
|
||||
public function validate()
|
||||
{
|
||||
$rtn = true;
|
||||
|
||||
foreach ($this->children as $child) {
|
||||
if (!$child->validate()) {
|
||||
$rtn = false;
|
||||
}
|
||||
}
|
||||
|
||||
return $rtn;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param View $view
|
||||
*/
|
||||
protected function onPreRender(View &$view)
|
||||
{
|
||||
$rendered = [];
|
||||
foreach ($this->children as $child) {
|
||||
$rendered[] = $child->render();
|
||||
}
|
||||
|
||||
$view->children = $rendered;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Element[]
|
||||
*/
|
||||
public function getChildren()
|
||||
{
|
||||
return $this->children;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $fieldName
|
||||
*
|
||||
* @return Element
|
||||
*/
|
||||
public function getChild($fieldName)
|
||||
{
|
||||
return $this->children[$fieldName];
|
||||
}
|
||||
}
|
||||
196
src/PHPCensor/Form/Input.php
Normal file
196
src/PHPCensor/Form/Input.php
Normal file
|
|
@ -0,0 +1,196 @@
|
|||
<?php
|
||||
|
||||
namespace PHPCensor\Form;
|
||||
|
||||
use PHPCensor\View;
|
||||
|
||||
class Input extends Element
|
||||
{
|
||||
/**
|
||||
* @var boolean
|
||||
*/
|
||||
protected $required = false;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $pattern;
|
||||
|
||||
/**
|
||||
* @var callable
|
||||
*/
|
||||
protected $validator;
|
||||
|
||||
/**
|
||||
* @var mixed
|
||||
*/
|
||||
protected $value;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $error;
|
||||
|
||||
/**
|
||||
* @var boolean
|
||||
*/
|
||||
protected $customError = false;
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @param string $label
|
||||
* @param boolean $required
|
||||
*
|
||||
* @return static
|
||||
*/
|
||||
public static function create($name, $label, $required = false)
|
||||
{
|
||||
$el = new static();
|
||||
$el->setName($name);
|
||||
$el->setLabel($label);
|
||||
$el->setRequired($required);
|
||||
|
||||
return $el;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getValue()
|
||||
{
|
||||
return $this->value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $value
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setValue($value)
|
||||
{
|
||||
$this->value = $value;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return boolean
|
||||
*/
|
||||
public function getRequired()
|
||||
{
|
||||
return $this->required;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param boolean $required
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setRequired($required)
|
||||
{
|
||||
$this->required = (bool)$required;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return callable
|
||||
*/
|
||||
public function getValidator()
|
||||
{
|
||||
return $this->validator;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param callable $validator
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setValidator($validator)
|
||||
{
|
||||
if (is_callable($validator) || $validator instanceof \Closure) {
|
||||
$this->validator = $validator;
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getPattern()
|
||||
{
|
||||
return $this->pattern;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $pattern
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setPattern($pattern)
|
||||
{
|
||||
$this->pattern = $pattern;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return boolean
|
||||
*/
|
||||
public function validate()
|
||||
{
|
||||
if ($this->getRequired() && empty($this->value)) {
|
||||
$this->error = $this->getLabel() . ' is required.';
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($this->getPattern() && !preg_match('/' . $this->getPattern() . '/', $this->value)) {
|
||||
$this->error = 'Invalid value entered.';
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
$validator = $this->getValidator();
|
||||
|
||||
if (is_callable($validator)) {
|
||||
try {
|
||||
call_user_func_array($validator, [$this->value]);
|
||||
} catch (\Exception $ex) {
|
||||
$this->error = $ex->getMessage();
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if ($this->customError) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $message
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setError($message)
|
||||
{
|
||||
$this->customError = true;
|
||||
$this->error = $message;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param View $view
|
||||
*/
|
||||
protected function onPreRender(View &$view)
|
||||
{
|
||||
$view->value = $this->getValue();
|
||||
$view->error = $this->error;
|
||||
$view->pattern = $this->pattern;
|
||||
$view->required = $this->required;
|
||||
}
|
||||
}
|
||||
1
src/PHPCensor/Form/View/Button.phtml
Normal file
1
src/PHPCensor/Form/View/Button.phtml
Normal file
|
|
@ -0,0 +1 @@
|
|||
<input class="btn <?= $class; ?>" type="<?= $type; ?>" value="<?= $value; ?>">
|
||||
20
src/PHPCensor/Form/View/Checkbox.phtml
Normal file
20
src/PHPCensor/Form/View/Checkbox.phtml
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
<?php if (!($parent instanceof \PHPCensor\Form\Element\CheckboxGroup)): ?>
|
||||
<div class="control-group <?= $containerClass ?> <?= (isset($error) ? 'error' : ''); ?>">
|
||||
<div class="controls">
|
||||
<div class="checkbox">
|
||||
<?php endif; ?>
|
||||
<label class="checkbox <?= $class; ?>" for="<?= $id ?>">
|
||||
<input type="checkbox" id="<?= $id; ?>" name="<?= $name; ?>"
|
||||
value="<?= $checkedValue; ?>"
|
||||
<?= ($checked ? 'checked' : ''); ?> <?= $required ? 'required' : '' ?>
|
||||
>
|
||||
<?= $label; ?>
|
||||
</label>
|
||||
<?php if (isset($error)): ?>
|
||||
<span class="help-block"><?= $error; ?></span>
|
||||
<?php endif; ?>
|
||||
<?php if (!($parent instanceof \PHPCensor\Form\Element\CheckboxGroup)): ?>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
11
src/PHPCensor/Form/View/CheckboxGroup.phtml
Normal file
11
src/PHPCensor/Form/View/CheckboxGroup.phtml
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
<div class="control-group <?= $class; ?>">
|
||||
<?php if ($label): ?>
|
||||
<label class="control-label"><?= $label; ?></label>
|
||||
<?php endif; ?>
|
||||
|
||||
<div class="controls">
|
||||
<?php foreach ($children as $field): ?>
|
||||
<?= $field; ?>
|
||||
<?php endforeach; ?>
|
||||
</div>
|
||||
</div>
|
||||
5
src/PHPCensor/Form/View/ControlGroup.phtml
Normal file
5
src/PHPCensor/Form/View/ControlGroup.phtml
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
<div class="control-group <?= $class; ?>">
|
||||
<?php foreach ($children as $field): ?>
|
||||
<?= $field; ?>
|
||||
<?php endforeach; ?>
|
||||
</div>
|
||||
1
src/PHPCensor/Form/View/Csrf.phtml
Normal file
1
src/PHPCensor/Form/View/Csrf.phtml
Normal file
|
|
@ -0,0 +1 @@
|
|||
<input type="hidden" id="<?= $id; ?>" name="<?= $name; ?>" value="<?= $csrf; ?>">
|
||||
9
src/PHPCensor/Form/View/FieldSet.phtml
Normal file
9
src/PHPCensor/Form/View/FieldSet.phtml
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
<fieldset class="row <?= $class; ?>">
|
||||
<?php if ($label): ?>
|
||||
<legend><?= $label; ?></legend>
|
||||
<?php endif; ?>
|
||||
|
||||
<?php foreach ($children as $field): ?>
|
||||
<?= $field; ?>
|
||||
<?php endforeach; ?>
|
||||
</fieldset>
|
||||
5
src/PHPCensor/Form/View/Form.phtml
Normal file
5
src/PHPCensor/Form/View/Form.phtml
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
<form id="<?= $id; ?>" class="<?= $class; ?>" action="<?= $action; ?>" method="<?= $method; ?>">
|
||||
<?php foreach ($children as $field): ?>
|
||||
<?= $field; ?>
|
||||
<?php endforeach; ?>
|
||||
</form>
|
||||
1
src/PHPCensor/Form/View/Hidden.phtml
Normal file
1
src/PHPCensor/Form/View/Hidden.phtml
Normal file
|
|
@ -0,0 +1 @@
|
|||
<input type="hidden" id="<?= $id; ?>" name="<?= $name; ?>" value="<?= $value; ?>">
|
||||
21
src/PHPCensor/Form/View/Radio.phtml
Normal file
21
src/PHPCensor/Form/View/Radio.phtml
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
<div id="<?= $id; ?>" class="control-group <?= $containerClass; ?>">
|
||||
<?php if ($label): ?>
|
||||
<label class="control-label"><?= $label; ?></label>
|
||||
<?php endif; ?>
|
||||
<div class="controls">
|
||||
<?php foreach ($options as $val => $lbl): ?>
|
||||
<label class="radio" for="radio-<?= $id; ?>-<?= $val; ?>">
|
||||
<input type="radio" id="radio-<?= $id; ?>-<?= $val; ?>" class="<?= $class; ?>"
|
||||
name="<?= $name; ?>"
|
||||
value="<?= $val; ?>"
|
||||
<?= ($value == $val) ? ' checked="checked"' : ''; ?> <?= $required ? 'required' : '' ?>
|
||||
>
|
||||
<?= $lbl; ?>
|
||||
</label>
|
||||
<?php endforeach; ?>
|
||||
|
||||
<?php if (isset($error)): ?>
|
||||
<span class="help-block"><?= $error; ?></span>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
</div>
|
||||
19
src/PHPCensor/Form/View/Select.phtml
Normal file
19
src/PHPCensor/Form/View/Select.phtml
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
<div class="control-group <?= $containerClass; ?>">
|
||||
<?php if ($label): ?>
|
||||
<label class="control-label" for="<?= $id ?>"><?= $label; ?></label>
|
||||
<?php endif; ?>
|
||||
|
||||
<div class="controls">
|
||||
<select id="<?= $id; ?>" class="<?= $class; ?>" name="<?= $name; ?>">
|
||||
<?php foreach ($options as $val => $lbl): ?>
|
||||
<option
|
||||
value="<?= $val; ?>" <?= ($value == $val) ? ' selected="selected"' : ''; ?>
|
||||
><?= $lbl; ?></option>
|
||||
<?php endforeach; ?>
|
||||
</select>
|
||||
|
||||
<?php if (isset($error)): ?>
|
||||
<span class="help-block"><?= $error; ?></span>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
</div>
|
||||
17
src/PHPCensor/Form/View/Text.phtml
Normal file
17
src/PHPCensor/Form/View/Text.phtml
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
<div class="control-group <?= $containerClass; ?> <?= (isset($error) ? 'error' : ''); ?>">
|
||||
<?php if ($label): ?>
|
||||
<label class="control-label" for="<?= $id ?>"><?= $label; ?></label>
|
||||
<?php endif; ?>
|
||||
|
||||
<div class="controls">
|
||||
<input id="<?= $id; ?>" type="<?= $type; ?>" class="<?= $class; ?>"
|
||||
name="<?= $name; ?>"
|
||||
<?= isset($value) ? ' value="' . $value . '"' : '' ?>
|
||||
<?= isset($pattern) ? ' pattern="' . $pattern . '"' : '' ?>
|
||||
<?= $required ? ' required' : '' ?>
|
||||
>
|
||||
<?php if (isset($error)): ?>
|
||||
<span class="help-block"><?= $error; ?></span>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
</div>
|
||||
13
src/PHPCensor/Form/View/TextArea.phtml
Normal file
13
src/PHPCensor/Form/View/TextArea.phtml
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
<div class="control-group <?= $containerClass; ?> <?= (isset($error) ? 'error' : ''); ?>">
|
||||
<?php if ($label): ?>
|
||||
<label class="control-label" for="<?= $id ?>"><?= $label; ?></label>
|
||||
<?php endif; ?>
|
||||
<div class="controls">
|
||||
<textarea rows="<?= $rows; ?>" id="<?= $id; ?>" class="<?= $class; ?>"
|
||||
name="<?= $name; ?>" <?= $required ? ' required' : '' ?>><?= isset($value) ? $value : '' ?></textarea>
|
||||
|
||||
<?php if (isset($error)): ?>
|
||||
<span class="help-block"><?= $error; ?></span>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
</div>
|
||||
|
|
@ -28,7 +28,7 @@ class View
|
|||
|
||||
protected static function getViewFile($file, $path = null)
|
||||
{
|
||||
$viewPath = is_null($path) ? Config::getInstance()->get('b8.view.path') : $path;
|
||||
$viewPath = is_null($path) ? (SRC_DIR . 'View/') : $path;
|
||||
$fullPath = $viewPath . $file . '.' . static::$extension;
|
||||
|
||||
return $fullPath;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue