question from mail-list

_ Is there a way to set the default DateFormat for all DateField

elements of a BeanForm?

I know that I could specify the format in a <t:Parameter /> block for

the form. But then I'd have to do this on every occurrence of a Date

object.

Is there maybe some service that I could intercept when it is creating

DateField instances and tell it what format to use?_

possible solution

create or add to application's global properties

format.date.field : %d.%m.%Y

add the class AppPropertyEditBlocks in your page subpackage

public class AppPropertyEditBlocks
{
    @Environmental
    private PropertyEditContext _context;

    /**
     * get the context of the bean editor.
     *
     * @return the context of the bean editor
     */
    public PropertyEditContext getContext()
    {
        return _context;
    }

    @Component(parameters = {"value=context.propertyValue", "label=prop:context.label", "clientId=prop:context.propertyid",
            "validate=prop:dateFieldValidator", "format=prop:dateFieldFormat"})
    private DateField _dateField;

    public String getDateFieldFormat()
    {
        String dateFieldFormater = "%m/%d/%y";

        if (_messages.contains("format.date.field"))
            dateFieldFormater = _messages.get("format.date.field");

        return dateFieldFormater;
    }

    public FieldValidator getDateFieldValidator()
    {
        return _context.getValidator(_dateField);
    }
}

add the tml-template AppPropertyEditBlocks.tml in your page subpackage

<div xmlns:t="http://tapestry.apache.org/schema/tapestry_5_0_0.xsd">

    <t:block id="date">
        <t:label for="dateField"/>
        <input t:id="dateField"/>
    </t:block>

</div>

add to your IoC configuration the contributeBeanBlockSource method like this:

public class Tap5ComponentsModule
{
    public static void contributeBeanBlockSource(Configuration<BeanBlockContribution> configuration)
    {
        configuration.add(new BeanBlockContribution("date", "AppPropertyEditBlocks", "date", true));
    }
}

and restart your application,.... voila!

  • No labels