Validation ZF2-使用字段集时的验证程序上下文

Validation ZF2-使用字段集时的验证程序上下文,validation,zend-framework2,fieldset,Validation,Zend Framework2,Fieldset,有没有一种方法可以在字段集元素的验证器中访问整个表单的上下文?标准行为似乎是上下文只包含元素所在的字段集的表单数据 这是我的自定义验证器: /** * Returns if the given value is valid * * @param string $value * @param array $context * @return boolean */ public function isValid($value, array $context = array())

有没有一种方法可以在字段集元素的验证器中访问整个表单的上下文?标准行为似乎是上下文只包含元素所在的字段集的表单数据

这是我的自定义验证器:

/**
 * Returns if the given value is valid
 *
 * @param  string  $value
 * @param  array   $context
 * @return boolean
 */
public function isValid($value, array $context = array())
{
    $this->setValue($value);

    $bookingId = isset($context['auftrag_buchung_id'])
        ? (int) $context['auftrag_buchung_id']
        : null;

    try {
        $anlieferung = new \DateTime($value);
    } catch (\Exception $e) {
        $this->error(self::NOT_DATETIME);
        return false;
    }

    try {
        $booking = $this->table->getById($this->user, $bookingId);
    } catch (\Exception $e) {
        $this->error(self::NOT_FOUND);
        return false;
    }

    if (!$this->isInBookingRange($value, $booking)) {
        $this->error(self::NOT_IN_BOOKINGRANGE);
        return false;
    }

    return true;
}

我的问题是$context只有我配置这个验证器的字段集的表单数据。但是我必须检查字段集之外的值,在使用InputFilter将验证程序分配给表单时,应该可以将表单实例注入验证程序

// 
$form = new \Zend\Form\Form();

/* ..following could be within __construct() Method of the Form object */

// 
$element = new \Zend\Form\Element\Text('text');
$form->add($element);

// 
$inputFilter = new \Zend\InputFilter\InputFilter();

// 
$textInput = new \Zend\InputFilter\Input('text');

// Add validator with injected form instance
$textInput->getValidatorChain()
          ->attach(new CustomValidator($form));

$inputFilter->add($textInput)

//  
$form->setInputFilter($inputFilter);
通过这些设置,您应该能够在验证类中使用表单数据:

class CustomValidator {

    private $form;

    public function __construct($form) {
        $this->form = $form;
    }

    public function isValid($value) {

        // Access form data
        $this->form->getData();
    }
}

请发布一些代码,以便我们更好地理解您的问题。我认为这是不可能的,我能够做到的唯一方法是将其添加到父字段集的元素中。