gpt4 book ai didi

php - Zend Framework2 表单错误消息

转载 作者:行者123 更新时间:2023-12-04 04:47:28 26 4
gpt4 key购买 nike

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

    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 的值如果我看到viewsource,它会显示value=''
  • 我想在我希望如何实现的地方显示错误消息?
    我试过这个echo $this->formElementerrors($form->getMessages('uname'));但没有任何建议或想法来解决这个问题?

  • 下面是我的 Controller 代码...
    $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);

    最佳答案

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

    改变:

    $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,
    ),
    ),
    ),
    )));

    关于php - Zend Framework2 表单错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17963951/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com