注册时自动在sfGuardUser上生成字段值 [英] Auto-generate field value on sfGuardUser when registering

查看:119
本文介绍了注册时自动在sfGuardUser上生成字段值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个Symfony 1.4项目,该项目使用 sfDoctrineGuardPlugin 用于用户/身份验证.我的用户表上有一个名为"access_token"的字段,我想在用户注册时使用自动生成的值填充该字段.我看到用于注册的sfGuardRegisterForm用于验证.不幸的是,当我从命令行运行./symfony doctrine:build-forms时,这种形式似乎没有扩展,这意味着如果不编辑插件就无法在其中生成访问令牌.

I'm building a Symfony 1.4 project that is using sfDoctrineGuardPlugin for users/authentication. I have a field on my user table named "access_token" that I would like to populate with an auto-generated value upon user registration. I see that for registration, the sfGuardRegisterForm is used for validation. Unfortunately, when I run ./symfony doctrine:build-forms from the command line, this form doesn't appear to be extended, meaning that I can't generate an access token there without editing the plugin.

或者,我在考虑修改sfRequest参数,以便在尝试通过此表单进行验证之前手动设置"access_token"值.但是,由于我允许用户同时通过前端应用程序和API应用程序进行注册,因此这意味着我必须确保在两个地方都手动填充此参数值,这似乎效率不高.但是,也许这实际上是正确的方法,并且尝试修改表单并不理想.

Alternatively, I was thinking of modifying the sfRequest parameters such that I manually set the "access_token" value before attempting to validate via this form. However, since I'm allowing users to register both by a frontend application as well as an API application, this means that I would have to make sure I manually stuff this parameter value in both places, which seems inefficient. However, perhaps that is actually the right way to go, and trying to modify the form is not ideal.

有什么意见/建议吗?除了我在这里列出的想法之外,还有谁会做其他事情?

Any input/suggestions? Would anyone do anything different than the ideas I have listed here?

编辑:我已经在名为generateAccessTokensfGuardUser模型上创建了一个公共静态方法,该方法返回该值.我只是想找出在项目代码中调用此代码的最佳位置.

Edit: I've already create a public static method on my sfGuardUser model named generateAccessToken, which returns the value. I'm just trying to figure out the best place to call this in my project's code.

推荐答案

sfDoctrineGuardPlugin不提供用户注册功能.这就是为什么您必须自己实现它的原因.因此,在您的控制器中,您可以按照使用symfony进行操作的方式来填充字段:

sfDoctrineGuardPlugin does not provide user registration functionality. This why you have to implement it on your own. So, in your controller you can populate a field just the way it's being done with symfony:

class myActions extends sfActions {
    public function executeRegistration(sfWebRequest $request) {
        ...
        $form = new myRegisterForm();
        $form->setDefault('access_token', sfGuardUser::generateAccessToken());
        ...
    }
}

access_token小部件应该是sfWidgetFormInputHidden的实例. 希望这会有所帮助.

And the access_token widget should be an instance of sfWidgetFormInputHidden. Hope this helps.

这篇关于注册时自动在sfGuardUser上生成字段值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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