You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by James Turner <tu...@blackbear.com> on 2002/07/12 04:12:49 UTC
Validating DynaBeans
More Dynabean stuff.
Looking over the new form-property DynaBean stuff in the struts config
file, it strikes me that the one thing that would make it work seamlessly
for 90% of all forms I ever code would be to allow the individual
properties to have validation functions rather than require a validation
function for the bean at as a whole.
For example, I could define a user login form as such:
<form-bean name="loginForm" type="org.apache.struts.action.DynaActionForm"
dynamic="true" >
<form-property name="user" type="java.lang.String"
validate="com.mypackage.FormValidators.requiredFieldValidate"/>
</form-bean>
Then, in FormValidators:
public static void requiredFieldValidate(String fieldValue, String
fieldName, ActionErrors errors) {
if ((fieldValue == null) || (fieldValue.size() == 0)) {
ActionErrors.add(fieldName, new ActionError("validator.required.field"));
}
}
The nice thing about this is that you can reuse the same validation
functions across many similar fields.
I know this is somewhat redundant with the DynaValidator stuff, but I'd
like to be able to use my own validation
approach with DynaForms if I want.
Is there merit in this. Have I yet again missed a an easier way of doing this?
James
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>
Re: Validating DynaBeans
Posted by "Craig R. McClanahan" <cr...@apache.org>.
On Thu, 11 Jul 2002, James Turner wrote:
> Date: Thu, 11 Jul 2002 22:12:49 -0400
> From: James Turner <tu...@blackbear.com>
> Reply-To: Struts Users Mailing List <st...@jakarta.apache.org>
> To: struts-user@jakarta.apache.org
> Subject: Validating DynaBeans
>
> More Dynabean stuff.
>
> Looking over the new form-property DynaBean stuff in the struts config
> file, it strikes me that the one thing that would make it work seamlessly
> for 90% of all forms I ever code would be to allow the individual
> properties to have validation functions rather than require a validation
> function for the bean at as a whole.
>
> For example, I could define a user login form as such:
>
> <form-bean name="loginForm" type="org.apache.struts.action.DynaActionForm"
> dynamic="true" >
> <form-property name="user" type="java.lang.String"
> validate="com.mypackage.FormValidators.requiredFieldValidate"/>
> </form-bean>
>
> Then, in FormValidators:
>
> public static void requiredFieldValidate(String fieldValue, String
> fieldName, ActionErrors errors) {
> if ((fieldValue == null) || (fieldValue.size() == 0)) {
> ActionErrors.add(fieldName, new ActionError("validator.required.field"));
> }
> }
>
> The nice thing about this is that you can reuse the same validation
> functions across many similar fields.
>
> I know this is somewhat redundant with the DynaValidator stuff, but I'd
> like to be able to use my own validation
> approach with DynaForms if I want.
>
> Is there merit in this. Have I yet again missed a an easier way of
> doing this?
>
The validator framework already lets you write reusable custom validation
functions, and it already supports dynabeans -- what do you need that is
not already available?
> James
>
Craig
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>