You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by captain_rhino <gr...@axa-travel-insurance.com> on 2011/07/21 12:05:45 UTC
Date Field with day, month year and datepicker component
The current datepicker component does not enable individual day,month, year
to be inoutted through 3 individual drop down boxes. I have seen some
discussion on the subject but can someone point me in the right direction on
how to achieve such a component?
Is there a component out there that alreday does this? (I've searched and
failed)
If I have to write my own component can anyone make a few suggestions
please?
P.s. I'm an experienced Struts developer relatively new to Tapestry -
looking to use it for a new project.
--
View this message in context: http://tapestry.1045711.n5.nabble.com/Date-Field-with-day-month-year-and-datepicker-component-tp4618810p4618810.html
Sent from the Tapestry - User mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org
Re: Date Field with day, month year and datepicker component
Posted by kleanthis <kl...@gmail.com>.
Well this is my component, which does exactly that. However i haven't manage
to implement dynamic client side validation(required true/false). have a go
and let me know if you improve it. It's based on the chenillekit datepicker
component.
the java file without package:
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import org.apache.tapestry5.Binding;
import org.apache.tapestry5.BindingConstants;
import org.apache.tapestry5.ComponentResources;
import org.apache.tapestry5.FieldValidationSupport;
import org.apache.tapestry5.FieldValidator;
import org.apache.tapestry5.MarkupWriter;
import org.apache.tapestry5.OptionModel;
import org.apache.tapestry5.SelectModel;
import org.apache.tapestry5.ValidationException;
import org.apache.tapestry5.ValidationTracker;
import org.apache.tapestry5.annotations.Environmental;
import org.apache.tapestry5.annotations.Parameter;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.corelib.base.AbstractField;
import org.apache.tapestry5.internal.OptionModelImpl;
import org.apache.tapestry5.internal.SelectModelImpl;
import org.apache.tapestry5.ioc.Messages;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.services.ComponentDefaultProvider;
import org.apache.tapestry5.services.Request;
/**
* @author mailto:shomburg@depot120.dpd.de S.Homburg
* @version $Id: DateSelector.java 471 2009-05-09 09:28:38Z homburgs $
*/
//@Import(library = {
// "context:js/datepick.js"})
public class DateSelector extends AbstractField
{
@Parameter(required = true, defaultPrefix =
BindingConstants.PROP,autoconnect = true)
private Date value;
@SuppressWarnings("unused")
@Parameter(defaultPrefix = BindingConstants.PROP, value = "false")
private boolean longMonth;
/**
* The object that will perform input validation (which occurs after
translation). The validate binding prefix is
* generally used to provide this object in a declarative fashion.
*/
@Parameter(defaultPrefix = BindingConstants.VALIDATE)
private FieldValidator validate;
@Inject
private Locale locale;
@Inject
private ComponentDefaultProvider defaultProvider;
@Inject
private ComponentResources resources;
@Inject
private Request request;
@Inject
private Messages messages;
@Property
private Integer day;
@Property
private Integer month;
@Property
private Integer year;
@Environmental
private ValidationTracker validationTracker;
@Inject
private FieldValidationSupport fieldValidationSupport;
private Calendar calendar = Calendar.getInstance(locale);
/**
* Computes a default value for the "validate" parameter using {@link
org.apache.tapestry5.services.FieldValidatorDefaultSource}.
*/
final Binding defaultValidate()
{
return defaultProvider.defaultValidatorBinding("value", resources);
}
/**
* Tapestry render phase method.
* Initialize temporary instance variables here.
*/
void setupRender()
{
if (value != null){
calendar.setTime(value);
day=calendar.get(Calendar.DAY_OF_MONTH);
month=calendar.get(Calendar.MONTH);
year=calendar.get(Calendar.YEAR);
}
}
/**
* Tapestry render phase method.
* Start a tag here, end it in afterRender
*/
void beginRender(MarkupWriter writer)
{
}
/**
* Tapestry render phase method. End a tag here.
*/
void afterRender(MarkupWriter writer)
{
}
/**
* Method implemented by subclasses to actually do the work of processing
the submission of the form. The element's
* elementName property will already have been set. This method is only
invoked if the field is <strong>not {@link
* #isDisabled() disabled}</strong>.
*
* @param elementName the name of the element (used to find the correct
parameter in the request)
*/
@Override
protected void processSubmission(String elementName)
{
Date translated = null;
try
{
day = Integer.parseInt(request.getParameter("days"));
month = Integer.parseInt(request.getParameter("months"));
year = Integer.parseInt(request.getParameter("years"));
calendar.set(year, month, day);
calendar.setLenient(false);
translated = calendar.getTime();
fieldValidationSupport.validate(translated, resources, validate);
}
catch(NumberFormatException ex){
value=null;
if(validate.isRequired() && value == null){
validationTracker.recordError(this, messages.get("date-not-null"));
}
}
catch (IllegalArgumentException ex)
{
if (ex.getMessage().equals("MONTH")){
validationTracker.recordError(this, messages.get("incorrect-date"));
}
value=null;
if(validate.isRequired() && value == null){
validationTracker.recordError(this, messages.get("date-not-null"));
}
}
catch (ValidationException ex)
{
// if(ex.getMessage().contains(""))
validationTracker.recordError(this, ex.getLocalizedMessage());
value=null;
}
value = translated;
}
@Override
public boolean isRequired()
{
return validate.isRequired();
}
public FieldValidator getValidate(){
return validate;
}
public SelectModel getdayModel(){
List<OptionModel> optionModels = new ArrayList<OptionModel>();
SelectModel res = new SelectModelImpl(null, optionModels);
for (int day = 1; day < 32; day++)
{
OptionModel temp = new OptionModelImpl(new Integer(day).toString(),day);
optionModels.add(temp);
}
return res;
}
public SelectModel getMonthModel(){
List<OptionModel> optionModels = new ArrayList<OptionModel>();
SelectModel res = new SelectModelImpl(null, optionModels);
for (int month = 0; month < 12; month++)
{
OptionModel temp = new OptionModelImpl(new
Integer(month+1).toString(),month);
optionModels.add(temp);
}
return res;
}
public SelectModel getYearModel(){
List<OptionModel> optionModels = new ArrayList<OptionModel>();
SelectModel res = new SelectModelImpl(null, optionModels);
for (int year = Calendar.getInstance().get(Calendar.YEAR)-18; year >=
Calendar.getInstance().get(Calendar.YEAR)-100; year--)
{
OptionModel temp = new OptionModelImpl(new
Integer(year).toString(),year);
optionModels.add(temp);
}
return res;
}
public String getId(){
return super.getClientId();
}
}
and the tml file is
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:t="http://tapestry.apache.org/schema/tapestry_5_1_0.xsd"
xmlns:p="tapestry:parameter">
<t:select t:id="days" model="dayModel" value="day" blankOption="ALWAYS"
blankLabel="Day" validate="required" />
<t:select t:id="months" model="monthModel" value="month"
blankOption="ALWAYS" blankLabel="Month" validate="required" />
<t:select t:id="years" model="yearModel" value="year"
blankOption="ALWAYS" blankLabel="Year" validate="required" />
</html>
properties as well
incorrect-date=Invalid date. Please fill in all the input fields correctly
for the date.
date-not-null=Date is required.
Good Luck
-----
Do not confuse motion for action.
--
View this message in context: http://tapestry.1045711.n5.nabble.com/Date-Field-with-day-month-year-and-datepicker-component-tp4618810p4630542.html
Sent from the Tapestry - User mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org