Created
September 24, 2014 17:57
-
-
Save ziadoz/b1a96cf66c41777ba883 to your computer and use it in GitHub Desktop.
Aura Input / Filter Wrapper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* A simple wrapper around Aura Input. | |
*/ | |
use Aura\Input\Form; | |
use Aura\Input\Builder; | |
use Aura\Input\Filter; | |
use Aura\Html\HelperLocatorFactory; | |
use Aura\Filter\FilterFactory; | |
class AbstractForm extends Form | |
{ | |
/** | |
* The Aura HTML helper. | |
* | |
* @var HelperLocator | |
**/ | |
protected $helper; | |
/** | |
* The form's namespace. | |
* | |
* @var string | |
**/ | |
protected $namespace; | |
/** | |
* Construct a new form. | |
* | |
* @var string | |
**/ | |
public function __construct($options = null) | |
{ | |
$builder = new Builder(); | |
$filter = new FilterFactory(); | |
$filter = $filter->newInstance(); | |
$helper = new HelperLocatorFactory(); | |
$helper = $helper->newInstance(); | |
$this->builder = $builder; | |
$this->filter = $filter; | |
$this->helper = $helper; | |
$this->options = $options; | |
$this->init(); | |
} | |
/** | |
* Get an Aura HTML helper object. | |
* | |
* @return HelperLocator | |
**/ | |
public function getHelper() | |
{ | |
return $this->helper; | |
} | |
/** | |
* Set the Aura HTML helper object. | |
* | |
* @param HelperLocator $helper | |
* @return void | |
**/ | |
public function setHelper(HelperLocator $helper) | |
{ | |
$this->helper = $helper; | |
} | |
/** | |
* Set the form namespace. | |
* | |
* @param string $namespace | |
* @return void | |
**/ | |
public function setNamespace($namespace) | |
{ | |
$this->namespace = $namespace; | |
} | |
/** | |
* Get the form namespace. | |
* | |
* @return string | |
**/ | |
public function getNamespace() | |
{ | |
return $this->namespace; | |
} | |
/** | |
* Get a form input by name without throwing exceptions (for Twig templates). | |
* | |
* @param string $name | |
* @return array | |
**/ | |
public function getInput($name) | |
{ | |
if (! isset($this->inputs[$name])) { | |
return false; | |
} | |
return parent::getInput($name); | |
} | |
/** | |
* Get the form inputs. | |
* | |
* @return array | |
**/ | |
public function getInputs() | |
{ | |
$inputs = array(); | |
foreach ($this->getInputNames() as $name) { | |
$inputs[] = $this->getInput($name); | |
} | |
return $inputs; | |
} | |
/** | |
* Get a namespaced field name for a form input. | |
* | |
* @param string $name | |
* @return string | |
**/ | |
public function getInputNamespace($name) | |
{ | |
if (isset($this->namespace) && ! empty($this->namespace)) { | |
return $this->namespace . '[' . $name . ']'; | |
} | |
return $name; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"require": { | |
"aura/input": "1.*", | |
"aura/filter": "1.*" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$form = new Form(); | |
$form->fill($_POST); | |
if ($form->filter()) { | |
echo 'valid'; | |
} | |
$helper = $form->getHelper(); | |
echo $helper->label('Name'); | |
echo $helper->input($form->get('name')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment