Created
December 3, 2012 12:14
-
-
Save caferrari/4194650 to your computer and use it in GitHub Desktop.
trait getInputFilter
This file contains hidden or 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 | |
namespace Core\Form; | |
use Zend\Form\Form; | |
abstract class AbstractForm extends Form | |
{ | |
use \Core\Traits\getInputFilter; | |
} |
This file contains hidden or 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 | |
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' | |
) | |
) | |
); | |
} | |
} |
This file contains hidden or 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 | |
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); | |
} | |
} |
This file contains hidden or 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 | |
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