如果字段未通过验证,是否可以在表单的输入字段下显示错误消息?我可以以某种方式处理与显示表单相同的操作(在本例中为用户/索引),然后发送这些错误消息进行查看吗?我所拥有的是: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
        }
    }

}
}

}


编辑:
或者,最好以与显示该表单相同的操作来处理此表单。我怎样才能做到这一点?

最佳答案

首先,您在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>

08-18 13:59