Re: Custom validation with Struts2
> > > > validate="true"> > > > > > > label="%{getText('card_add_edit_field')}"> > > > > value="%{EmpNo}"> > > > > value="%{true}"> > > > > value="%{true}"> > > > > value="%{getText('button_submit')}"/> > > > > > > > > > > > > > > > > JAVA Action: > > > > > > > > public void validate() { > > > > request = > ServletActionContext.getRequest(); > > > > String tmp = ""; > > > > try { > > > > tmp = > request.getParameter("cardNo"); > > > > Integer i = > this.getCardNo(); > > > > if (tmp != null > && tmp.length() > 0) { > > > > int > tmpInt = Integer.parseInt(tmp); > > > > > this.setCardNo(tmpInt); > > > > > System.out.println("Card no (validation) = " + > > cardNo); > > > > if > (cardNo < 1 || cardNo > 99) { > > > > > > > > > > addFieldError("cardNo", "Must be int"); > > > > > > > > > > > > > > > > System.out.println("INPUT..."); > > > > } > > > > } > > > > } catch (Exception e) { > > > > > > > > > e.printStackTrace(); > > > > } > > > > > > > > } > > > > > > > > Thanx a lot. > > > > > > > > Best regards, Filippov Andrey > > > > > > > > -- > View this message in context: > http://www.nabble.com/Custom-validation-with-Struts2-tp16115973p19425117.html > Sent from the Struts - User mailing list archive at > Nabble.com. > > > - > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: > [EMAIL PROTECTED] - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Custom validation with Struts2
If Integer Range Validator does not work for you then you shoud consider regular expression validation for same, its can be done by restricting number of digits and allowing only numeric characters. Here is an example of similar problem http://struts-2-developers.blogspot.com/2008/08/struts-2-integer-validation-example.html Struts 2 Integer Validation fil78 wrote: > > Hello everybody, > > > > I need to validate only 1 field on the jsp. It value should be between 1 > and 99. Initially I started to implement through int validation but it > seemed to go wrong - it works with some limitation - maximum value should > not exceed 1000 or something like that. I am trying to implement > validation through overriding validate() method. I use tiles together with > Struts 2.0.11. I could not tune it to work correctly. Could someone help, > please? > > > > Here are my snippets: > > > > > > Struts.xml: > > > > > class="com.tsystems.tintra4.actions.EmployeeCard"> > > > > > > WTT_CARDS_EDITOR > > > > > > > > name="SUCCESS">def_page_employee_card > > > > def_page_employee_card > > > > > > > > > > Jsp: > > > > <%@ page language="java" contentType="text/html; charset=UTF-8" > > pageEncoding="UTF-8"%> > > <%@ taglib prefix="s" uri="/struts-tags" %> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > label="%{getText('card_add_edit_field')}"> > > > > > > > > > > > > > > > > > > > > > > > > > > > > label="%{getText('card_add_edit_field')}"> > > > > > > > > > > > > > > > > JAVA Action: > > > > public void validate() { > > request = ServletActionContext.getRequest(); > > String tmp = ""; > > try { > > tmp = request.getParameter("cardNo"); > > Integer i = this.getCardNo(); > > if (tmp != null && tmp.length() > 0) { > > int tmpInt = Integer.parseInt(tmp); > > this.setCardNo(tmpInt); > > System.out.println("Card no (validation) = " + > cardNo); > > if (cardNo < 1 || cardNo > 99) { > > > > addFieldError("cardNo", "Must be int"); > > > > > > System.out.println("INPUT..."); > > } > > } > > } catch (Exception e) { > > > > e.printStackTrace(); > > } > > > > } > > > > Thanx a lot. > > > > Best regards, Filippov Andrey > > > -- View this message in context: http://www.nabble.com/Custom-validation-with-Struts2-tp16115973p19425117.html Sent from the Struts - User mailing list archive at Nabble.com. - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Custom validation with Struts2
Lukasz Lenart wrote: I'm not sure were is the problem but I've just checked such simple example below and everything is ok. Maybe you should consider to use Long instead of Integer, because when you enter more then 2147483647 to the field you will get some strange value back to the input (because you loose precision ;-) class EmployeeAction extends ActionSupport { private Integer cardNo; // getter & setter public void validate() { if (cardNo == null) { addFieldError("cardNo", "Should be integer!"); } else if ( cardNo < 1 || cardNo > 99) { addFieldError("cardNo", "Should be integer!"); } } } JSP Regards You may also want to consider declarative validation [1], either using an XML file or annotations. Struts has built-in support for validating an integer input is in a given range [2]. As to the problem of getCardNo() always returning 0, that shouldn't happen; you may have a conversion error happening, or perhaps something else is mis-configured somewhere. Struts should call setCardNo() for you to pass the form data in. L. [1] http://struts.apache.org/2.0.11/docs/validation.html [2] http://struts.apache.org/2.0.11/docs/int-validator.html - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Custom validation with Struts2
I'm not sure were is the problem but I've just checked such simple example below and everything is ok. Maybe you should consider to use Long instead of Integer, because when you enter more then 2147483647 to the field you will get some strange value back to the input (because you loose precision ;-) class EmployeeAction extends ActionSupport { private Integer cardNo; // getter & setter public void validate() { if (cardNo == null) { addFieldError("cardNo", "Should be integer!"); } else if ( cardNo < 1 || cardNo > 99) { addFieldError("cardNo", "Should be integer!"); } } } JSP Regards -- Lukasz http://www.linkedin.com/in/lukaszlenart - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: Custom validation with Struts2
ng(EmpNo)); cardN = String.valueOf(card.getId()); cardNo = Integer.parseInt(String.valueOf(card.getId()).trim()); } if (addCard == true) { cardN = ""; } return "SUCCESS"; } public String getEmpNo() { return EmpNo; } public void setEmpNo(String empNo) { EmpNo = empNo; } public String getFio() { return fio; } public void setFio(String fio) { this.fio = fio; } public String getActionName() { return actionName; } public void setActionName(String actionName) { this.actionName = actionName; } public void setServletRequest(HttpServletRequest request) { // TODO Auto-generated method stub this.request = request; } public ArrayList getMenu_items() { return menu_items; } public void setMenu_items(ArrayList menu_items) { this.menu_items = menu_items; } public ArrayList getPage_menu_items() { return page_menu_items; } public void setPage_menu_items(ArrayList page_menu_items) { this.page_menu_items = page_menu_items; } public ArrayList getPage_menu_manager_items() { return page_menu_manager_items; } public void setPage_menu_manager_items( ArrayList page_menu_manager_items) { this.page_menu_manager_items = page_menu_manager_items; } public CardsVO getCard() { return card; } public void setCard(CardsVO card) { this.card = card; } public HttpServletResponse getResponse() { return response; } public void setResponse(HttpServletResponse response) { this.response = response; } public int getFieldName() { return fieldName; } public void setFieldName(int fieldName) { this.fieldName = fieldName; } public String getCardN() { return cardN; } public void setCardN(String cardN) { this.cardN = cardN; } } -Original Message- From: Lukasz Lenart [mailto:[EMAIL PROTECTED] Sent: Tuesday, March 18, 2008 11:09 AM To: Struts Users Mailing List Subject: Re: Custom validation with Struts2 > request = ServletActionContext.getRequest(); > String tmp = ""; > try { >tmp = request.getParameter("cardNo"); >Integer i = this.getCardNo(); >if (tmp != null && tmp.length() > 0) { > int tmpInt = Integer.parseInt(tmp); Remove this part above > this.setCardNo(tmpInt); You don't have to do this, it will be done by Struts2 > System.out.println("Card no (validation) = " + cardNo); > if (cardNo < 1 || cardNo > 99) { > addFieldError("cardNo", "Must be int"); > System.out.println("INPUT..."); > } That's all you have to have in your validate() method (if cardNo has getter and setter defined in action class) Remove also validate=true from your form, because you don't use client side validation. Regards -- Lukasz http://www.linkedin.com/in/lukaszlenart - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Custom validation with Struts2
> request = ServletActionContext.getRequest(); > String tmp = ""; > try { >tmp = request.getParameter("cardNo"); >Integer i = this.getCardNo(); >if (tmp != null && tmp.length() > 0) { > int tmpInt = Integer.parseInt(tmp); Remove this part above > this.setCardNo(tmpInt); You don't have to do this, it will be done by Struts2 > System.out.println("Card no (validation) = " + > cardNo); > if (cardNo < 1 || cardNo > 99) { > addFieldError("cardNo", "Must be int"); > System.out.println("INPUT..."); > } That's all you have to have in your validate() method (if cardNo has getter and setter defined in action class) Remove also validate=true from your form, because you don't use client side validation. Regards -- Lukasz http://www.linkedin.com/in/lukaszlenart - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Custom validation with Struts2
Hello everybody, I need to validate only 1 field on the jsp. It value should be between 1 and 99. Initially I started to implement through int validation but it seemed to go wrong - it works with some limitation - maximum value should not exceed 1000 or something like that. I am trying to implement validation through overriding validate() method. I use tiles together with Struts 2.0.11. I could not tune it to work correctly. Could someone help, please? Here are my snippets: Struts.xml: WTT_CARDS_EDITOR def_page_employee_card def_page_employee_card Jsp: <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags" %> JAVA Action: public void validate() { request = ServletActionContext.getRequest(); String tmp = ""; try { tmp = request.getParameter("cardNo"); Integer i = this.getCardNo(); if (tmp != null && tmp.length() > 0) { int tmpInt = Integer.parseInt(tmp); this.setCardNo(tmpInt); System.out.println("Card no (validation) = " + cardNo); if (cardNo < 1 || cardNo > 99) { addFieldError("cardNo", "Must be int"); System.out.println("INPUT..."); } } } catch (Exception e) { e.printStackTrace(); } } Thanx a lot. Best regards, Filippov Andrey