Search code examples
javajqueryjquery-pluginstapestry

How to set default date in DateField?


In my ActivitiesFilterControl component, I have two DateFields, which (int the .tml file) are inside the fieldset section:

<fieldset class="float_right">

  <t:label for="dateFrom">${message:search.form.label.dateFrom}</t:label>
  <t:datefield t:id="dateFrom" t:mixins="jquery/CustomDatepicker" t:value="dateFrom"
               t:params="calendarParams.params" format="prop:calendarParams.dateFormat" messages="messages" class="datepicker"/>

  <t:label for="dateTo">${message:search.form.label.dateTo}</t:label>
  <t:datefield t:id="dateTo" t:mixins="jquery/CustomDatepicker" t:value="dateTo"
               t:params="calendarParams.params" format="prop:calendarParams.dateFormat" messages="messages" class="datepicker"/>

</fieldset>

CustomDatepicker is this one: http://tapestry5-jquery.com/mixins/docscustomdatepicker

How can I set default values to the CustomDatepickers/DateFields? Here is the sample code from the component:

public class ActivitiesFilterControl extends FilterControl {

  @Persist
  @Property
  private Date dateFrom;

  @Persist
  @Property
  private Date dateTo;

  @InjectComponent("dateFrom")
  private DateField dateFromField;

  @InjectComponent("dateTo")
  private DateField dateToField;

}

Solution

  • Just initialize your component's fields for the render phase, e.g. via a setup render annotated method:

    @SetupRender
    final void init() {
        dateFrom = new Date(); // or sthg else
        dateTo = new Date(); // or sthg else
    }
    

    (no need to inject the embedded components in your java class - attributes "dateFromField" and "dateToField" of your code)

    Same thing for the calendarParams property you use for the params parameter of the CustomDatepicker mixin: you can initialize it at the same place.

    This tells Tapestry the initial values to use to produce the HTML page sent back to your browser for display.

    Note : Concerning form field values "dateFrom" & "dateTo", these values set in your @SetupRender method may be overriden by the ones memorised by the validation tracker, in case of a re-display of the page after validation errors have been detected.