我的ZF2表单字段集验证无效但表单验证正常工作我已尝试过相同的表单验证,但这些都无效。
我在字段集中实现了InputFilterProviderInterface
,但它无效。
以下是我的代码:
class CompanyCreditLimitFieldset extends Fieldset implements InputFilterProviderInterface {
protected $_curency = null;
protected $inputFilter;
public function __construct($name = null, $options = array()) {
$this
->setHydrator(new ClassMethodsHydrator(false))
->setObject(new \Application\Entity\PhvCompanyCurrencyCredit())
;
parent::__construct($name, $options);
$this->add(array(
'name' => 'credit_limit',
'type' => 'text',
'attributes' => array(
'id' => 'credit_limit',
'class' => 'form-control maxlength-simple credit_limit',
'placeholder' => 'Credit Limit'
),
'options' => array(
'label' => 'Credit Limit',
)
));
$this->add(array(
'name' => 'mininum_balance_limit',
'type' => 'text',
'attributes' => array(
'id' => 'mininum_balance_limit',
'class' => 'form-control maxlength-simple mininum_balance_limit',
'placeholder' => 'Minimum Balance Limit'
),
'options' => array(
'label' => 'Minimum Balance Limit',
)
));
}
public function getInputFilterSpecification() {
return array(
'credit_limit' => array(
'filters' => array(
array('name' => 'StringTrim')
),
'validators' => array(
array('name' => 'NotEmpty')
)
),
'mininum_balance_limit' => array(
'filters' => array(
array('name' => 'StringTrim')
),
'validators' => array(
array('name' => 'NotEmpty')
)
),
);
}
}
表格
class AddCompanyForm extends AbstractForm implements InputFilterAwareInterface {
protected $inputFilter;
protected $dbAdapter;
private $_country = null;
public function __construct($id = null, $name = null) {
$this->entity = new \Application\Entity\PhvCompany();
parent::__construct($name);
$this
->setHydrator(new ClassMethodsHydrator(false))
->setObject(new \Application\Entity\PhvCompany())
;
$this->__addElements();
}
private function __addElements() {
$this->setAttribute('method', 'post');
$this->add(array(
'type' => 'Zend\Form\Element\Collection',
'name' => 'creditlimitfieldset',
'options' => array(
'label' => 'Credit Limits',
'count' => 3,
'allow_add' => true,
'should_create_template' => true,
'template_placeholder' => '__placeholder__',
'target_element' => array(
'type' => 'Application\Form\Fieldset\CompanyCreditLimitFieldset',
),
),
// 'type' => 'Application\Form\Fieldset\CompanyCreditLimitFieldset',
// 'options' => array('label' => 'Credit Limits',)
));
$this->add(array(
'name' => 'submit',
'attributes' => array(
'type' => 'submit',
'value' => 'Save',
'class' => 'btn btn-inline btn-secondary btn-rounded'
)
));
}
public function setDbAdapter($dbAdapter) {
$this->dbAdapter = $dbAdapter;
}
public function setInputFilter(InputFilterInterface $inputFilter) {
throw new \Exception("Not used");
}
}
你能帮我解决这个问题。
答案 0 :(得分:0)
坦率地说,这种类型的问题就是为什么我总是构建一个与Form结构相匹配的独立InputFilter层次结构,而不是依赖于ZF" magic"输入过滤器构建器这是一篇关于如何做到这一点的精彩文章:
以这种方式处理集合时,您需要将CollectionInputFilter添加到表单的每个Collection表单元素的输入过滤器中。该课程完全没有文件记录IIRC,但您can find an example here和the class here。
我写了一个简短的剧本,可以复制你所看到的内容:
user@user-host:/tmp/1$ join file1.txt file2.txt | join - file3.txt | awk -v OFS=' ' 'NR==1{for (i=1;i<=NF;i++)if ($i=="IID"){n=i-1;m=NF-(i==NF)}} {for(i=1;i<=NF;i+=1+(i==n))printf "%s%s",$i,i==m?ORS:OFS}' | column -t
FID IID SEX PHENOTYPE KIR2DL5 KIR2DS5 Bw4 INFO
A1 A1 0 1 1 2 2 0.4
A2 A2 1 2 1 2 1 0.6
A3 A3 0 1 1 1 1 0.2
当我运行它(PHP 7.0.9)时,我得到了这个:
<?php
<<<CONFIG
packages:
- "zendframework/zend-form: ^2.0"
- "zendframework/zend-servicemanager: ^3.0"
- "zendframework/zend-hydrator: ^2.0"
CONFIG;
// Run this script with Melody: http://melody.sensiolabs.org/
use Zend\Form\Fieldset;
use Zend\InputFilter\InputFilterProviderInterface;
use Zend\Form\Form;
class ChildFieldset extends Fieldset implements InputFilterProviderInterface
{
public function __construct($name = null, $options = array())
{
parent::__construct($name, $options);
$this->add(array(
'name' => 'fieldA',
'type' => 'text',
'attributes' => array(
'id' => 'fieldA',
),
'options' => array(
'label' => 'Field A',
)
));
}
public function getInputFilterSpecification()
{
return array(
'fieldA' => array(
'required' => true,
'allow_empty' => false,
'continue_if_empty' => false,
'filters' => array(
array('name' => 'StringTrim')
),
'validators' => array(
array('name' => 'NotEmpty')
)
),
);
}
}
class ParentForm extends Form
{
public function __construct($name, array $options)
{
parent::__construct($name, $options);
$this->add(array(
'type' => 'Zend\Form\Element\Collection',
'name' => 'childForms',
'options' => array(
'label' => 'Child Forms',
'allow_add' => true,
'should_create_template' => true,
'template_placeholder' => '__placeholder__',
'target_element' => array(
'type' => 'ChildFieldset',
),
),
));
$this->add(array(
'name' => 'submit',
'attributes' => array(
'type' => 'submit',
'value' => 'Save',
)
));
}
}
$form = new ParentForm('foo', []);
$data = [];
$form->setData($data);
echo "Dataset: " . print_r($data, true) . "\n";
echo "Result: " . ($form->isValid() ? 'valid' : 'invalid');
echo "\n\n--------------------------------------\n\n";
$data = [
'childForms' => []
];
$form->setData($data);
echo "Dataset: " . print_r($data, true) . "\n";
echo "Result: " . ($form->isValid() ? 'valid' : 'invalid');
echo "\n\n--------------------------------------\n\n";
$data = [
'childForms' => [
['fieldA' => ''],
],
];
$form->setData($data);
echo "Dataset: " . print_r($data, true) . "\n";
echo "Result: " . ($form->isValid() ? 'valid' : 'invalid') . "\n\n";
echo "\n\n--------------------------------------\n\n";
$data = [
'childForms' => [
['fieldA' => 'fff'],
],
];
$form->setData($data);
echo "Dataset: " . print_r($data, true) . "\n";
echo "Result: " . ($form->isValid() ? 'valid' : 'invalid') . "\n\n";
子字段集上的输入过滤器仅在数据中存在完整的层次结构时触发。它不是本身的错误,但它不是您期望发生的。