Phalcon - 形式验证

时间:2015-07-10 19:00:10

标签: php forms validation phalcon

我创建了一个包含验证规则的表单。一切都很好,形式是可见的和有效的。 问题在于验证者。只有第一个验证器适用于addValidators([ ....])

我的表单类源代码:

public function initialize()
{

    $title = new Text('title');
    $title->setLabel('Title of article');
    $title->setFilters([
        'striptags', 'trim'
    ]);

    $title->addValidators([
        new PresenceOf([
            'message' => 'Title can not be empty'
        ]),
        new StringLength([
            'min' => 5,
            'messageMinimum' => 'Title is too short. Should has more than 5 letters'
        ]),
        new MYArticleAddCheckTitleValidator([
            'message' => 'aaaaaaaaaaaaaaa'
        ])
    ]);

    $this->add($title);

    ..........
  • 验证器PresenceOf工作正常。验证Flash消息可见。
  • Validator StringLength不起作用。看起来形式并不知道它
  • Validator MYArticleAddCheckTitleValidator(我自己的验证器类) - 与StringLength相同。

Windows上的Phalcon 2.0.4版。

任何提议或建议?

非常感谢。

1 个答案:

答案 0 :(得分:0)

使用Phalcon \ Flash \ Direct

解决此问题的最佳方法是使用类Phalcon\Flash\Direct的Flash消息。您可以找到文档here

这个策略由phalcon vokuro project使用,我建议你看一下。

此解决方案的关键是表单类中的message()函数。

表单类

<?php
namespace Your\App\Forms;

use Phalcon\Forms\Form;
use Phalcon\Forms\Element\Text;
use Phalcon\Validation\Message;
use Phalcon\Validation\Validator\PresenceOf;
use Phalcon\Validation\Validator\StringLength;
use Your\App\Validation\MYArticleAddCheckTitleValidator;

class YourForm extends Form {

    public function initialize()
    {

        $title = new Text('title');
        $title->setLabel('Title of article');
        $title->setFilters([
            'striptags', 'trim'
        ]);

        $title->addValidators([
            new PresenceOf([
                'message' => 'Title can not be empty'
            ]),
            new StringLength([
                'min' => 5,
                'messageMinimum' => 'Title is too short. Should has more than 5 letters'
            ]),
            new MYArticleAddCheckTitleValidator([
                'message' => 'aaaaaaaaaaaaaaa'
            ])
        ]);

        $this->add($title);
    }

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

<强>控制器

<?php
namespace Your\App;

use Your\App\YourForm;

class YourController extends ControllerBase
{

    public function indexAction()
    {
       $form = new YourForm();
       $this->view->form = $form;

        if($this->request->hasQuery('title')){
            if ($form->isValid($this->request->getQuery()) != false) {
                // Code when form is valid
            }
        }
    }
}

查看 如果您遵循建议的架构,则应位于/app/views/your/index.html

<form method="GET" action="">
    <?= $form->label('title') ?>
    <?= $form->render('title')?>
    <?= $form->messages('title') //show messages here ?>
</form>

如果您有多个表单,则使用DI注册Flash服务非常有用。 定义服务时(可以位于根文件夹中的index.phpservices.php文件夹中的/app/config/),您可以定义Flash服务:

<?php 
use Phalcon\DI\FactoryDefault;

$di = new FactoryDefault();

// Register the flash service with custom CSS classes
$di->set('flash', function () {
    $flash = new Phalcon\Flash\Direct(
        array(
            'error'   => 'your-error-class',
            'success' => 'your-success-class',
            'notice'  => 'your-notice-class',
            'warning' => 'your-warning-class'
        )
    );

    return $flash;
});