Login UpdateProfile date field not saving, and resetting

When using the Login UpdateProfile snippet the Date of birth field isn’t saving properly and will reset after the form is saved. If I set the date in the manager it fills into the form properly using the following input field.

<input type="date" name="dob" id="dob" value="[[+dob:date=`%Y-%m-%d`]]" />

But, after saving the form it causes the date to reset to 12/31/1969. Manager shows the same date. The error log doesn’t show anything.

If I show the [[+dob]] placeholder unformatted it will show the year from whatever date I chose.

The value for the “Date of birth” is stored in the database as a single number - a timestamp. (database table modx_user_attributes → column dob.)

Maybe when using “UpdateProfile” on the front-end, you have to do the conversion from a date to a timestamp manually (in a prehook snippet). Similar to how it is done when a user is saved in the manager:

Here is some example code (that might work):

[[!UpdateProfile?
    &preHooks=`myDobPrehook`
]]

<form class="form" action="[[~[[*id]]]]" method="post">
    <input type="date" name="dob" id="dob" value="[[+dob:date=`%Y-%m-%d`]]" />
    <input type="submit" name="login-updprof-btn" value="[[!%login.update_profile]]" />
</form>

Snippet “myDobPrehook”:

<?php
// Convert birth date from string to timestamp
$dob_as_string = $hook->getValue("dob");
$dob_as_date = \DateTimeImmutable::createFromFormat('Y-m-d', $dob_as_string);
if ($dob_as_date === false) {
    $hook->setValue("dob", 0);
} else {
    $dob_as_timestamp = $dob_as_date->getTimestamp();
    $hook->setValue("dob", $dob_as_timestamp);
}

return true;

That seems to work on my end. Thanks.

I tried getting help from Microsoft CoPilot time time and after about 40 different suggestions, it still couldn’t figure it out.

This topic was automatically closed 2 days after discussion ended and a solution was marked. New replies are no longer allowed. You can open a new topic by clicking the link icon below the original post or solution and selecting “+ New Topic”.