简体   繁体   English

Phalcon PHP - 表单和模型验证

[英]Phalcon PHP - Form and model validation

Phalcon support 2 validation components: Phalcon支持2个验证组件:

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

I dont know how to use them in my situation. 我不知道如何在我的情况下使用它们。 I have a registration form with: 我有一个注册表格:

  • csrf CSRF
  • username 用户名
  • password 密码
  • repeat password 重复输入密码
  • email 电子邮件
  • repeat email 重复的电子邮件
  • submit button 提交按钮

I created a registration form as following: 我创建了一个注册表格如下:

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. When we use model validator and vice versa? 当我们使用模型验证器时,反之亦然?
  2. Can we combine them and how? 我们可以将它们结合起来吗?
  3. I want to know the right way to implement this register action using model and non-model validator to check csrf, identical password, username and email uniqueness. 我想知道使用模型和非模型验证器来实现此注册操作的正确方法,以检查csrf,相同的密码,用户名和电子邮件唯一性。

Thank for your help! 谢谢您帮忙!

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. 通常,POST和模型中的数据(类型/结构/层次结构)会有所不同,例如,单个表单接收与两个模型相关的输入,这两个模型都将更新。 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. 因此,在收到POST数据后,您要检查它是否有效,并且在保存两个独立模型后,您还需要检查它们是否有效。

Phalcon has a validation component , which is a base class for all validation. Phalcon有一个验证组件 ,它是所有验证的基类。 It works in exactly the same way as the form validation in your code above. 它的工作方式与上面代码中的表单验证完全相同。

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. 我不是Phalcon中整个验证业务如何实现的忠实粉丝 - 它没有给你一个粒度级别的控制,并且验证和验证器之间存在很强的依赖关系。 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 :) 重复使用相同的验证器没有整洁或整洁的方法,但有一些知道的尝试 ,你可以用你的想象力:)

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. 我可能错了,但Phalcon模型会根据字段上的元数据自动验证数据,因此您需要担心的是POST输入。 Big working with models documentation covers that subject in detail, I'm sure you've already been there. 大量使用模型文档详细介绍了这个主题,我相信你已经在那里了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM