wood burning stoves 2.0*
The moose likes Struts and the fly likes struts validation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "struts validation" Watch "struts validation" New topic
Author

struts validation

kaushik saha
Greenhorn

Joined: Sep 22, 2005
Posts: 18
how can i validate this type field using xml validation in struts2

the format is NN-NNNNNNN

N is no from (0->9) must be 9 digits in all


Nishan Patel
Ranch Hand

Joined: Sep 07, 2008
Posts: 688


Hi,

for pattern match you have to use regex fpr validation.


like :


<field name="XYZ">
<field-validator type="regex">
<param name="expression">[0-9]-[0-9]</param>
<message>The value of bar2 must be in the format "x, y", where x and y are between 0 and 9</message>
</field-validator>
</field>


For better ref :- http://struts.apache.org/2.x/docs/validation.html#Validation-DefiningValidationRules




Thanks, Nishan Patel
SCJP 1.5, SCWCD 1.5, OCPJWSD Java Developer,My Blog
kaushik saha
Greenhorn

Joined: Sep 22, 2005
Posts: 18
where i place this <actionclassname>-validation.xml file
Nishan Patel
Ranch Hand

Joined: Sep 07, 2008
Posts: 688


Hi,

With the package contains your action class.

Like If your Action Abc.java in your com.xyz.action package then your Abc-validation.xml also in your com.xyz.action package.


kaushik saha
Greenhorn

Joined: Sep 22, 2005
Posts: 18
<!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0.2//EN"
"http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">

<validators>
<field name="taxId">
<field-validator type="regex">
<param name="expression">[0-9][0-9]-[0-9][0-9][0-9][0-9][0-9][0-9][0-9]</param>
<message>The value of Tax Id must be in the format "NN-NNNNNNN", where N is number between 0 and 9</message>
</field-validator>
</field>
</validators>

put it in the WEB-INF/classes/pkgname/action/<actionclass name>-validation.xml

and in the jsp page in the form tag write validate="true" ...but still the validation is not working ....

Other than these what i have to do???Please reply me..
Nishan Patel
Ranch Hand

Joined: Sep 07, 2008
Posts: 688


Hi,

You don't have to do any thing just make sure your url Action name.

When it called your action called that class file.

It automatic called validation when Struts get xml of same as your action class.

before check your struts xml mapping.

if you still not get put your URL , jsp and struts xml and action class.


kaushik saha
Greenhorn

Joined: Sep 22, 2005
Posts: 18
action class

public String execute() throws Exception {

System.out.println("I am inside execute method of customerMaster update ");
UserLogSetting lobj_ULSetting =(UserLogSetting)session.get("USER_LOG");


customerData= salesManager.getCustomerDetail( customerData,lobj_ULSetting);

System.out.println("I am inside execute method of after customer update");
return SUCCESS;
}

jsp page



<s:form action="updateCustomer" id="custDetailFrm" method="POST" theme="simple" namespace="/salesmgmt" target='_self' validate="true">


strutsxml

<action name="updateCustomer" class="com.hrms.salesmgmt.action.UpdateCustomer">
<result name="input">/salesmgmt/customerDetails.jsp</result>
<result >/salesmgmt/customerDetails.jsp</result>
</action>
Nishan Patel
Ranch Hand

Joined: Sep 07, 2008
Posts: 688

Hi,


Just remove target='_self' validate="true" form form tag.

And what is your validation xml name ???..


And make sure it is at com.hrms.salesmgmt.action.UpdateCustomer package.....



kaushik saha
Greenhorn

Joined: Sep 22, 2005
Posts: 18
sorry it is still not working..
Amit Ghorpade
Bartender

Joined: Jun 06, 2007
Posts: 2716
    
    6

"srk " please check your private messages for an important administrative matter. You can check them by clicking the My Private Messages link above.


SCJP, SCWCD.
|Asking Good Questions|
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: struts validation