我有添加用戶的表單。 問題是:我嘗試從我的控制器獲取表單元素時收到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);