Form via AnnotationBuilder in extended form doesn't use validators and filters #4220

Closed
jirinapravnik opened this Issue Apr 13, 2013 · 3 comments

4 participants

@jirinapravnik

I create form via AnnotationBuilder and I want to add new elements to it. I can use it in controller and add new elements there. But it's not nice approach to add new elements in the controller and it's better add them in the extended form. Inputs are added sucessfuly, but filters and validators don't wok.

Entity:

use Zend\Form\Annotation as Form;
/** 
 * @Form\Hydrator("Zend\Stdlib\Hydrator\ObjectProperty")
 * 
 */
class Category {

    /**
     * @Form\Type("Zend\Form\Element\Hidden");
     */
    protected $id;

    /**
     * @Form\Required(true)
     * @Form\Filter({"name" : "StringTrim"})
     */
    protected $name;   
}

Form:

class Edit extends Form {

    public function __construct() {
        parent::__construct('categoryMain');


        $builder = new AnnotationBuilder();
        $form = $builder->createForm(new Category());
        $this->add($form);

        $this->add(
                array(
                    'type' => 'submit',
                    'name' => 'save'
                )
        );
  }
}

In controller:

$form = new Edit();
$form->isValid();

Return always true (even if the name element is empty).

@samsonasik

you should use * @Annotation\Filter({"name":"StringTrim"})

@jirinapravnik

It is not that problem, I have Form\Annotation namespace as: use Zend\Form\Annotation as Form; . I updated issue

@adamlundrigan
Zend Framework member

Not able to reproduce this. I aped one of the existing tests in AnnotationBuilderTest (and it's annotated form) to verify it:

public function testInputFilterAnnotationsActuallyWork()
{
    $entity = new TestAsset\Annotation\EntityWithInputFilterAnnotation();
    $builder = new Annotation\AnnotationBuilder();
    $form = $builder->createForm($entity);
    $inputFilter = $form->getInputFilter();

    $this->assertCount(2, $inputFilter->get('username')->getValidatorChain());
    $this->assertCount(1, $inputFilter->get('password')->getValidatorChain());

    $form->setData(['username' => '', 'password' => '']);
    $this->assertFalse($form->isValid());

    $form->setData(['username' => '123456', 'password' => 'foo@bar.com']);
    $this->assertTrue($form->isValid());
}

@Maks3w this can be closed

@Maks3w Maks3w closed this Oct 21, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment