【发布时间】:2015-11-20 17:02:34
【问题描述】:
我正在尝试创建一个带有电子邮件字段和密码字段的简单登录表单。尝试在视图中显示单个字段时遇到问题。 Zends 网站上的专辑教程没有使用FIELDSET,博客教程只使用了echo $this->formCollection($form);。所以我认为不会有太大区别,而且我在网上找到的所有内容都表明语法没有区别。据我所知,我所拥有的一切似乎都分别与 Zends 网站上的博客和专辑教程相匹配。
如果我将字段定义移动到我的 FORM 类(绕过 FIELDSET)或者如果我使用以下方式转储所有字段,则不会发生错误:
echo $this->formCollection($form);
这是我得到的错误:
No element by the name of [USER_LOGIN] found in form
我正在尝试使用以下方法显示单个字段:
echo $this->formRow($form->get('USER_LOGIN'));
这是formCollection 调用的结果:
(*注意:我尝试在$form->get() 调用中使用“login-fieldset[USER_LOGIN]”并得到相同的行为)
<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>
以下是相关代码:
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'
)
));
}
}
【问题讨论】:
标签: php forms zend-framework form-helpers