如何将新的验证器添加到集合中(fieldset)

时间:2013-05-13 19:28:32

标签: php zend-framework2 zend-form zend-inputfilter

我想在我的收藏中添加一个新的验证器(使用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'
                    ),
                ),
            ),
        ),
    );
}
}

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

1 个答案:

答案 0 :(得分:2)

执行此操作的一种方法是从表单输入过滤器获取fieldset字段的验证器链,然后将您自己的验证器附加到链。

假设您有一些虚构的DateIsLaterValidator要附加,这是一个将该验证器添加到dateend字段的示例。

$form = new EventEditBasicForm;

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

$dateLaterValidator = new DateIsLaterValidator;

$dateValidators->attach($dateLaterValidator);