Zend Form Element Select - 如何得到 return 整数?
Zend Form Element Select - how to return integer?
有什么方法可以将Zend\Form\Element\Select配置为return整数吗?
如果我有一个带有 Select 元素的表单(这是根据文档和我的互联网研究配置 Select 元素的常用方法):
$this->add(array(
'name' => 'category_id',
'type' => 'Zend\Form\Element\Select',
'options' => array(
'label' => 'Category',
'value_options' => array(
'1' => 'Gold',
'2' => 'Silver',
'3' => 'Diamond',
'4' => 'Charm'
),
'attributes' => array(
'class' => 'form-control',
),
));
我想如果我像这样更改值选项:
$this->add(array(
'name' => 'category_id',
'type' => 'Zend\Form\Element\Select',
'options' => array(
'label' => 'Category',
'value_options' => array(
1 => 'Gold',
2 => 'Silver',
3 => 'Diamond',
4 => 'Charm'
),
'attributes' => array(
'class' => 'form-control',
),
));
整数将被 returned 但我错了。在这两种情况下,字符串都是 returned。我的 php 代码将此表单值写入数据库 table,其中 category_id 定义为 int.
在 ZF2 中,根据您使用的 ZF2 版本使用 Zend\Filter\Int or Zend\Filter\ToInt,Zend\Filter\Int 在 ZF2.4 中已弃用。
在您的表单中,假设您使用的是 Zend\InputFilter\InputFilterProviderInterface 使用:
public function getInputFilterSpecification()
{
return array(
'category_id' => array(
'required' => TRUE,
'filters' => array(
array('name' => 'Int'),
),
'validators' => array(
// Your validators here
),
),
);
}
有什么方法可以将Zend\Form\Element\Select配置为return整数吗? 如果我有一个带有 Select 元素的表单(这是根据文档和我的互联网研究配置 Select 元素的常用方法):
$this->add(array(
'name' => 'category_id',
'type' => 'Zend\Form\Element\Select',
'options' => array(
'label' => 'Category',
'value_options' => array(
'1' => 'Gold',
'2' => 'Silver',
'3' => 'Diamond',
'4' => 'Charm'
),
'attributes' => array(
'class' => 'form-control',
),
));
我想如果我像这样更改值选项:
$this->add(array(
'name' => 'category_id',
'type' => 'Zend\Form\Element\Select',
'options' => array(
'label' => 'Category',
'value_options' => array(
1 => 'Gold',
2 => 'Silver',
3 => 'Diamond',
4 => 'Charm'
),
'attributes' => array(
'class' => 'form-control',
),
));
整数将被 returned 但我错了。在这两种情况下,字符串都是 returned。我的 php 代码将此表单值写入数据库 table,其中 category_id 定义为 int.
在 ZF2 中,根据您使用的 ZF2 版本使用 Zend\Filter\Int or Zend\Filter\ToInt,Zend\Filter\Int 在 ZF2.4 中已弃用。
在您的表单中,假设您使用的是 Zend\InputFilter\InputFilterProviderInterface 使用:
public function getInputFilterSpecification()
{
return array(
'category_id' => array(
'required' => TRUE,
'filters' => array(
array('name' => 'Int'),
),
'validators' => array(
// Your validators here
),
),
);
}