Zend 框架 2 与原则 2 在控制器中$form->getData() 不返回使用 $form->setData() 设置的所有字段



我已经使用$form->setData()设置了我的表单以进行验证。验证后,我没有使用 $form->getData() 收到我的所有属性。

我在控制器中使用以下行不知何故$form->getData()没有返回所有字段,任何人都不知道为什么?

 if ($request->isPost()) 
 {  
   $company = new Company();   
   $form->setInputFilter($company->getInputFilter());                        
   $form->setData($request->getPost()); 
   print_r($request->getPost());      // getPost shows all fields fine  
   if ($form->isvalid()) 
   {                   
      print_r($form->getData()); // is returning only select and text type fields which are in input filter. why?
   }
}

我的形式看起来像这样。

class Companyform extends Form
{
    public function __construct()
    {        
        parent::__construct('company');
        $this->setAttribute ('method', 'post');
        $this->setAttribute ('class', 'form-horizontal');
        $this->add(array(
           'name' => 'id',
            'attributes' => array(
                'type' => 'hidden'
            ),
        ));
        $this->add ( array (
            'name' => 'title',
            'type' => 'ZendFormElementSelect',        
            'attributes' => array(
                'id' => 'title',
                'options' => array(                    
                    'mr' => 'Mr',
                    'miss' => 'Miss',
                    'mrs' => 'Mrs',
                    'dr' => 'Dr'
                ),
                'value' => 'mr'
            ),
            'options' => array(
                'label' => 'Title'
            )
        ));

        $this->add(array(
            'name' => 'fname',            
            'attributes' => array(
                'id' => 'fname',
                'type' => 'text',
                'placeholder' => "First Name",
            ),
            'options' => array(
                'label' => 'First Name'
            )
        ));

        $this->add(array(
            'name' => 'surname',            
            'attributes' => array(
                'id' => 'surname',
                'type' => 'text',
                'placeholder' => "Surname Name",
            ),
            'options' => array(
                'label' => 'Surname Name'
            )
        ));  

        $this->add(array(
           'name' => 'companyName',
            'attributes' => array(
                'id' => 'companyName',
                'type' => 'text',
                'placeholder' => "Company Name",
            ),
            'options' => array(
                'label' => 'Company Name'
            )

        ));

        $this->add(array(
           'name' => 'address1',
            'attributes' => array(
                'id' => 'address1',
                'type' => 'text',
                'placeholder' => "Address Line 1",
            ),
            'options' => array(
                'label' => 'Address'
            )
        ));
        $this->add(array(
           'name' => 'address2',
            'attributes' => array(
                'id' => 'address2',
                'type' => 'text',
                'placeholder' => "Address Line 2",
            ),
            'options' => array(
                'label' => 'Address'
            )
        ));

        $this->add(array(
           'name' => 'address3',
            'attributes' => array(
                'id' => 'address3',
                'type' => 'text',
                'placeholder' => "Address Line 3",
            ),
            'options' => array(
                'label' => 'Address'
            )
        ));
$this->add(array(
            'name' => 'btnsubmit',
            'attributes' => array(
                'id' => 'btnsubmit',
                'type' => 'submit',
                'value' => 'Add',   
                'class' => 'btn btn-primary'
            ),
        ));
    }
}

这是我在实体公司中使用的输入过滤器

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;
}

每个表单元素都必须经过验证!即使验证器是空的,比如

$inputFilter->add($factory->createInput(array(
    'name' => 'title'
)));

只有经过验证的数据才会从表单中传递。

最新更新