2

I have a custom module that invokes the civicrm_buildForm hook to set the default date for a given field to today's date.

It is working - in part. When I examine the code I see that the default value is set. However, it is set in the input that is hidden. Whereas there is a second input that is displayed using what appears to be a randomly generated id (dp1479931180679) which seems to be created by the date picker widget.

If you leave the field blank, the default date is picked up and works. But it's a usability issue since it doesn't appear to be filled out to the user.

Anyone have a work around?

Here is what the html code looks like:

<span class="crm-form-date-wrapper">
<input id="custom_17" class="crm-form-text required crm-hidden-date" data-crm-custom="Constituent_Info__Individuals:Date_Started" data-crm-datepicker="{"date":"mm\/dd\/yy","minDate":"1986-01-01","maxDate":null,"time":false}" name="custom_17" value="11/28/2016" style="display: none;" type="text">
<input id="dp1480365364891" class="crm-form-text required crm-form-date hasDatepicker" style="" type="text">
</span>

The first input (which properly shows the right value) is hidden. The second one, which is the one displayed, doesn't have a value set.

3
  • Hm, your date format is "mm\/dd\/yy", and your date is "11/28/2016". Maybe it has to do with the escape character before the slash. Nov 28, 2016 at 21:14
  • Where you able to figure this out?
    – Moe
    Oct 30, 2019 at 8:50
  • Yes - See @TwoMice solved my problem - however, I think the solution requires both answers.
    – Jamie
    Nov 1, 2019 at 14:08

3 Answers 3

2

You don't say your version, but I'm guessing 4.7 from the description. The trick is to use ISO date format. So this should work:

function civitest_civicrm_buildForm($formName, &$form) {
  $defaults['register_date'] = date("2017-04-15");
  $form->setDefaults($defaults);
}

FWIW, this is still an issue for setting defaults via URL query string parameters, as of 4.7.22; there's now an open ticket with PR here: https://issues.civicrm.org/jira/browse/CRM-20922

From https://issues.civicrm.org/jira/browse/CRM-18349:

In 4.7 custom fields of type date do not use the jcalendar.tpl but have been upgraded to use the quickform binding of https://wiki.civicrm.org/confluence/display/CRMDOC/crmDatepicker

1
  • Hooray!!! Success at last! Thank you @TwoMice.
    – Jamie
    Jul 21, 2017 at 13:47
4

I think the date widget have its default value based on the format it is being set.

Eg - For mm/dd/yyyy input format set in Administer -> Localization -> Date Formats.

This works -

function civitest_civicrm_buildForm($formName, &$form) {
  $defaults['register_date'] = date("m/d/Y");
  $form->setDefaults($defaults);
}

This doesn't -

$defaults['register_date'] = date("m-d-Y");
$defaults['register_date'] = date("d/m/Y");
1
  • Thanks for the answer. Unfortunately, it still doesn't seem to work (it appears I was already adding the right format). I just updated the question to show the output that I'm getting.
    – Jamie
    Nov 28, 2016 at 20:41
0

Try inspecting and changing the form element type to a text element and then hide it via css.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.