How to set a date field limitation

5 posts by 2 authors in: Forums > CMS Builder
Last Post: February 12, 2022   (RSS)

Hi

In my section, there are date fields. I would like to set some limitations. For example the value of the field date_of_treatment should always be greater or equal than the value of the field date_of_admission. How can I do it?

Regards,

Andreas Lazaris

By daniel - February 9, 2022

Hi Andreas,

Are you trying to apply these limits within the CMSB admin, or is it on a front-end form?

Thanks,

Daniel
Technical Lead
interactivetools.com

Hi Daniel

Thanks for your reply. Yes, I am trying to apply this limitation within the CMSB Admin environment.

Andreas

By daniel - February 10, 2022

Hi Andreas,

I think the most effective way to accomplish this would be to set up some error checking in a plugin using the record_presave hook. This would let you prevent a record from saving if it doesn't meet the constraints you provide. Using this hook would look something like this:

<?php
addAction('record_presave', 'your_presave_function', null, 3);
function your_presave_function($tableName, $isNewRecord, $oldRecord) {

  if ($tableName == 'your_table_name') {
    $formValues = _getRecordValuesFromFormInput();
    
    if (strtotime($formValues['later_date_fieldname']) < strtotime($formValues['earlier_date_fieldname'])) {
      echo 'Error Message';
      exit;
    }

  }

}
?>

With this solution, you will need to set up your own date constraint conditions - I've given an example of how to require one date field to be "greater" than another, though you would still need to substitute the correct table/field names. This code would need to be in an active plugin to be executed by the CMS - there should be an example plugin by default in all CMSB installations called "samplePlugin.php"

Let me know if this helps, or if you have any other questions!

Thanks,

Daniel
Technical Lead
interactivetools.com

By andreasml - February 12, 2022 - edited: February 12, 2022

Hi Daniel

Great work! It works like a charm!! Thanks for your kind assistance.

Best regards,

Andreas