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: action name=EmployeeCard class=com.tsystems.tintra4.actions.EmployeeCard interceptor-ref name=i18n / interceptor-ref name=roles WTT_CARDS_EDITOR /interceptor-ref !-- interceptor-ref name=workflow/-- interceptor-ref name=workflow/ result type=tiles name=SUCCESSdef_page_employee_card/result result type=tiles name=input def_page_employee_card /result /action Jsp: %@ page language=java contentType=text/html; charset=UTF-8 pageEncoding=UTF-8% %@ taglib prefix=s uri=/struts-tags % !--Reference to the Employee profile-- div s:url id=url_fio action=GetEmployee includeParams=none s:param name=employeeIds:property value=%{EmpNo}//s:param /s:url s:a href=%{url_fio} s:property value=%{fio}/ /s:a /div br/ !--Adding new card-- s:if test=addCard == true s:text name=card_adding_proccess/s:text s:fielderror/ s:form method=GET validate=true s:textfield name=cardNo label=%{getText('card_add_edit_field')}/s:textfield s:textfield name=EmpNo value=%{EmpNo}/s:textfield s:hidden name=editCard value=%{true}/s:hidden s:hidden name=addCardFinal value=%{true}/s:hidden s:submit value=%{getText('button_submit')}/ /s:form /s:if !--Editing new card-- s:if test=editCard == true s:text name=card_editing_proccess/s:text s:fielderror/ s:form method=GET validate=true s:textfield name=cardNo label=%{getText('card_add_edit_field')}/s:textfield s:hidden name=EmpNo value=%{EmpNo}/s:hidden s:hidden name=editCard value=%{true}/s:hidden s:hidden name=editCardFinal value=%{true}/s:hidden s:submit value=%{getText('button_submit')}/ /s:form /s:if 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
); 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]
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: action name=EmployeeCard class=com.tsystems.tintra4.actions.EmployeeCard interceptor-ref name=i18n / interceptor-ref name=roles param name=allowedRolesWTT_CARDS_EDITOR/param /interceptor-ref !-- interceptor-ref name=workflow/-- interceptor-ref name=workflow/ result type=tiles name=SUCCESSdef_page_employee_card/result result type=tiles name=input def_page_employee_card /result /action Jsp: %@ page language=java contentType=text/html; charset=UTF-8 pageEncoding=UTF-8% %@ taglib prefix=s uri=/struts-tags % !--Reference to the Employee profile-- div s:url id=url_fio action=GetEmployee includeParams=none s:param name=employeeIds:property value=%{EmpNo}//s:param /s:url s:a href=%{url_fio} s:property value=%{fio}/ /s:a /div br/ !--Adding new card-- s:if test=addCard == true s:text name=card_adding_proccess/s:text s:fielderror/ s:form method=GET validate=true s:textfield name=cardNo label=%{getText('card_add_edit_field')}/s:textfield s:textfield name=EmpNo value=%{EmpNo}/s:textfield s:hidden name=editCard value=%{true}/s:hidden s:hidden name=addCardFinal value=%{true}/s:hidden s:submit value=%{getText('button_submit')}/ /s:form /s:if !--Editing new card-- s:if test=editCard == true s:text name=card_editing_proccess/s:text s:fielderror/ s:form method=GET validate=true s:textfield name=cardNo label=%{getText('card_add_edit_field')}/s:textfield s:hidden name=EmpNo value=%{EmpNo}/s:hidden s:hidden name=editCard value=%{true}/s:hidden s:hidden name=editCardFinal value=%{true}/s:hidden s:submit value=%{getText('button_submit')}/ /s:form /s:if 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
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]
RE: Custom validation with Struts2
) { 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 ArrayListMenu getMenu_items() { return menu_items; } public void setMenu_items(ArrayListMenu menu_items) { this.menu_items = menu_items; } public ArrayListMenu getPage_menu_items() { return page_menu_items; } public void setPage_menu_items(ArrayListMenu page_menu_items) { this.page_menu_items = page_menu_items; } public ArrayListMenu getPage_menu_manager_items() { return page_menu_manager_items; } public void setPage_menu_manager_items( ArrayListMenu 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
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 s:textfield label=Card Number name=cardNo / 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
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 s:textfield label=Card Number name=cardNo / 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]