2

下面是我的代码,用于创建登录表单

    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();

表单可以正常工作,没有问题,但是:

  1. submit如果我看到它显示的视图源,它就没有价值value=''
  2. 我想在我希望如何实现的地方显示错误消息?我试过了, 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);
4

1 回答 1

3

您的语法对于显示单个错误消息不正确,请尝试以下操作:

改变:

$this->formElementerrors($form->getMessages('uname')); // incorrect
$this->formElementErrors($form->get('uname')); // correct

例子:

<?php foreach($form->getElements() as $element): ?>
    <?php if($element->getLabel()): ?>
        <?php echo $this->formLabel($element) ?>
    <?php endif ?>
    <?php echo $this->formElementErrors($element, array('class' => 'inline')); ?>
   <?php echo $this->formElement($element) ?>
<?php endforeach ?>

请注意,我传递的是实际元素,而不是通过 getMessages()。

您的按钮定义上还有一个类型(s最后缺少):

attribute => attributes

见下文:

$this->add(array(
     'name'=>'submit',
     'attributes' => array(
         'type'=>'submit',
         'value' => 'Go',
         'class'=>'submit','id'=>'submitbutton',
      )  
));

我假设您也设置了输入过滤器?

$inputFilter->add($factory->createInput(array(
    'name'     => 'username',
    'required' => true,
    'filters'  => array(
        array('name' => 'StripTags'),
        array('name' => 'StringTrim'),
    ),
    'validators' => array(
        array(
            'name'    => 'StringLength',
            'options' => array(
                'encoding' => 'UTF-8',
                'min'      => 1,
                'max'      => 100,
            ),
        ),
    ),
)));
于 2013-07-31T07:54:40.203 回答