Phalcon PHP-表单和模型验证 [英] Phalcon PHP - Form and model validation

查看:77
本文介绍了Phalcon PHP-表单和模型验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Phalcon支持2个验证组件:

Phalcon support 2 validation components:

Phalcon\Validation\Validator
Phalcon\Mvc\Model\Validator

我不知道如何在自己的情况下使用它们.我有以下注册表:

I dont know how to use them in my situation. I have a registration form with:

  • csrf
  • 用户名
  • 密码
  • 重复密码
  • 电子邮件
  • 重复电子邮件
  • 提交按钮

我创建了以下注册表:

class RegistrationForm extends \Phalcon\Forms\Form
{
    public function initialize()
    {
        $csrf = new \Phalcon\Forms\Element\Hidden('csrf');
        $csrf->addValidator(new \Phalcon\Validation\Validator\Identical(array(
            'value' => $this->security->getSessionToken(),
            'message' => 'CSRF validation failed'
        )));

        $username = new \Phalcon\Forms\Element\Text('username');
        $username->addFilter('trim');
        $username->addValidator(new PresenceOf(array(
            'message' => 'Username is required.',
        )));
        $username->addValidator(new StringLength(array(
            'min' => 6,
            'messageMinimum' => 'Username must be at least 6 characters.'
        )));

        // ...
    }
}

这是我的控制器/操作:

And this is my controller/action:

class UserController extends \Phalcon\Mvc\Controller
{
    public function registerAction()
    {
        $form = new RegistrationForm();
        if ($this->request->isPost()) {
            if ($form->isValid($this->request->getPost())) {
                // Test only
                var_dump($this->request->getPost());
                exit;
            } else {
                // Test only
                foreach ($form->getMessages() as $message) {
                    echo $message, '<br/>';
                }
                exit;
            }
        }

        $this->view->form = $form;
    }
}

  1. 当我们使用模型验证器时,反之亦然吗?
  2. 我们可以将它们结合在一起吗?
  3. 我想知道使用模型和非模型验证器来检查csrf,相同的密码,用户名和电子邮件唯一性的正确方法,以执行此注册操作.

感谢您的帮助!

推荐答案

在将数据发送到数据库之前,请使用模型验证器.通常,您的POST和模型中的数据(类型/结构/层次结构)会有所不同,例如,一个表单接收与两个模型相关的输入,这两个模型都会被更新.因此,接收到POST数据后,您要检查它是否有效,保存两个独立的模型后,还要检查它们是否有效.

You use model validator before sending your data to the database. Often, the data (type / structure / hierarchy) in your POST and in your model would differ, for example, a single form receives input related to two models, both of which will be updated. So, upon receiving POST data you want to check that it's valid, and upon saving two independent models, you want also to check that they are valid.

Phalcon具有验证组件,它是基类.用于所有验证.它的工作方式与上述代码中的表单验证完全相同.

Phalcon has a validation component, which is a base class for all validation. It works in exactly the same way as the form validation in your code above.

我不太喜欢如何在Phalcon中实现整个验证业务-它不能为您提供严格的控制级别,并且验证和验证程序之间存在强烈的依存关系.但是,它在形式和模型验证的范围内做得很好.没有重复使用相同验证器的整洁方法,但是有一些已知的尝试,您可以发挥想象力:)

I'm not a big fan of how the whole validation business is implemented in Phalcon – it doesn't give you a grain-level of control and there's a strong dependency between validation and validators. Yet, it does good work in the scope of form and model validation. There's no neat or tidy way of reusing the same validators, but there are some know attempts, you can use your imagination :)

要执行注册操作,您仅应使用表单验证器过滤掉用户输入.我可能是错的,但是Phalcon模型会根据字段上的元数据自动验证数据,因此您只需担心POST输入即可. 使用模型文档详细介绍了该主题,确保您已经去过那里.

To implement your register action you only should use your form validator to filter out the user input. I might be wrong, but Phalcon models automatically validate data based on the metadata on the fields, so all you need to worry is your POST input. Big working with models documentation covers that subject in detail, I'm sure you've already been there.

这篇关于Phalcon PHP-表单和模型验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