문제

I would like add a new validator (using inputFilter) into my collection.

The current code is as follows:

My Code Form:

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'
            )
        )
    ));
}

My Code Fieldset:

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'
                    ),
                ),
            ),
        ),
    );
}
}

I want to add a validator date is later before validation in my controller, but can I? Any tips, corrections or suggestions?

도움이 되었습니까?

해결책

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);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top