Fork me on GitHub
Current releases are 2.1 (gold) and 2.0.18.11 (security patch for 2.0)
Ready for 2.1? Find out if your favorite plugins are compatible.
The 2.0 branch will no longer receive updates after Dec 2014.

Add required fields to signup

edited January 2012 in Vanilla 2.0 Help

Hi! I'm putting together a forum for a client, and they need to add 5 or 6 fields that will be required (zipcode, city, state, etc). I added them to the user database, and found all the files I need to edit in /entry. However, I would like to make this into a plugin so I don't have to worry about all my changes being overwritten when upgrading.

I am new to Vanilla and am having trouble wrapping my head around it. How do I override the UserModel to add new fields when inserting and validating? Should I just copy the usermodel code into a new model in my plugin somehow? I thought maybe just copying the entire entry folder into a plugin would be the way to go, but then I'm not sure how to create a different URL (something like mysite.com/signup). Is this the write way to go? I have no idea! Struggling for a week or two with this. Any help is appreciated!

PS: this is for Vanilla 2.

Tagged:

Best Answers

  • hbfhbf wiki guy? MVP
    edited January 2012 Answer ✓

    i would think about it in one of two ways:

    1. you can force the validation and user entry of this data into the native signup form or
    2. upon completion of the native signup form you can trigger a second form with it's own validation rules.

    option 1 is going to probably rely on adding hooks to the signup process that might not exist right now... so you run the risk of having upgrade path problems.

    option 2 is going to be less "integrated" as the user will have to go through two screens before completing the signup process. But this is likely to be based on out of the box hooks, and will make upgrades much easier.

    There are probably other ways to skin the cat, but this is how i see it from where i'm sitting.

  • ToddTodd Chief Product Officer Vanilla Staff
    Answer ✓

    One thing to note. As long as the fields are submitted on the form and they are also on your database table then they'll save to the database. You don't have to do anything else. If the fields in the database are required then they'll also give you an error message if you leave them blank in the application. It's all automagic.

    Usually, we say just modify the appropriate registration view and put the new fields in after adding them to the user table. This has become such a common request that we added some events in the registration views so that you can just echo out some html form controls in a plugin. Unfortunately, this didn't make it into 2.0.18, but I may just back-port those files for the next 2.0.18.* service release.

    Right now you can maybe just grab the latest views or modify them. I'd be happy to help.

