php - 在表单字段旁边显示表单错误

标签 php html mysql model-view-controller phalcon

仍在通过教程学习 Phalcon,但我在表单字段旁边显示表单错误消息时遇到问题。

表单代码如下

<?php
use Phalcon\Forms\Form;
use Phalcon\Forms\Element\Text;
use Phalcon\Forms\Element\TextArea;
use Phalcon\Validation;
use Phalcon\Validation\Validator\PresenceOf;
use Phalcon\Validation\Validator\Email;

class ContactForm extends Form
{
    public function initialize()
    {
        // Name
        $fullname = new Text('fullname');
        $fullname->setFilters(array('striptags', 'string'));
        $fullname->addValidators(array(
            new PresenceOf(array(
                'message' => 'Full Name is required'
            ))
        ));
        $this->add($fullname);

    // Email and text area fields with validators


/**
 * Prints messages for a specific element
 */
public function messages($name)
{
    if ($this->hasMessagesFor($name)) {
        foreach ($this->getMessagesFor($name) as $message) {
            $this->flash->error($message);
        }
    }
}
}

使用伏特引擎的形式如下

{{ form('pages/contact') }}
                <div class="controls controls-row">
                    {{ form.render('fullname') }}
                    {{ form.render('email') }}
                    <span class="help-inline error">{{ form.messages('fullname') }}</span>
                    <span class="help-inline error">{{ form.messages('email') }}</span>
                </div>

                <div class="controls">
                    <span class="help-inline error">{{ form.messages('comments') }}</span>
                    {{ form.render('comments') }}
                </div>
                <div class="controls">

                    {{ submit_button('Send It', 'class': 'btn btn-primary pull-right') }}
                </div>
        </form><!--end form-->

contactAction() 中处理表单的代码如下

$form = new ContactForm();

        if ($this->request->isPost() == true)
        {
            if ($form->isValid($_POST)==false)
            {
                $form->messages("fullname");
                $form->messages("email");
                $form->messages("comments");

            }else
            {
                //send email
            }
        }
$this->view->form =$form

通过调用 $form->messages("fullname") 和其他字段,将使用 {{ flash.output() }} 打印验证消息,它通常位于我放置代码的页面顶部.如何让即显消息显示在表单域旁边?

请帮忙。谢谢

最佳答案

经过大量尝试,我只是将 ContactForm 类中的消息函数更改为返回消息而不是闪烁消息。

public function messages($name)
{
    if ($this->hasMessagesFor($name)) {
        foreach ($this->getMessagesFor($name) as $message) {
            return '<font color="FF0000">'.$message.'</font>';
        }
    }
}

代码的其他部分仍然保持不变。

关于php - 在表单字段旁边显示表单错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32214224/

相关文章:

javascript - 当链接到带有 id 的页面时,是否可以部分降落在页面下方而不是在顶部?

javascript - super 简单的测验程序。我的答案没有验证,分数也不是++

自执行 block 中的 Javascript 函数无法访问。为什么?

c# - 初学者请求 Mysql 和 C# 帮助

PHP 到 MySQL 和 Google 表格?

php - Laravel 即时更新电子邮件配置

php - $ROW = 未定义?

PHP 和 MySQL 编码和奇怪的标志

php - 在 Laravel 中将路由域更改为 IP 地址

php - 更新数据库动态php中的表