문제

I have a form for adding user. The problem is: I recieve NULL when trying to get the form element from my Controller. Here is the form:

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

And here is Controller example:

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

In var_dump($form) I recieve array, but there is no data:

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 } 

In var_dump($form->get('TypeId')) I recieve just NULL.

Therefore when I am trying to add the user, I have an error:

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

The line is: $form->get('TypeId')->setAttribute('options', $options);

도움이 되었습니까?

해결책

You have a typo in the constructor; meaning the elements are never added on construct as the method would not be called.

 public function __cunstruct($name = null)

Should be

 public function __construct($name = null)

With that said @MaiKay's answer suggesting use of the FormElementManager is really what you should be doing.

You will also then be able to move the adding of elements to the init() method which is called when you get the form using $formElementManager->get('Module\Form\Foo')

다른 팁

i think you have to use the FormElementManager to recieve your form.

for example

<?php
$sl = $this->getServiceLocator();
$form = $sl->get('FormElementManager')->get('\Application\Form\MyForm');
return array('form' => $form);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top