Skip to content

Instantly share code, notes, and snippets.

@eminetto
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eminetto/d0354d3992d704ea08b1 to your computer and use it in GitHub Desktop.
Save eminetto/d0354d3992d704ea08b1 to your computer and use it in GitHub Desktop.
<?php
namespace Application\Model;
use Zend\InputFilter\InputFilter;
use Zend\InputFilter\InputFilterAwareInterface;
use Zend\InputFilter\InputFilterInterface;
class Post implements InputFilterAwareInterface
{
public $id;
public $title;
public $description;
public $post_date;
//usado pelo TableGateway
public function exchangeArray($data)
{
$this->id = (!empty($data['id'])) ? $data['id'] : null;
$this->title = (!empty($data['title'])) ? $data['title'] : null;
$this->description = (!empty($data['description'])) ? $data['description'] : null;
$this->post_date = (!empty($data['post_date'])) ? $data['post_date'] : null;
}
// Exigido pela implementação da interface InputFilterAwareInterface
public function setInputFilter(InputFilterInterface $inputFilter)
{
throw new \Exception("Não usado");
}
/**
* Configura os filtros dos campos da classe
*
* @return Zend\InputFilter\InputFilter
*/
public function getInputFilter()
{
if (!$this->inputFilter) {
$inputFilter = new InputFilter();
$inputFilter->add(array(
'name' => 'id',
'required' => true,
'filters' => array(
array('name' => 'Int'),
),
));
$inputFilter->add(array(
'name' => 'title',
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 1,
'max' => 100,
),
),
),
));
$inputFilter->add(array(
'name' => 'description',
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
));
$inputFilter->add(array(
'name' => 'post_date',
'required' => false,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
));
$this->inputFilter = $inputFilter;
}
return $this->inputFilter;
}
//necessário para o uso dos forms
public function getArrayCopy()
{
return get_object_vars($this);
}
}
@stopassola
Copy link

Grande Minetto

No meu ambiente (PHP 5.5.14), na linha 38, ele gera um “Notice” e consequentemente não faz o redirecionamento (header). Ex.:
Notice: Undefined property: Application\Model\Post::$inputFilter in /Applications/MAMP/htdocs/iniciando-zf2/module/Application/src/Application/Model/Post.php on line 38

Sugiro alterar a linha de:
if (!$this->inputFilter) {
para
if (!isset($this->inputFilter)) {

Obrigado por compartilhar desse tutorial rápido de ZF2!
Abração
Ari Jr.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment