aspose file tools*
The moose likes Struts and the fly likes custom validator and custom tags Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "custom validator and custom tags" Watch "custom validator and custom tags" New topic
Author

custom validator and custom tags

Neha Khandelwal
Greenhorn

Joined: Aug 04, 2010
Posts: 4
hii plzz tell me how to create our own custom validator and custom tag .

in my struts program i have use validation-user.xml and validation-rule.xml
but this validator are not working

the validation which are working in my program are those which are used in form class method name

public ActionErrors validate(ActionMapping mapping, HttpServletRequest request)
{
if (password == null || password.trim().equals(""))
{
errors.add("password", new ActionError("error.password.required"));
}
}

like these validator are working

i want to know why my validator.xml file validation are not working

how i can make Custom Validator and Tag

plzz reply me ....i need help

validator-user.xml

<!DOCTYPE form-validation PUBLIC
"-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.3.0//EN"
"http://jakarta.apache.org/commons/dtds/validator_1_3_0.dtd">

<form-validation>
<formset>
<form name="UserValidationForm">
<field property="loginid" depends="required,maxlength,minlength,mask" >

<msg name="required" key="error.loginid.required" />
<msg name="maxlength" key="error.loginid.length" />
<msg name="minlength" key="error.loginid.length" />
<msg name="mask" key="error.loginid.invalid" />

<arg name="maxlength" key="${var:minlength}" position="0" resource="false"/>
<arg name="maxlength" key="${var:maxlength}" position="1" resource="false"/>

<arg name="minlength" key="${var:minlength}" position="0" resource="false"/>
<arg name="minlength" key="${var:maxlength}" position="1" resource="false"/>

<arg key="UserValidationForm.loginid"/>

<var>
<var-name>minlength</var-name>
<var-value>3</var-value>
</var>
<var>
<var-name>maxlength</var-name>
<var-value>15</var-value>
</var>

<var>
<var-name>mask</var-name>
<var-value>^[a-zA-Z0-9-_]*$</var-value>
</var>

</field>


<field property="password" depends="required,maxlength,minlength,mask">

<msg name="required" key="error.password.required" />
<msg name="maxlength" key="error.password.length" />
<msg name="minlength" key="error.password.length" />
<msg name="mask" key="error.password.invalid" />

<arg name="maxlength" key="${var:minlength}" position="0" resource="false"/>
<arg name="maxlength" key="${var:maxlength}" position="1" resource="false"/>

<arg name="minlength" key="${var:minlength}" position="0" resource="false"/>
<arg name="minlength" key="${var:maxlength}" position="1" resource="false"/>

<arg key="UserValidationForm.password"/>

<var>
<var-name>minlength</var-name>
<var-value>7</var-value>
</var>
<var>
<var-name>maxlength</var-name>
<var-value>20</var-value>
</var>

<var>
<var-name>mask</var-name>
<var-value>^[a-zA-Z0-9]*$</var-value>
</var>

</field>

<field property="retypepassword" depends="validwhen">

<msg name="validwhen" key="error.retypepassword.notmatch" />

<arg key="UserValidationForm.retypepassword"/>

<var>
<var-name>test</var-name>
<var-value>
(password == *this*)
</var-value>
</var>
</field>


<field property="emailaddress" depends="required,email">

<msg name="required" key="error.emailaddress.required" />
<msg name="email" key="error.emailaddress.invalid" />

<arg key="UserValidationForm.password"/>

</field>


</form>
</formset>
</form-validation>


validation entry in struts-config.xml

<message-resources parameter="com.yashdc.strutsexample.resources.ApplicationResources" key="StrutsExample"/>

<plug-in className="org.apache.struts.validator.ValidatorPlugIn" >
<set-property property="pathnames"
value="/WEB-INF/validator-rules.xml, /WEB-INF/validator-user.xml"/>
</plug-in>

Form Class which contain validate method this validation are running but not the validation-user.xml

public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
ActionErrors errors = new ActionErrors();

if (loginid == null || loginid.trim().equals(""))
{
errors.add("loginid", new ActionError("error.loginid.required"));
}
if (password == null || password.trim().equals(""))
{
errors.add("password", new ActionError("error.password.required"));
}
if (retypepassword == null || retypepassword.trim().equals(""))
{
errors.add("retypepassword", new ActionError("error.retypepassword.required"));
}
}
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Please UseCodeTags when posting code or configuration. Unformatted code and configuration is unnecessarily difficult to read. You can edit your post by using the button.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

If you write your own validate() method you must still call the super-class's validate() to run the default functionality.
 
wood burning stoves
 
subject: custom validator and custom tags