2

我想在我的集合中添加一个新的验证器(使用 inputFilter)。

当前代码如下:

我的代码表格:

namespace EventyEvent\Form;

use Zend\Form\Element;
use Zend\Form\Form;
use Zend\InputFilter\InputFilter;
use Zend\Stdlib\Hydrator\ClassMethods as ClassMethodsHydrator;

class EventEditBasicForm extends Form
{

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

    $this   ->setName('event')
            ->setAttribute('method', 'post')
            ->setAttribute("accept-charset", "UTF-8")
            ->setHydrator(new ClassMethodsHydrator(false))
            ->setInputFilter(new InputFilter());

 // date
    $this->add(array(
        'type' => 'Zend\Form\Element\Collection',
        'name' => 'dates',
        'options' => array(
            'label' => "Dates of your event",
            'count' => 1,
            'target_element' => array(
                'type' => 'EventyEvent\Form\Basic\DateFieldset'
            )
        )
    ));
}

我的代码字段集:

namespace EventyEvent\Form\Basic;

use EventyEvent\Entity\EventDates;
use Zend\Form\Fieldset;
use Zend\InputFilter\InputFilterProviderInterface;
use Zend\Stdlib\Hydrator\ClassMethods as ClassMethodsHydrator;

class DateFieldset extends Fieldset implements InputFilterProviderInterface{

public function __construct()
{
    parent::__construct('EventDates');
    $this->setHydrator(new ClassMethodsHydrator(false))
         ->setObject(new EventDates());

    // date start
    $this->add(array(
        'name' => 'datestart',
        'attributes' => array(
            'required' => 'required',
            'type'=>'Text',
        ),
        'options'=>array(
            'label'=>"Date start",
        )
    ));

    // date end
    $this->add(array(
        'name' => 'dateend',
        'attributes' => array(
            'required' => 'required',
            'type'=>'Text',
        ),
        'options'=>array(
            'label'=>"Date end",
        )
    ));



}

/**
 * @return array
 */
public function getInputFilterSpecification()
{
    return array(
        'datestart' => array(
            'required' => true,
            'validators' => array(
                array(
                    'name' => 'Date',
                    'options' => array(
                        'format' => 'd F Y - H:i'
                    ),
                ),
            ),
        ),
        'dateend' => array(
            'required' => true,
            'validators' => array(
                array(
                    'name' => 'Date',
                    'options' => array(
                        'format' => 'd F Y - H:i'
                    ),
                ),
            ),
        ),
    );
}
}

我想在我的控制器中验证之前添加一个验证器日期,但是我可以吗?有任何提示、更正或建议吗?

4

1 回答 1

2

One way to do this is to get the validator chain for the fieldset field from the forms input filter and then just attach your own validator to the chain.

Assuming you have some imaginary DateIsLaterValidator to attach, here's an example to add that validator to the dateend field.

$form = new EventEditBasicForm;

$dateValidators = $form->getInputFilter()->get('dates')
                                         ->get('dateend')
                                         ->getValidatorChain();

$dateLaterValidator = new DateIsLaterValidator;

$dateValidators->attach($dateLaterValidator);
于 2013-05-14T03:28:05.157 回答