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');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 需要跳转番茄畅听app的adb命令
  • ¥50 寻找一位有逆向游戏盾sdk 应用程序经验的技术
  • ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证