Hi Guys, Ive been working on forms a lot lately, and what ive been missing from tapestry is a fieldlabel component. Basically a fieldlabel is a label that is linked to a form component.
This allows to do cool things like: 1) FieldLabel can change its apperance based on the form component TextField tf=new RequiredTextField(...) FieldLabel lb=new FieldLabel("label1","First Name", tf) { public void initialize() { add(new AttributeModifier("style", true, new Model("color:red;")) { public boolean isEnabled() { return !getFieldComponent().isValid(); } }); } }; This will create a field label that will turn red when the linked form component has an error. Another good use is to prepand an asterisk to the fieldlabel's label if the linked form component has a requiredvalidator added. 2) FieldLabel's label can participate in validator messages With this patch it is possible to create generic error messages of form : "'${label}' is required" or "'${label}' contains an invalid email address" I am looking for feedback and ideas on how to make this better before I submit this as a patch. Thank you! Igor
Index: wicket/markup/html/form/FormComponent.java =================================================================== RCS file: /cvsroot/wicket/wicket/src/java/wicket/markup/html/form/FormComponent.java,v retrieving revision 1.43 diff -u -r1.43 FormComponent.java --- wicket/markup/html/form/FormComponent.java 28 Jul 2005 11:56:51 -0000 1.43 +++ wicket/markup/html/form/FormComponent.java 30 Jul 2005 02:32:19 -0000 @@ -30,6 +30,7 @@ import wicket.model.IModel; import wicket.util.lang.Classes; import wicket.util.string.StringList; +import wicket.version.undo.Change; /** * An html form component knows how to validate itself. Validators that @@ -71,6 +72,9 @@ /** The validator or validator list for this component. */ private IValidator validator = IValidator.NULL; + /** The field label for this component */ + private FieldLabel fieldLabel; + /** * Typesafe interface to code that is called when visiting a form component * @@ -575,4 +579,24 @@ { validator.validate(this); } + + protected FormComponent setFieldLabel(FieldLabel label) { + if (fieldLabel!=null) { + + addStateChange(new Change() { + private final FieldLabel oldFieldLabel=FormComponent.this.fieldLabel; + + public void undo() + { + FormComponent.this.fieldLabel=oldFieldLabel; + } + }); + } + this.fieldLabel=label; + return this; + } + + public FieldLabel getFieldLabel() { + return fieldLabel; + } } \ No newline at end of file Index: wicket/markup/html/form/validation/AbstractValidator.java =================================================================== RCS file: /cvsroot/wicket/wicket/src/java/wicket/markup/html/form/validation/AbstractValidator.java,v retrieving revision 1.31 diff -u -r1.31 AbstractValidator.java --- wicket/markup/html/form/validation/AbstractValidator.java 21 Jul 2005 10:46:36 -0000 1.31 +++ wicket/markup/html/form/validation/AbstractValidator.java 30 Jul 2005 02:32:19 -0000 @@ -21,6 +21,7 @@ import java.util.Map; import wicket.Localizer; +import wicket.markup.html.form.FieldLabel; import wicket.markup.html.form.FormComponent; import wicket.model.IModel; import wicket.model.Model; @@ -139,6 +140,21 @@ final Map resourceModel = new HashMap(4); resourceModel.put("input", formComponent.getInput()); resourceModel.put("name", formComponent.getId()); + + // try to retrieve the label from field label, default to empty string + String labelValue = ""; + FieldLabel label = formComponent.getFieldLabel(); + if (label != null) + { + Object modelObject = label.getModelObject(); + if (modelObject != null) + { + labelValue = modelObject.toString(); + } + } + resourceModel.put("label", labelValue); + + return resourceModel; } } Index: wicket/markup/html/form/FieldLabel.java =================================================================== RCS file: wicket/markup/html/form/FieldLabel.java diff -N wicket/markup/html/form/FieldLabel.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ wicket/markup/html/form/FieldLabel.java 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,55 @@ +package wicket.markup.html.form; + +import wicket.AttributeModifier; +import wicket.Component; +import wicket.markup.html.WebMarkupContainer; +import wicket.markup.html.basic.Label; +import wicket.markup.html.panel.Panel; +import wicket.model.IModel; +import wicket.model.Model; +import wicket.model.PropertyModel; + +public class FieldLabel extends Label +{ + protected FormComponent fc; + + public FieldLabel(String id, FormComponent formComponent) + { + super(id); + internalInitialize(formComponent); + } + + public FieldLabel(String id, String string, FormComponent formComponent) + { + super(id, string); + internalInitialize(formComponent); + } + + public FieldLabel(String id, IModel model, FormComponent formComponent) + { + super(id, model); + internalInitialize(formComponent); + } + + private void internalInitialize(FormComponent formComponent) + { + if (fc==null) throw new IllegalArgumentException("formComponent cannot be null"); + this.fc=fc; + + fc.setFieldLabel(this); + setRenderBodyOnly(true); + + initialize(); + } + + public final FormComponent getFormComponent() { + return fc; + } + + public void initialize() { + + } + + + +}