下面是我的代码,用于创建登录表单
class LoginForm extends Form
{
public function __construct($name = null)
{
parent::__construct('login');
$this->setAttribute('method', 'post');
$this->add(array('name'=>'uname','attributes'=>array('type'=>'text',),
'options'=>array('label'=>'UserName'),
));
$this->add(array('name'=>'pword','attributes'=>array('type'=>'password',),
'options'=>array('label'=>'Password'),
));
$this->add(array('name'=>'submit','attribute'=>array('type'=>'submit',
'value' => 'Go',
'class'=>'submit','id'=>'submitbutton',)
));
}
}
以下是我的登录页面的代码
$form = $this->form;
$form->setAttribute('action', $this->url('users', array('action' => 'login')));
$form->prepare();
echo $this->form()->openTag($form);
echo $this->formRow($form->get('uname'));
echo $this->formRow($form->get('pword'));
echo $this->formElementerrors($form->get('pword'));
echo $this->formSubmit($form->get('submit'));
echo $this->form()->closeTag();
表单可以正常工作,没有问题,但是:
submit
如果我看到它显示的视图源,它就没有价值value=''
- 我想在我希望如何实现的地方显示错误消息?我试过了,
echo $this->formElementerrors($form->getMessages('uname'));
但没有提出任何建议或想法来解决这个问题?
下面是我的控制器代码...
$form = new LoginForm();
$request = $this->getRequest();
if ($request->isPost()) {
$post = $request->getPost();
if($post->get('uname')=='')
{
$umessage='please enter username';
return $this->redirect()->toRoute('users',array('action'=>'login'));
}
$this->db =$this->getServiceLocator()->get('db');
$authAdapter = new AuthAdapter($this->db);
$authAdapter->setTableName('users')
->setIdentityColumn('uname')
->setCredentialColumn('pword');
$authAdapter->setIdentity($post->get('uname'))
->setCredential(md5($post->get('pword')));
$authService = new AuthenticationService();
$authService->setAdapter($authAdapter);
$result = $authService->authenticate();
if ($result->isValid()) {
return $this->redirect()->toRoute('users');
} else {
switch ($result->getCode()) {
case Result::FAILURE_IDENTITY_NOT_FOUND:
echo 'user name not valid dude';
break;
case Result::FAILURE_CREDENTIAL_INVALID:
echo 'password incorrect';
break;
case Result::SUCCESS:
echo 'login successfull';
break;
}
}
}
$this->layout('layout/index');
return array('form' => $form);