You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tapestry.apache.org by Apache Wiki <wi...@apache.org> on 2007/12/27 02:21:47 UTC

[Tapestry Wiki] Update of "Tapestry5LocalizedDateField" by SvenHomburg

Dear Wiki user,

You have subscribed to a wiki page or wiki category on "Tapestry Wiki" for change notification.

The following page has been changed by SvenHomburg:
http://wiki.apache.org/tapestry/Tapestry5LocalizedDateField

New page:
==== question from mail-list ====
''
Is there a way to set the default !DateFormat for all !DateField[[BR]]
elements of a !BeanForm?[[BR]]
I know that I could specify the format in a <t:Parameter /> block for[[BR]]
the form. But then I'd have to do this on every occurrence of a Date[[BR]]
object.[[BR]]
Is there maybe some service that I could intercept when it is creating[[BR]]
!DateField instances and tell it what format to use?
''

==== possible solution ====
create a subclass from !DateField like this
{{{
public class LocaleDateField extends DateField
{
    @Inject
    private Messages _messages;

    /**
     * Invoked to allow subclasses to further configure the parameters passed to the JavaScript
     * Calendar.setup() function. The values inputField, ifFormat and button are pre-configured.
     * Subclasses may override this method to configure additional features of the client-side
     * Calendar. This implementation does nothing.
     *
     * @param setup parameters object
     */
    protected void configure(JSONObject setup)
    {
        setup.put("ifFormat", _messages.get("format.date.picker"));
    }
}
}}}
[[BR]]
create or add to application's global properties
{{{
format.date.field : dd.MM.yyyy
format.date.picker : %d.%m.%Y
}}}
[[BR]]
add the class 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;
    }

    @SuppressWarnings("unused")
    @Component(parameters = {"value=context.propertyValue", "label=prop:context.label", "clientId=prop:context.propertyid",
            "validate=prop:dateFieldValidator", "format=message:format.date.field"})
    private LocaleDateField _dateField;

    public FieldValidator getDateFieldValidator()
    {
        return _context.getValidator(_dateField);
    }
}
}}}
[[BR]]
add the tml-template 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>
}}}
[[BR]]
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!

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tapestry.apache.org
For additional commands, e-mail: dev-help@tapestry.apache.org