【问题标题】:display form error messages underneath the input fields Phalcon php在输入字段下显示表单错误消息 Phalcon php
【发布时间】:2014-06-12 21:29:57
【问题描述】:

如果字段未通过验证,是否有办法在表单的输入字段下方显示错误消息?我可以以某种方式以显示表单的相同操作(在我的情况下为用户/索引)处理表单,然后将这些错误消息发送到查看吗?我所拥有的是:index.volt:

<div class="loginForm">
<form action=<?= $form->getAction(); ?> method="POST">
<label for="username">Username: </label>
<?= $form->render('username'); ?>
    <br/>
<label for="password">Password: </label>
<?= $form->render('password'); ?>
<br>
<?= $form->render('login'); ?>
</form>
</div>

LoginForm.php:

<?php
use Phalcon\Forms\Form,
Phalcon\Forms\Element\Text,
Phalcon\Forms\Element\Password,
Phalcon\Forms\Element\Submit,
Phalcon\Validation\Validator\PresenceOf,
Phalcon\Validation\Validator\StringLength;

class LoginForm extends Form {

public function initialize()
{
$this->setAction('login');
$username = new Text('username');
$username->addValidator(new PresenceOf(array (
    'message' => 'Can\'t be empty'
)));

$password = new Password('password');
$password->addValidator(new PresenceOf(array (
    'message' => 'Can\'t be empty'
)));

$submit = new Submit('login', array('value' => 'Login'));

$this->add($username);       
$this->add($password);
$this->add($submit);
}
}

和 UserController.php:

<?php



class UserController extends \Phalcon\Mvc\Controller
{
/**
* login form
* @var LoginForm
*/
private $_loginForm;
public function initialize()
{
$this->_loginForm = new LoginForm();
}

public function indexAction()
{
$this->view->setVar('form', $this->_loginForm);
}
public function loginAction()
{   
if($this->request->isPost()) {

    if (!$this->_loginForm->isValid($this->request->getPost())) {
        foreach ($this->_loginForm->getMessages() as $message) {
            echo $message. '<br />';
            // redirect to User/index and pass error messages to view to display them to a user
        }
    }

}
}

}

编辑: 或者,在显示的同一操作上处理此表单会更好。我该怎么做?

【问题讨论】:

    标签: php css forms phalcon


    【解决方案1】:

    首先,您在index.volt 中提供的不是伏特内容。请参阅此处how to configure Volt 并在您的视图中使用 Volt 语言。

    您所要求的在 Phalcon 中称为 flashing messages

    不幸的是,在当前版本中,您只能根据类型(成功、错误、警告等)闪烁消息,但您可以创建自己的类型,所以我们假装类型表示输入名称。

    UserController.php

    ...
        public function loginAction()
        {   
          if($this->request->isPost()) {
    
            if (!$this->_loginForm->isValid($this->request->getPost())) {
                $messages = $this->_loginForm->getMessages();
    
                $userMessage = $messages->filter('username');
                if(count($userMessage)) 
                  $this->flash->message('username', $userMessage[0]);
    
                $passMessage = $messages->filter('password');
                if(count($passMessage)) 
                  $this->flash->message('username', $passMessage[0]);
    
                return $this->dispatcher->forward(["action" => "index"]);
            } else {
              //Login
            }
        }
    

    index.volt

    <div class="loginForm">
      <form action="{{form.getAction()}}" method="POST">
        <label for="username">Username: </label>
        {{form.render('username')}}<br/>
        {{ flash.has('username') ? flash.output('username') : '' }}
    
        <label for="password">Password: </label>
        {{form.render('password')}}<br>
        {{ flash.has('password') ? flash.output('password') : '' }}
    
        {{form.render('login')}}
      </form>
    </div>
    

    【讨论】:

    • 感谢您的响应,但我在第 5 行遇到了一个致命错误,其中 {{ flash.output('username') }} 即使验证未通过并且 flash 消息为用户名存在。有什么建议吗?
    • 我用 flash.output() 错误解决了我的问题。我必须在 di 中注册它。但另一个问题出现了。这些错误消息被附加到表单中,每当我转到另一个页面并返回时,它们都会作为 div 标签附加到表单中。有没有办法只显示一次这些错误消息?
    • @user2167174 在输出消息之前测试是否有消息要输出,请参阅我更新的答案。
    猜你喜欢
    • 1970-01-01
    • 2017-10-11
    • 1970-01-01
    • 1970-01-01
    • 2011-05-31
    • 1970-01-01
    • 2014-12-12
    • 2021-11-24
    • 1970-01-01
    相关资源
    最近更新 更多