0

我正在研究 Zend Framework 2,尤其是 Zend Forms。我已经声明了一个选择下拉框

形式:

$selectElement = new Element\Select('selectElement');
    $selectElement->setAttribute('title', 'Select a Value')
            ->setAttribute('id', 'id');

    $data = array(
        array(
           //Fetching the values from database
            ),
    );

   $selectElement->setAttribute('multiple', 'multiple')      
        ->setValueOptions($data);

   $this->add($selectElement);

输入过滤器:

$inputFilter->add($factory->createInput(array(
                'name'     => 'selectElement',
                'required' => false,
                'filters'  => array(
                    array(
                        'name' => 'Int'
                        ),
                    ),
           )));

我使用 Zend Debug 以这种方式获取 selectElement 保管箱中的值:

$dataSelectElements = $this->getRequest()->getPost('selectElement');
 \Zend\Debug\Debug::dump($dataSelectElements);

调试结果:

array(4) {
  [0] => string(2) "20"
  [1] => string(2) "22"
  [2] => string(2) "23"
  [3] => string(2) "75"
}

基本上我从 selectElement 表单中获取 id 以将其存储在数据库中。现在我收到通知和 zend 表单错误:

注意错误:

Notice: Array to string conversion in ..\zendframework\zendframework\library\Zend\Filter\Int.php on line 29

还有一个表单无效错误:

array(1) {
  [0] => array(1) {
    ["selectElement "] => array(1) {
      ["explodeInvalid"] => string(35) "Invalid type given. String expected"
    }
  }
}

有没有办法解决这个问题。任何帮助,将不胜感激。

4

3 回答 3

1

过滤器将Int尝试从您的数据数组中生成一个整数,这是行不通的。

以前我使用过回调过滤器,它可以用来循环遍历数据并检查每个值是否为 Int。

例如:

'filters' => array(
    array(
        'name' => 'Callback',
        'options' => array(
            'callback' => function($values) {
                return array_filter($values, function($value) {
                    return ((int)$value == $value);
                });
            }
        )
    ),
),
于 2012-10-29T12:50:15.090 回答
0

我做的有点不同,像这样

形式

class Companyform extends Form

    {
        public function __construct()
        {
            // we want to ignore the name passed
            parent::__construct('company');

            $this->setAttribute ('method', 'post');
            $this->setAttribute ('class', 'form-horizontal');
    $this->add ( array (
                'name' => 'parentID',
                'type' => 'Zend\Form\Element\Select',        
                'attributes' => array(
                    'id' => 'parentID',
                    'type' => 'select',
                    'placeholder' => "Parent Company",
                ),
                'options' => array(
                    'label' => 'Parent Company'
                )
            ));

            $this->add(array(
                'name' => 'btnsubmit',
                'attributes' => array(
                    'id' => 'btnsubmit',
                    'type' => 'submit',
                    'value' => 'Add',   
                    'class' => 'btn btn-primary'
                ),
            ));

        }

    }

控制器

public function addAction() 
{        
    $request = $this->getRequest();

    $companyList = $this->_getCompanyList();         

    $form = new Companyform();                  

    $form->get('parentID')->setAttribute('options',$companyList);


    if ($request->isPost()) 
    {
        $company = new Company();   
            $form->setInputFilter($company->getInputFilter());            

            $form->setData($request->getPost());

            if ($form->isvalid()) 
            {                 
            }

    }
}

public function _getCompanyList()
{
     $companies = $this->Em()->getEntityManager()->getRepository('XXXX\Entity\Company')->findBy(array('isDeleted'=>'0'));
     $companyIDList = array();
     $companyIDList[0] = "No Parent";
     foreach ($companies as $company) 
     {        
       $companyIDList[$company->id] = $company->companyName;
     }         
     return $companyIDList;        
}

实体类

 protected $inputFilter;

    public function setInputFilter(InputFilterInterface $inputFilter)
    {
        throw new \Exception("Not used");
    }

    public function getInputFilter()
    {
        if (!$this->inputFilter) {
            $inputFilter = new InputFilter();

            $factory = new InputFactory();

            $inputFilter->add($factory->createInput(array(
                'name' => 'companyName',
                'required' => true,
                'filters' => array(
                    array('name' => 'StripTags'),
                    array('name' => 'StringTrim'),
                ),
                'validators' => array(
                    array(
                        'name' => 'StringLength',
                        'options' => array(
                            'encoding' => 'UTF-8',
                            'min' => 2,
                            'max' => 255,
                        ),
                    ),
                ),
            )));

            $this->inputFilter = $inputFilter;
        }

        return $this->inputFilter;
    }

您可能需要在实体中添加以下库

use Zend\InputFilter\InputFilter;
use Zend\InputFilter\Factory as InputFactory;
use Zend\InputFilter\InputFilterAwareInterface;
use Zend\InputFilter\InputFilterInterface;
于 2012-10-29T16:31:07.427 回答
0

在 ZendFramework 2 中,当您从表单文件创建(添加)元素时,请检查属性:inarrayvalidator 是否为 true

        $this->add(array(
           'name' => 'select_name',
           'type' => 'select',
           'id' => 'select_name',
           'options' => array(
                 'label' => 'Select Name',
            ),
            'attributes' => array(
                 'id' => 'select_id',
                  'inarrayvalidator' => true,
             ),
        )); 

我希望,这行得通...

于 2013-12-05T12:23:09.883 回答