Skip to content

Instantly share code, notes, and snippets.

@caferrari
Created December 3, 2012 12:14
Show Gist options
  • Save caferrari/4194650 to your computer and use it in GitHub Desktop.
Save caferrari/4194650 to your computer and use it in GitHub Desktop.
trait getInputFilter
<?php
namespace Core\Form;
use Zend\Form\Form;
abstract class AbstractForm extends Form
{
use \Core\Traits\getInputFilter;
}
<?php
namespace Clinica\Form;
use Core\Form\AbstractForm;
class Especie extends AbstractForm
{
public function __construct()
{
parent::__construct('especie');
$this->setInputFilter($this->getInputFilter());
$this->setAttribute('method', 'post');
$this->add(array(
'name' => 'id',
'type' => 'hidden'
));
$this->add(array(
'name' => 'nome',
'type' => 'text',
'options' => array(
'label' => 'Nome'
),
'attributes' => array(
'placeholder' => 'Nome da espécie'
)
)
);
}
}
<?php
namespace Core\Traits;
use \RuntimeException;
trait getInputFilter
{
protected $inputFilter;
public function getInputFilter()
{
if (null == $this->inputFilter) {
$filter = $this->getInputFilterClassName();
if (!class_exists($filter)) throw new RuntimeException ("Filter \"{$filter}\" not found");
$this->inputFilter = new $filter();
}
return $this->inputFilter;
}
private function getInputFilterClassName()
{
$classParts = explode('\\', get_called_class());
$classParts[1] = 'Filter';
return implode('\\', $classParts);
}
}
<?php
namespace Clinica\Filter;
use Zend\InputFilter\InputFilter;
class Especie extends InputFilter
{
public function __construct()
{
$this->add(
array(
'name' => 'nome',
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim')
),
'validators' => array(
array(
'name' => 'NotEmpty',
'options' => array(
'messages' => array('isEmpty' => 'Digite um nome!')
)
)
)
)
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment