Please upgrade here. These earlier versions are no longer being updated and have security issues.
HackerOne users: Testing against this community violates our program's Terms of Service and will result in your bounty being denied.
Options

How can I add a custom field in Vanilla Registration form?

I want to add a custom field eg. STUDENT ID CARD NO. in registration form, after submitting the form it should check the forum database if already the same STUDENT ID CARD NO. exists then returns to the registration form with error text...
Can anyone please help me out?

Answers

  • Options
    AdrianAdrian Wandering Spirit Montreal MVP

    If you upgrade to 2.1.1, you can use profile extender: https://github.com/vanilla/vanilla/tree/2.1/plugins/ProfileExtender

  • Options

    @Adrian said:
    If you upgrade to 2.1.1, you can use profile extender: https://github.com/vanilla/vanilla/tree/2.1/plugins/ProfileExtender

    Can I check the forum database using this plugin, if already the same STUDENT ID CARD NO. exists then returns to the registration form with error text.?

  • Options
    AdrianAdrian Wandering Spirit Montreal MVP

    The information is loaded on the database, so it should be doable. Maybe someone with a bit more finesse can help you on that.

  • Options
    R_JR_J Ex-Fanboy Munich Admin

    You would need a custom plugin, because ProfileExtender processes additional registration fields after user has been created.

  • Options
    AdrianAdrian Wandering Spirit Montreal MVP

    Good point @R_J‌ :)

  • Options
    peregrineperegrine MVP
    edited August 2014

    @Trideep‌

    I could write you a custom plugin for a donation, if no one else details all the necessary code for you you can pm me if you like.

    I may not provide the completed solution you might desire, but I do try to provide honest suggestions to help you solve your issue.

  • Options
    hgtonighthgtonight ∞ · New Moderator
    edited August 2014

    Using Profile extender doesn't allow for Validation rules to be automatically applied.

    You would need to hook into the user model and do your unique validation code.

    public function UserModel_BeforeRegister_Handler($Sender) {
      $FormPostValues = $Sender->EventArguments['User'];
      $StudentID = $FormPostValues['FieldName'];
    
      // Add your custom code here
      $Valid = FALSE;
    
      if(!$Valid) {
        $Sender->Form->AddError("The Student ID you entered has already been registered");
        $Sender->EventArguments['Valid'] = FALSE;
      }
    }
    

    I think you would find that using profile extender will make life difficult in terms of trying to find if a student id has already been used. Adding your own column would greatly simplify the process of determining if the student ID has already been used.

    Search first

    Check out the Documentation! We are always looking for new content and pull requests.

    Click on insightful, awesome, and funny reactions to thank community volunteers for their valuable posts.

  • Options

    But it does enforce schema. If student ids are unique, then they need a unique index in the database.

    grep is your friend.

  • Options
    hgtonighthgtonight ∞ · New Moderator
    edited August 2014

    @x00 said:
    But it does enforce schema. If student ids are unique, then they need a unique index in the database.

    The moral of the story would be to create a new column in the User table and make it a unique index. Then you just have to spit out a textbox with the same column name and Vanilla will validate it for you automatically.

    Below is a minimum viable plugin that requires a unique student ID number:

    public function EntryController_RegisterBeforePassword_Handler($Sender) {
      echo '<li>';
      echo $Sender->Form->Label('Student ID', 'StudentID');
      echo Wrap($Sender->Form->TextBox('StudentID'), 'div', array('class' => 'TextBoxWrapper'));
      echo '</li>';
    }
    
    public function Structure() {
      $Database = Gdn::Database();
      $Construct = $Database->Structure();
    
      $Construct->Table('User')
        ->Column('StudentID', 'int', false, 'unique')
        ->Set();
    }
    

    EDIT - I don't really like the form of the error returned. Is there an easy way to hook into the schema validation? I couldn't find anything.

    Search first

    Check out the Documentation! We are always looking for new content and pull requests.

    Click on insightful, awesome, and funny reactions to thank community volunteers for their valuable posts.

Sign In or Register to comment.