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

时间:2015-08-25 21:15:18

标签: 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-&gt;消息(“fullname”)和其他字段,将使用{{flash.output()}}打印验证消息,{{flash.output()}}通常位于我放置的页面的顶部码。如何在表单字段旁边显示Flash消息?

请帮忙。感谢

0 个答案:

没有答案
相关问题