duanjiebian6712 2015-11-20 17:02
浏览 36
已采纳

ZF2和字段集/表单用法 - 意外行为

I am trying to create a simple login form with an email field and password field. I am running into issues when trying to display individual fields in my view. The Album tutorial on Zends' website didn't use a FIELDSET and the Blog tutorial only used echo $this->formCollection($form);. So I assumed there wouldn't be much of a difference, and everything I find online suggests there isn't a difference in syntax. Everything I have seems to match up to the Blog and Album tutorials on Zends' website respectively as far as I can tell.

The error does not occur if I move my field definitions to my FORM class (bypassing the FIELDSET) or if I dump all of the fields using:

echo $this->formCollection($form);

This is the error I am getting:

No element by the name of [USER_LOGIN] found in form

I am trying to display a single field using:

echo $this->formRow($form->get('USER_LOGIN'));

Here is the result of the formCollection call:

(*NOTE: I tried using "login-fieldset[USER_LOGIN]" in the $form->get() call and got the same behavior)

<fieldset>
    <fieldset>
        <label>
            <span>Username</span>
            <input type="text" name="login-fieldset[USER_LOGIN]" value="">
        </label>
        <label>
            <span>Password</span>
            <input type="password" name="login-fieldset[USER_PWD]" value="">     
        </label>
    </fieldset>
    <input type="submit" name="submit" value="Login">
</fieldset>

Here is the relevant code:

CSAdmin\Controller\LoginController:

namespace CSAdmin\Controller;

use Zend\View\Model\ViewModel;
use Zend\Form\FormInterface;

class LoginController extends AdminController
{
    protected $loginService;

    protected $loginForm;

    public function __construct(
        \CSAdmin\Service\LoginServiceInterface $loginService,
        FormInterface $loginForm) {

        parent::__construct();
        $this->loginService = $loginService;
        $this->loginForm    = $loginForm;
    }

    public function indexAction()
    { 
        array_push($this->layoutVars['customStyles'], 'css/admin/form.css');
        array_push($this->layoutVars['customStyles'], 'css/admin/styles.css');

        $request = $this->getRequest();
        $login    = $this->loginService->findUser($this->params('USER_LOGIN'));
        $this->loginForm->bind($login);

        if ($request->isPost()) {
            //Nothing here yet
        }

        //Override view to use predefined Admin Views
        $view = new ViewModel(array('data'=>$this->data,
                                    'form'=>$this->loginForm
        ));
        $view->setTemplate('CSAdmin/login/login.phtml'); // path to phtml file under view folder

        //Set the Admin Layout
        $layout = $this->layout();
        $layout->setVariable('layout', $this->layoutVars);
        $layout->setTemplate('layout/CSAdmin/login.phtml');

        //Render Page
        return $view;
    }
}

CSAdmin\Form\LoginForm:

namespace CSAdmin\Form;

 use Zend\Form\Form;
 use Zend\Stdlib\Hydrator\ClassMethods;
 use \CSAdmin\Model\User;

 class LoginForm extends Form
 {
     public function __construct($name = null, $options = array())
     {
         parent::__construct($name, $options);
         $this->setHydrator(new ClassMethods(false));
         $this->setObject(new User());

         $this->add(array(
             'name' => 'login-fieldset',
             'type' => 'CSAdmin\Form\LoginFieldset',
             'options' => array(
                 'use_as_base_fieldset' => true
             )
         ));
         $this->add(array(
             'type' => 'submit',
             'name' => 'submit',
             'attributes' => array(
                 'value' => 'Login'
             )
         ));
     }

 }

CSAdmin\Form\LoginFieldset:

namespace CSAdmin\Form;

use Zend\Form\Fieldset;
use Zend\Stdlib\Hydrator\ClassMethods;
use \CSAdmin\Model\User;

class LoginFieldset extends Fieldset
{
    public function __construct($name = null, $options = array())
    {
        parent::__construct($name, $options);

        $this->setHydrator(new ClassMethods(false));
        $this->setObject(new User());

        $this->add(array(
            'type' => 'text',
            'name' => 'USER_LOGIN',
            'options' => array(
                'label' => 'Username'
            )
        ));

        $this->add(array(
            'type' => 'password',
            'name' => 'USER_PWD',
            'options' => array(
                'label' => 'Password'
            )
        ));
    }
}
  • 写回答

1条回答 默认 最新

  • duanbaque4230 2015-11-23 21:19
    关注

    You need to get the field set then the element so try:

    echo $this->formRow($form->get('login-fieldset')->get('USER_LOGIN');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图