You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by Ryan <wi...@mandrake.us> on 2008/06/30 19:42:58 UTC

Slightly-OT: lower tier validation

I've come to love the validators in Wicket. They are simple, powerful
and have everything I need (regex patterns, localization, validators for
emails, etc).

My current project uses Hibernate+DAO, Spring services, and Wicket.  I
will be hooking in some web services into the service layer as well.
Anyway, I need to do validation atleast in the service layer (for the
API calls). I've looked at Spring validators, Commons Validator, and
Hibernate Validator. At this point I am leaning towards Hibernate
Validator because of the hooks it has at the data layer.

Does anyone have any pointers, suggestions, or opinions? Perhaps some wisdom from
past experiences?

Thanks,
Ryan

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Slightly-OT: lower tier validation

Posted by Ryan <wi...@mandrake.us>.
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: users-unsubscribe@wicket.apache.org
>For additional commands, e-mail: users-help@wicket.apache.org

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Slightly-OT: lower tier validation

Posted by Eelco Hillenius <ee...@gmail.com>.
> 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: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org