我正在尝试使用电子邮件字段和密码字段创建一个简单的登录表单。尝试在我的视图中显示单个字段时遇到问题。关于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 \控制器\的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 \表格\ 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 \表格\ 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'
)
));
}
}