You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by John Doe <ba...@gmail.com> on 2007/10/26 12:36:07 UTC

Struts 1.2.7 custom validator problem

Hi,
I have Struts 1.2.7 application and I need to write custom validator for my
form. I've wrote struts tutorial at
 http://struts.apache.org/1.2.7/userGuide/dev_validator.html
and have tried to do the same. But my validator is not working. Here is my
class method :

public class GroupIdValidator {

    public static boolean checkGroupId(Object bean, Field field,
HttpServletRequest request) {

        return false;
    }

as you can see it's just the test method, than always returns FALSE. And
here is my validation-rules.xml :

        <validator name="uniqueGroupId"
                   classname="com.mysite.validators.GroupIdValidator"
                   method="checkGroupId"
                   msg="errors.groupid.unique"
                   methodParams="java.lang.Object,
                       org.apache.commons.validator.Field,
                       javax.servlet.http.HttpServletRequest"
                   depends="required"
                />

and validation.xml for my form :

        <form name="addGroupForm">
            <field property="id" depends="required,mask,uniqueGroupId">
                <arg0 key="formGroup.groupName"/>
                <msg name="mask" key="errors.mask" />
                <arg1 key="mask.devicegroupid" resource="true"/>
                <var>
                    <var-name>mask</var-name>
                    <var-value>^[a-zA-Z0-9_]*$</var-value>
                </var>
            </field>

Every thing looks great, but when I submit my form it passes validation
(when uniqueGroupId always return false)! It seems like my validator returns
true. The strange thing is that my validation method calls and works normaly
and it should always fail field validation.
What is wrong with my custom pluggable validator?

-- 
Best regards,
Bashmaкov Anton

Re: Struts 1.2.7 custom validator problem

Posted by Paul Benedict <pb...@apache.org>.
I wonder what the purpose is of returning false.

On 10/30/07, John Doe <ba...@gmail.com> wrote:
>
> I've solved the problem. The only way to validate properly is to add an
> error to the errors variable. Without this validator will think that
> everything is OK even if you returned false in validate method.
>
> On 10/29/07, John Doe <ba...@gmail.com> wrote:
> >
> > No, there is no exception.
> >
> > On 10/28/07, Paul Benedict <pb...@apache.org> wrote:
> > >
> > > I think validation can pass if an exception is thrown. Please check
> your
> > > log
> > > files that no error is occurring with your validation.
> > >
> > > Paul
> > >
> >
> >
> >
> > --
> > Best regards,
> > Bashmaкov Anton
>
>
>
>
> --
> Best regards,
> Bashmaкov Anton
>

Re: Struts 1.2.7 custom validator problem

Posted by John Doe <ba...@gmail.com>.
I've solved the problem. The only way to validate properly is to add an
error to the errors variable. Without this validator will think that
everything is OK even if you returned false in validate method.

On 10/29/07, John Doe <ba...@gmail.com> wrote:
>
> No, there is no exception.
>
> On 10/28/07, Paul Benedict <pb...@apache.org> wrote:
> >
> > I think validation can pass if an exception is thrown. Please check your
> > log
> > files that no error is occurring with your validation.
> >
> > Paul
> >
>
>
>
> --
> Best regards,
> Bashmaкov Anton




-- 
Best regards,
Bashmaкov Anton

Re: Struts 1.2.7 custom validator problem

Posted by John Doe <ba...@gmail.com>.
No, there is no exception.

On 10/28/07, Paul Benedict <pb...@apache.org> wrote:
>
> I think validation can pass if an exception is thrown. Please check your
> log
> files that no error is occurring with your validation.
>
> Paul
>



-- 
Best regards,
Bashmaкov Anton

Re: Struts 1.2.7 custom validator problem

Posted by Paul Benedict <pb...@apache.org>.
I think validation can pass if an exception is thrown. Please check your log
files that no error is occurring with your validation.

Paul

Re: Struts 1.2.7 custom validator problem

Posted by John Doe <ba...@gmail.com>.
Yes. I have validation working correctly. Required and mask works good. And
my validator method gets called and returns false, but I do not add any
errors message is it necessary?

On 10/26/07, Laurie Harper <la...@holoweb.net> wrote:
>
> John Doe wrote:
> > Hi,
> > I have Struts 1.2.7 application and I need to write custom validator for
> my
> > form. I've wrote struts tutorial at
> >  http://struts.apache.org/1.2.7/userGuide/dev_validator.html
> > and have tried to do the same. But my validator is not working. Here is
> my
> > class method :
> >
> > public class GroupIdValidator {
> >
> >     public static boolean checkGroupId(Object bean, Field field,
> > HttpServletRequest request) {
> >
> >         return false;
> >     }
> >
> > as you can see it's just the test method, than always returns FALSE. And
> > here is my validation-rules.xml :
> >
> >         <validator name="uniqueGroupId"
> >                    classname="com.mysite.validators.GroupIdValidator"
> >                    method="checkGroupId"
> >                    msg="errors.groupid.unique"
> >                    methodParams="java.lang.Object,
> >                        org.apache.commons.validator.Field,
> >                        javax.servlet.http.HttpServletRequest"
> >                    depends="required"
> >                 />
> >
> > and validation.xml for my form :
> >
> >         <form name="addGroupForm">
> >             <field property="id" depends="required,mask,uniqueGroupId">
> >                 <arg0 key="formGroup.groupName"/>
> >                 <msg name="mask" key="errors.mask" />
> >                 <arg1 key="mask.devicegroupid" resource="true"/>
> >                 <var>
> >                     <var-name>mask</var-name>
> >                     <var-value>^[a-zA-Z0-9_]*$</var-value>
> >                 </var>
> >             </field>
> >
> > Every thing looks great, but when I submit my form it passes validation
> > (when uniqueGroupId always return false)! It seems like my validator
> returns
> > true. The strange thing is that my validation method calls and works
> normaly
> > and it should always fail field validation.
> > What is wrong with my custom pluggable validator?
>
> Do you have the standard validation working correctly? I.e. if you
> remove uniqueGroupId are the required and mask validators applied
> correctly? Have you confirmed that your custom validation method gets
> called? What happens if you have it add a validation error message as
> well as return false?
>
> L.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>


-- 
Best regards,
Bashmaкov Anton

Re: Struts 1.2.7 custom validator problem

Posted by Laurie Harper <la...@holoweb.net>.
John Doe wrote:
> Hi,
> I have Struts 1.2.7 application and I need to write custom validator for my
> form. I've wrote struts tutorial at
>  http://struts.apache.org/1.2.7/userGuide/dev_validator.html
> and have tried to do the same. But my validator is not working. Here is my
> class method :
> 
> public class GroupIdValidator {
> 
>     public static boolean checkGroupId(Object bean, Field field,
> HttpServletRequest request) {
> 
>         return false;
>     }
> 
> as you can see it's just the test method, than always returns FALSE. And
> here is my validation-rules.xml :
> 
>         <validator name="uniqueGroupId"
>                    classname="com.mysite.validators.GroupIdValidator"
>                    method="checkGroupId"
>                    msg="errors.groupid.unique"
>                    methodParams="java.lang.Object,
>                        org.apache.commons.validator.Field,
>                        javax.servlet.http.HttpServletRequest"
>                    depends="required"
>                 />
> 
> and validation.xml for my form :
> 
>         <form name="addGroupForm">
>             <field property="id" depends="required,mask,uniqueGroupId">
>                 <arg0 key="formGroup.groupName"/>
>                 <msg name="mask" key="errors.mask" />
>                 <arg1 key="mask.devicegroupid" resource="true"/>
>                 <var>
>                     <var-name>mask</var-name>
>                     <var-value>^[a-zA-Z0-9_]*$</var-value>
>                 </var>
>             </field>
> 
> Every thing looks great, but when I submit my form it passes validation
> (when uniqueGroupId always return false)! It seems like my validator returns
> true. The strange thing is that my validation method calls and works normaly
> and it should always fail field validation.
> What is wrong with my custom pluggable validator?

Do you have the standard validation working correctly? I.e. if you 
remove uniqueGroupId are the required and mask validators applied 
correctly? Have you confirmed that your custom validation method gets 
called? What happens if you have it add a validation error message as 
well as return false?

L.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org