Storing optional registration fields on login.php - SOLVED

Permalink 1 user found helpful
Hi!

I've been struggling with this problem and hoping someone here can point out what I'm doing wrong.

Basically the code below executes when the user has not populated mandatory registration fields. The ultimate aim is that when a user populates a drop down referring to their availability, then for certain values they are prompted to populate when they'll be available. The trouble is, although the user's response is included in the POST, it's not stored in the DB.

The attribute date_available is configured to show on the registration form (which I bypass through social login, hence showing it on login), but is not required on registration.

Any ideas why this data isn't being stored? Any help would be greatly appreciated!

$ak = UserAttributeKey::getByHandle('date_available');
print '<div class="ccm-profile-attribute">';
print $af->display($ak, $ak->isAttributeKeyRequiredOnRegister());
print '</div>';

 
mnakalay replied on at Permalink Reply
mnakalay
Sorry this is not very clear, have you also bypassed concrete5 attribute-saving code?

The code you are showing is just displaying a user attribute, nothing related to mandatory fields not being filled.

If you are saying the attribute date_available is showing up as expected on the registration page, then it is very strange that it's not saving.

But really if you could give more information it would be helpful
viDigital replied on at Permalink Reply 1 Attachment
Thanks for the response & your help! This is a bit new to me, but AFAIK I have not done anything to bypass the saving code.

I've attached the the file (had to change it to txt to upload it) taken from /public_html/concrete/single_pages/login.php

It's really strange as I've produced similar functionality in Edit Profile without a hitch.
mnakalay replied on at Permalink Reply
mnakalay
Here's the problem:
when submitting, the data is processed by the login controller in 2 stages. First the function do_login takes care of checking login and password. Then a second function finishlogin takes care of checking that all mandatory atributes have been filled.

That's where the problem is. Your attribute doesn't appear in the list of mandatory attributes so it's not processed and not saved.

You need to override that controller and modify the finishlogin function to take into account your extra attribute

You might make your life easier by just making that attribute mandatory in the first place. Just give it a bogus value such as "n/a" that will be saved anyway when the field is not needed
viDigital replied on at Permalink Best Answer Reply
That was a massive help, thanks so much. I'll share how I solved it incase anyone else may find the info useful.

First I tried adding 'n/a' but the validation freaked out as it's not in date format.

Finally, following your advice, I looked at the login controller (/public_html/concrete/controllers/login.php), made a copy of it in here (/public_html/controllers/). I added a couple of new lines (which I've commented below). The 1st change creates the object of the 'date_available' field, the second change then submits it with the rest of the POST. I kept the original code in the posts above to display the field on the form.

$this->set('invalidRegistrationFields', false);
      Loader::model('attribute/categories/user');
      $ui = UserInfo::getByID($u->getUserID());
      $aks = UserAttributeKey::getRegistrationList();
      $unfilledAttributes = array();
      foreach($aks as $uak) {
         if ($uak->isAttributeKeyRequiredOnRegister()) {
            $av = $ui->getAttributeValueObject($uak);
            if (!is_object($av)) {
               $unfilledAttributes[] = $uak;
            }
         }
      }
      //Change1: Create the date_available object
      $ak = UserAttributeKey::getByHandle('date_available');
mnakalay replied on at Permalink Reply
mnakalay
Hi again. I don't think this is going to work. You are not checking whether your custom attribute was filled or not, you need to make sure it has a value and if not, throw an error the same way they do for the other attributes.