在我的ZF2應用程序中,我有一個帶有幾個可選字段的表單。如果用戶決定將該字段留空,那麼它應該在db中將其設置爲NULL,但是 - 它不起作用。ZF2 - inputFilter - >將值設置爲空
例字段:
$this->add(array(
'name' => 'productId',
'type' => 'select',
'options' => array(
'value_options' => array(
'global' => 'Global option', // this should be null
'mobile' => 'Another option'
)
),
));
和濾波:
$inputFilter->add(array(
'name' => 'productId',
'validators' => array(
array(
'name' => 'Callback',
'options' => array(
// 'messages' => array(
// \Zend\Validator\Callback::INVALID_VALUE => $value,
//),
'callback' => function ($value) {
if($value === 'global')
{
$value = null;
//return true;
// echo '<pre>' . var_export($value, true) . '</pre>';
// die();
}
}
),
),
),
));
當選擇 「移動」 選項時,此片的代碼的工作。但是,使用「全局」選項它不起作用。正如你所看到的,我已經做了一些基本的調試,以確保該值在回調時被覆蓋,並且確實返回NULL。不管驗證的說:
array ( 'productId' => array ( 'callbackValue' => 'The input is not valid', ), )
於是,我就在回調導致如下的錯誤返回true:
Statement could not be executed (23000 - 1452 - Cannot add or update a child row: a foreign key constraint fails (
app
.codes
, CONSTRAINTfk_products_id
FOREIGN KEY (productId
) REFERENCESproducts
(id
)))
我應該如何爲null PARAM傳遞給我的分貝?如果我完全從表單中刪除這個字段,那麼它將被忽略,並且一切正常。