1
例如,我們有兩個字段,過濾器和校驗的一種形式:如何設置另一個表單域的值?
$factory = new \Zend\Form\Factory();
$form = $factory->createForm([
'elements' => [
[
'spec' => [
'name' => 'fieldOne',
'type' => 'Text',
],
],
[
'spec' => [
'name' => 'fieldTwo',
'type' => 'Text',
],
],
],
'input_filter' => [
'fieldOne' => [
'filters' => [
['name' => 'StringTrim'],
],
'validators' => [
new \Application\Validator\FieldOneValidator(),
],
],
'fieldTwo' => [
'filters' => [
['name' => 'StringToUpper'],
],
],
],
]);
如果fieldOne
是有效的,fieldTwo
是空的,那麼我們就需要濾波值形式fieldOne
設置成fieldTwo
和過濾。
$form->setData([
'fieldOne' => ' test ',
'fieldTwo' => '',
]);
if ($form->isValid()) {
$form->getData(); // ['fieldOne' => 'test', 'fieldTwo' => 'TEST']
}
$form->setData([
'fieldOne' => ' test ',
'fieldTwo' => 'not empty',
]);
if ($form->isValid()) {
$form->getData(); // ['fieldOne' => 'test', 'fieldTwo' => 'NOT EMPTY']
}
如何實現這一點?
驗證器只驗證該值,但我需要使用上下文對其進行更改。 –