Auto-generate field value on sfGuardUser when registering

大憨熊 提交于 2019-12-14 02:26:39

问题


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.

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?

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.


回答1:


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());
        ...
    }
}

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



来源:https://stackoverflow.com/questions/4771313/auto-generate-field-value-on-sfguarduser-when-registering

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!