Eelco, This is perfect and a great idea. Thanks for sharing. If this isn't posted somewhere on the wiki I think it would be a great entry.
Thanks again!, Ryan On Mon, Jun 30, 2008 at 11:08:59AM -0700, Eelco Hillenius exclaimed: >> Does anyone have any pointers, suggestions, or opinions? Perhaps some wisdom >> from >> past experiences? > >I think Hibernate Validator would be a good way to go. Consider >implementing a variant of IComponentOnBeforeRenderListener to add some >basic validations so that you don't have to get through your business >tier all the time, and can do things like setting field sizes in your >UI automatically. Something like (adjust to your own needs): > >public final class ValidationListener implements >IComponentOnBeforeRenderListener { > > public void onBeforeRender(Component component) { > if (component instanceof FormComponent && > !component.hasBeenRendered()) { > processComponent((FormComponent) component); > } > } > > public void processComponent(FormComponent component) { > IModel model = component.getModel(); > if (model instanceof IPropertyReflectionAwareModel) { > Field field = ((IPropertyReflectionAwareModel) > model).getPropertyField(); > if (field != null) { > processComponentField(field, component); > } > } > } > > private void processComponentField(Field field, FormComponent > component) { > > if (field.getDeclaredAnnotations().length > 0) { > > if (field.isAnnotationPresent(Column.class)) { > > boolean big = > field.isAnnotationPresent((Lob.class)); > Column column = > field.getAnnotation(Column.class); > > Class<?> type = field.getType(); > if (big == false && type.equals(String.class)) { > addMaxLengthValidator(component, > column.length()); > } > > if (column.nullable() == false && type != > Boolean.class && type != >Boolean.TYPE) { > component.setRequired(true); > } > } > > if (field.isAnnotationPresent(EmailValidator.class)) { > EmailValidator validator = > field.getAnnotation(EmailValidator.class); > > > component.add(EmailAddressValidator.getInstance()); > > if (validator.required()) { > component.setRequired(true); > } > } > > if > (field.isAnnotationPresent(ts4.valid.annot.StringValidator.class)) { > ts4.valid.annot.StringValidator validator = >field.getAnnotation(ts4.valid.annot.StringValidator.class); > > if (validator.required()) { > component.setRequired(true); > } > if (validator.min() != 0) { > > component.add(StringValidator.minimumLength(validator.min())); > } > if (validator.max() != Integer.MAX_VALUE) { > > component.add(StringValidator.maximumLength(validator.max())); > } > } > } > } > > private void addMaxLengthValidator(FormComponent component, final int > length) { > component.add(StringValidator.maximumLength(length)); > > if (component instanceof TextField) { > component.add(new AbstractBehavior() { > private static final long serialVersionUID = 1L; > > @Override > public void onComponentTag(Component component, > ComponentTag tag) { > if > (tag.getAttributes().get("maxlength") == null) { > tag.put("maxlength", length); > } > } > }); > } > } >} > > >Eelco > >--------------------------------------------------------------------- >To unsubscribe, e-mail: [EMAIL PROTECTED] >For additional commands, e-mail: [EMAIL PROTECTED] --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]