I have a problem with the ZF2 Form validation. The isValid() return is false always I use a checkbox in my form. If I change that element to a text element, it works normally. Below is my code. Can anyone help me?
Thank you!
Entity/Marca.php
namespace Application\Entity;
class Marca
{
/**
* @var string
*/
protected $select;
/**
* @var string
*/
protected $descr;
function getSelect()
{
return $this->select;
}
function getDescr()
{
return $this->descr;
}
function setSelect($select)
{
$this->select = $select;
return $this;
}
function setDescr($descr)
{
$this->descr = $descr;
return $this;
}
}
Form/MarcaFieldset.php
namespace Application\Form;
use Application\Entity\Marca;
use Zend\Form\Fieldset;
use Zend\InputFilter\InputFilterProviderInterface;
use Zend\Stdlib\Hydrator\ClassMethods as ClassMethodsHydrator;
class MarcaFieldset extends Fieldset implements InputFilterProviderInterface
{
public function __construct()
{
parent::__construct('brand');
$this->setHydrator(new ClassMethodsHydrator(false))
->setObject(new Marca());
/*$this->add(array(
'name' => 'select',
'type' => 'Text',
'options' => array(
'label' => 'Adicionar',
),
));*/
$this->add(array(
'type' => 'Checkbox',
'name' => 'select',
'options' => array(
'label' => 'Adicionar',
'use_hidden_element' => true,
'checked_value' => '1',
'unchecked_value' => '0'
),
));
$this->add(array(
'name' => 'descr',
'type' => 'Text',
'options' => array(
'label' => 'Descrição',
),
));
}
/**
* @return array
*/
public function getInputFilterSpecification()
{
return array();
}
}
Form/MarcaFieldsetFilter.php
namespace Application\Form;
use Zend\InputFilter\InputFilter;
use Zend\InputFilter\Factory as InputFactory;
class MarcaFieldsetFilter extends InputFilter
{
public function __construct()
{
$factory = new InputFactory();
$name = $factory->createInput(array(
'name' => 'select',
'required' => false,
));
$descr = $factory->createInput(array(
'name' => 'descr',
));
$descr->getValidatorChain()
->attach(new \Zend\Validator\NotEmpty(\Zend\Validator\NotEmpty::NULL))
->attach(new \Zend\Validator\Callback(array(
'message' => 'Campo obrigatório.',
'callback' => function ($value) use ($name) {
return !(empty($value) && $name->getValue() == 1);
},
)));
$this->add($name);
$this->add($descr);
}
}
Form/MarcaForm.php
namespace Application\Form;
use Zend\Form\Form;
use Zend\Stdlib\Hydrator\ClassMethods as ClassMethodsHydrator;
class MarcaForm extends Form
{
public function __construct()
{
parent::__construct('marca-form');
$this->setAttribute('method', 'post')
->setHydrator(new ClassMethodsHydrator(false))
->setInputFilter(new MarcaFormFilter());
$this->add(array(
'type' => 'Zend\Form\Element\Collection',
'name' => 'marcas',
'options' => array(
'label' => 'Informe as Marcas',
'count' => 3,
'should_create_template' => true,
'allow_add' => true,
'target_element' => array(
'type' => 'Application\Form\MarcaFieldset'
)
)
));
$this->add(array(
'type' => 'Zend\Form\Element\Csrf',
'name' => 'csrf'
));
$this->add(array(
'name' => 'submit',
'attributes' => array(
'type' => 'submit',
'value' => 'Gravar'
)
));
}
}
Form/MarcaFormFilter.php
namespace Application\Form;
use Zend\InputFilter\InputFilter;
use Zend\InputFilter\CollectionInputFilter;
class MarcaFormFilter extends InputFilter
{
public function __construct()
{
$marcaFilter = new CollectionInputFilter();
$marcaFilter->setInputFilter(new MarcaFieldsetFilter());
$this->add($marcaFilter, 'marcas');
}
}
Controller/IndexController.php
namespace Application\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Application\Form\MarcaForm;
use Application\Entity\Marca;
class IndexController extends AbstractActionController
{
public function indexAction()
{
$form = new MarcaForm();
$marca = new Marca();
$form->bind($marca);
$request = $this->getRequest();
if ($request->isPost()) {
$form->setData($request->getPost());
if ($form->isValid()) {
echo 'Form válido!';
} else {
echo 'Form inválido! ';
print_r($form->getMessages());
}
}
return array(
'form' => $form,
);
}
}
view index.phtml
<?php
$form = $this->form;
$form->setAttribute('action', $this->url('home'))->prepare();
$marca = $form->get('marcas');
?>
<div class="panel panel-primary">
<div class="panel-heading">
<div class="panel-title">
Classes
</div>
</div>
<?php echo $this->form()->openTag($form); ?>
<div class="panel-body">
<?php foreach ($marca as $m) : ?>
<div class="form-group">
<?php echo $this->formLabel($m->get('select'));?>
<div class="col-lg-9 col-md-9">
<?php //echo $this->formInput($m->get('select'));
echo $this->formElement($m->get('select'));
echo $this->formElementErrors()
->setMessageOpenFormat('<small class="text-danger">')
->setMessageSeparatorString('</small><br/><small class="text-danger">')
->setMessageCloseString('</small>')
->render($m->get('select'));
?>
</div>
</div>
<div class="form-group">
<?php echo $this->formLabel($m->get('descr'));?>
<div class="col-lg-9 col-md-9">
<?php echo $this->formInput($m->get('descr'));
echo $this->formElementErrors()
->setMessageOpenFormat('<small class="text-danger">')
->setMessageSeparatorString('</small><br/><small class="text-danger">')
->setMessageCloseString('</small>')
->render($m->get('descr'));
?>
</div>
</div>
<?php endforeach; ?>
</div>
<div class="panel-footer">
<?php
echo $this->formHidden($form->get('csrf'));
echo $this->formElement($form->get('submit'));
echo $this->form()->closeTag();
?>
</div>
</div>
Aucun commentaire:
Enregistrer un commentaire