Zend Framework2形成错误消息

时间:2013-07-31 07:16:26

标签: php zend-framework zend-framework2 zend-form

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

    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
  2. ,则value=''没有取值echo $this->formElementerrors($form->getMessages('uname'));
  3. 我想在哪里显示错误信息,我希望如何实现? 我试过这个 $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 个答案:

答案 0 :(得分: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,
            ),
        ),
    ),
)));