2014-05-12 51 views
1

我有添加用户的表单。 问题是:我尝试从我的控制器获取表单元素时收到NULL。 下面的形式:

public function __cunstruct($name = null) 
{ 
    parent::__construct('user'); 
    $this->setAttribute('method', 'post'); 

    $this->add(array(
     'name' => 'Id', 
     'attributes' => array(
      'type' => 'text', 
     ), 
     'options' => array(
      'label' => 'id', 
     ), 
    )); 

    $this->add(array(
     'type' => 'Zend\Form\Element\Hidden', 
     'name' => 'IsCreate', 
     'attributes' => array(
      'value' => 'false' 
     ) 
    )); 

    $this->add(array(
     'name' => 'Username', 
     'attributes' => array(
      'type' => 'text', 
      'id' => 'username' 
     ), 
     'options' => array(
      'label' => 'Name' 
     ), 
    )); 

    $this->add(array(
     'name' => 'Password', 
     'attributes' => array(
      'type' => 'password' 
     ), 
     'options' => array(
      'label' => 'Password' 
     ) 
    )); 

    $this->add(array(
     'name' => 'ConfirmPassword', 
     'attributes' => array(
      'type' => 'password' 
     ), 
     'options' => array(
      'label' => 'Confirm password' 
     ) 
    )); 

    $this->add(array(
     'name' => 'TypeId', 
     'type' => 'Zend\Form\Element\Select', 
     'options' => array(
      'label' => 'Type' 
     ) 
    )); 

    $this->add(array(
     'name' => 'submit', 
     'attributes' => array(
      'type' => 'submit', 
      'valut' => 'Create', 
      'class' => 'btn btn-primary' 
     ) 
    )); 
} 

这里是控制器例如:

public function indexAction() 
{ 
    $form = new AddUserForm(); 
    var_dump($form); 
    echo "<BR>"; 
    echo "<BR>"; 
    var_dump($form->get('TypeId')); 
} 

var_dump($form)我收到阵列,但没有数据:

object(WebAdmin\Form\EditUserForm)#306 (27) { ["attributes":protected]=> array(1) { ["method"]=> string(4) "POST" } ["bindAs":protected]=> int(17) ["bindOnValidate":protected]=> int(0) ["baseFieldset":protected]=> NULL ["data":protected]=> NULL ["filter":protected]=> NULL ["useInputFilterDefaults":protected]=> bool(true) ["hasAddedInputFilterDefaults":protected]=> bool(false) ["hasValidated":protected]=> bool(false) ["isValid":protected]=> bool(false) ["isPrepared":protected]=> bool(false) ["preferFormInputFilter":protected]=> bool(false) ["wrapElements":protected]=> bool(false) ["validationGroup":protected]=> NULL ["factory":protected]=> NULL ["byName":protected]=> array(0) { } ["elements":protected]=> array(0) { } ["fieldsets":protected]=> array(0) { } ["messages":protected]=> array(0) { } ["iterator":protected]=> object(Zend\Stdlib\PriorityQueue)#307 (3) { ["queueClass":protected]=> string(28) "Zend\Stdlib\SplPriorityQueue" ["items":protected]=> array(0) { } ["queue":protected]=> NULL } ["hydrator":protected]=> NULL ["object":protected]=> NULL ["useAsBaseFieldset":protected]=> bool(false) ["label":protected]=> NULL ["labelAttributes":protected]=> NULL ["options":protected]=> array(0) { } ["value":protected]=> NULL } 

var_dump($form->get('TypeId'))我收到刚空值。

因此,当我试图添加的用户,我有一个错误:

Fatal error: Call to a member function setAttribute() on a non-object in /controller_path/ on line 72 

该生产线是:$form->get('TypeId')->setAttribute('options', $options);

回答

2

你必须在构造一个错字;这意味着元素永远不会添加到构造上,因为方法不会被调用。

public function __cunstruct($name = null) 

应该

public function __construct($name = null) 

随着中说@ MaiKay的回答暗示使用FormElementManager的真的是你应该做的事情。

您还届时可移动元素的init()方法,当你使用$formElementManager->get('Module\Form\Foo')

形式被称为的加入
1

我认为你必须使用FormElementManager才能收到您的形式。

例如

<?php 
$sl = $this->getServiceLocator(); 
$form = $sl->get('FormElementManager')->get('\Application\Form\MyForm'); 
return array('form' => $form);