Hi,
last year I had some toipics with Nigel regarding the calculating of the age of a person at the beggining of an event, for which this person registered. Until now I used a datefield and a custom solution from Nigel to calculate that age. But there are sometimes problems with this jQuery solution, because for some reason the age is 0 for some persons. Now I want to use three textfields for the users, where they can input their birthdate day, birthdate month and birthdate year. Based on their input I want to calculate the age of the person at the beginning of the event but after submitting the form field. Please see also topic https://toolset.com/forums/topic/help-using-cred_save_data-hook-option-after-sending-form/#post-1146571
Is there an easier way to realize that?
Thanks
Thorsten
I think the better solution is to figure out why the age is sometimes 0 given the existing date field, and fix that problem, rather than introducing a whole other set of problems by adding three more input fields. If necessary you can use the cred_form_validate hook to check the value of the birthday date field, and display a message and error if the value is not set appropriately.
Hi Christian,
the age of 0 problem only occurs sometimes. I couldn't reproduce the problem, because it works fine from my side. The solution now is a custom jquery-solution where the age will be calculated live after picking the dates from the datepicker. So if I understood you right, the cred_form_validate hook validates the age from server-side, right? If yes, do I have to put this hook into the custom code section in the Toolset settings? The calculation has to similar like in the jQuery code I guess?
// Edit fields as required
var dateField = 'geburts-datum';
var ageField = 'alter-neu';
var eventSelector = '#veranstaltungs-beginn';
// Get current datepicker options
var currentOptions = $('input[name="wpcf-' + dateField + '[display-only]"]').datepicker('option', 'all');
// Set new option for function to calculate age and update age field
var newOptions = {
yearRange: "1900:2050",
onClose: function(dateText, instance) {
if (instance.currentYear == 0) return;
// get event start date
var eventStart = $(eventSelector).text();
var eventStartInt = parseInt(eventStart);
var eventDate = new Date(eventStartInt * 1000);
var eventYear = eventDate.getFullYear();
var eventMonth = (eventDate.getMonth()) + 1;
var eventDay = eventDate.getDate();
var selectedYear = instance.selectedYear;
var selectedMonth = (instance.selectedMonth) + 1;
var selectedDay = instance.selectedDay;
var age = eventYear - selectedYear;
if (selectedMonth > eventMonth) age--;
else {
if (selectedMonth == eventMonth) {
if (selectedDay > eventDay) age--;
}
}
// update target field with age
$('input[name="wpcf-' + ageField + '"]').val(age);
$("#alter-anzeige").text(age).appendTo("#alter-veranstaltungsbeginn");
//$("#alter-anzeige").appendTo("#alter-veranstaltungsbeginn");
}
};
So if I understood you right, the cred_form_validate hook validates the age from server-side, right? If yes, do I have to put this hook into the custom code section in the Toolset settings?
Yes the cred_form_validate hook is triggered on the backend when the form is submitted. You may put your PHP code in the custom code section of Toolset > Settings, or you may put the code in your child theme's functions.php file. However, if you are already using jQuery to perform front-end validation and form input manipulation, it may not be necessary. You might be able to add your own validation to the custom JS code.