Answers

  • hbfhbf wiki guy? MVP

    @Kristin

    in my experience with this platform,

    most plugins extend existing tables or create their own tables, then use their own plugin controller to handle the processing, rather than overriding core controllers or models.

    Themes typically do the job of overriding of controllers and views.

    That said I would suggest using a plugin and attaching to a hook in the signup controller. Sorry that's a bit vague, but i haven't really dug in deep enough to give you specifics. You'll also need to hook into the user panel to display your info i suppose.

    There is a plugin out there that extends the user metamodel already... i would look at that and modify from there.

    http://vanillaforums.org/addon/aboutme-plugin

  • Thanks, that does help. I'll take a look at that plugin (I think I already downloaded it).

    one thing I'm confused about is how to save additional information when the userModel is inserting the fields... I'm not on my work computer, but I don't think there's a hook into the _insert method, but I could be wrong. I guess I would do the verifications on my own fields and then somehow send out any errors with the validation check that the existing form already does? I will look more at this tomorrow and try to post more specific questions. Thanks!

  • AoleeAolee Hobbyist & Coder ✭✭

    i like option 2 :D thats a good idea.

  • hbfhbf wiki guy? MVP

    another benefit of option 2 is that you can reuse the form code to allow users to update their contact information when required.

    Also, You can use other hooks to check for the presence of required contact info and raise a notification if something doesn't pass validation. That will catch any conditions where a users creates an account then stops short of finishing the contact data.

  • AoleeAolee Hobbyist & Coder ✭✭

    im planning to make the 2nd page optional so user can skip and update later. in my case really hate filling out alot of fields during sign up. :)

  • Todd--oh really.... :) How do I make the zipcode field verify as a zip? These are my extra fields right now, and I use a plugin to input the selection data using EntryController_Register_Handler()

    <?php echo $this->Form->Label('City', 'City'); echo $this->Form->TextBox('City'); echo ''; ?> <?php echo $this->Form->Label('State', 'State'); echo $this->Form->DropDown('State', $this->StateOptions); ?> <?php echo $this->Form->Label('Zipcode', 'Zip'); echo $this->Form->TextBox('Zip'); echo ''; ?> <?php echo $this->Form->Label('Relationship to Parkinsons', 'Relationship'); echo $this->Form->DropDown('Relationship', $this->RelationshipOptions); ?> <?php echo $this->Form->Label('Age', 'Age'); echo $this->Form->DropDown('Age', $this->AgeOptions); ?>

  • For some reason that didn't show the spans that are included as incorrect statements... under "city" and "zip" I have something like <span id="CityWrong" class="Incorrect" style="display: none;">'.T('City is a required field.').'</span>

    Is that all it takes to make it required?

  • x00x00 MVP
    edited January 2012

    A trick I use in plugins

    public function EntryController_Render_Before($Sender,$Args) {
    //~     if(!$this->IsEnabled())
    //~         return;
        if(strcasecmp($Sender->RequestMethod,'register')==0){
            $RegistrationMethod = Gdn::Config('Garden.Registration.Method');
            if (in_array($RegistrationMethod, array( 'Basic','Captcha'))){
                $Sender->View =$this->GetView( 'register'.strtolower($RegistrationMethod).'.php');
            }
        }
    
    }
    

    now copy the register views into a views folder in your plugin folder. Works a charm

    grep is your friend.

    Aolee
  • I can't believe that's all I had to do (turns out I also had an issue with the fields I added to the Database...ugh!)

    One other question... how do I add the captcha field to the registerapproval view? I would like the admins to approve members, but I don't want them deluged with spam either. I added this bit to the top:

    $CaptchaPublicKey = Gdn::Config('Garden.Registration.CaptchaPublicKey'); $Request = Gdn::Request(); $CaptchaSSL = (StringBeginsWith(Url('/', TRUE), 'https') || Gdn::Request()->GetValueFrom(Gdn_Request::INPUT_SERVER, 'SERVER_PORT') == 443) ? TRUE : FALSE;

    and this bit to the form:

    <?php echo $this->Form->Label("Security Check", ''); echo recaptcha_get_html($CaptchaPublicKey, NULL, $CaptchaSSL); ?>

    But I get this error:

    Fatal error: Call to undefined function recaptcha_get_html() in /Users/kristin/Sites/vanilla/themes/powering_forward/views/entry/registerapproval.php on line 88

    I would assume this is because the RegisterApproval method doesn't include this function, but I'm not entirely sure how to add it. Thanks for your help!

  • AoleeAolee Hobbyist & Coder ✭✭

    x00 said: A trick I use in plugins

    > public function EntryController_Render_Before($Sender,$Args) {
    > //~       if(!$this->IsEnabled())
    > //~       return;
    >   if(strcasecmp($Sender->RequestMethod,'register')==0){
    >       $RegistrationMethod = Gdn::Config('Garden.Registration.Method');
    >       if (in_array($RegistrationMethod, array( 'Basic','Captcha'))){
    >           $Sender->View =$this->GetView( 'register'.strtolower($RegistrationMethod).'.php');
    >       }
    >   }
    > 
    > }
    > 

    now copy the register views into a views folder in your plugin folder. Works a charm

    im also using the same trick by tapping on the EntryController_Render_hander :)

  • AoleeAolee Hobbyist & Coder ✭✭

    kristin said: I can't believe that's all I had to do (turns out I also had an issue with the fields I added to the Database...ugh!)

    One other question... how do I add the captcha field to the registerapproval view? I would like the admins to approve members, but I don't want them deluged with spam either. I added this bit to the top:

    $CaptchaPublicKey = Gdn::Config('Garden.Registration.CaptchaPublicKey'); $Request = Gdn::Request(); $CaptchaSSL = (StringBeginsWith(Url('/', TRUE), 'https') || Gdn::Request()->GetValueFrom(Gdn_Request::INPUT_SERVER, 'SERVER_PORT') == 443) ? TRUE : FALSE;

    and this bit to the form:

    <?php > echo $this->Form->Label("Security Check", ''); > echo recaptcha_get_html($CaptchaPublicKey, NULL, $CaptchaSSL); > ?>

    But I get this error:

    Fatal error: Call to undefined function recaptcha_get_html() in /Users/kristin/Sites/vanilla/themes/powering_forward/views/entry/registerapproval.php on line 88

    I would assume this is because the RegisterApproval method doesn't include this function, but I'm not entirely sure how to add it. Thanks for your help!

    go the same problem before, u will need to study the

    applications\dashboard\controllers\class.entrycontroller.php

  • Don't Forget you can use custom views in your theme.

    Just sayin...

    I've done this successfully before, just add the fields in the theme's hooks, and the custom views to the theme.

Sign In or Register to comment.