Back Forum Reply New

Short and BigDecimal not required

Hi.  I'm new to Spring so please bare with me.  I have a bean that is setup as follows:Code:

public SomeClass implements java.io.Serializable {   private String name;   private Short val;   private BigDecimal bigDecVal;

   public String getName() {       return name;   }
   public Short getVal()   {       return val;   }
   public BigDecimal getBigDecVal()   {       return bigDecVal;   }
   /**    * @spring.validator type=quot;requiredquot;    */   public void setName(String name)   {       this.name = name;   }
   /**    * @spring.validator type=quot;requiredquot;    */   public void setVal(Short val)   {       this.val = val;   }   
   /**    * @spring.validator type=quot;requiredquot;    */   public void setBigDecVal(BigDecimal bigDecVal)   {       this.bigDecVal = bigDecVal;   }

}
The controler is as follows:Code:
public class MyFormController extends BaseFormController {   public MyFormController() {       setCommandName(quot;someClassquot;);       setCommandClass(SomeClass.class);   }
   public ModelAndView onSubmit(fromServletRequest request,                     fromServletResponse response, Object command,                     BindException errors)   throws Exception {       // ... do something and insert into db       return new ModelAndView(getSuccessView());   }
}
and my jsp that submits to the controler:Code:

lt;!-- bunch of stuff - MyForm is mapped to the controler --gt;

lt;form name=quot;someEditquot; action=quot;MyForm.htmlquot;gt;
lt;spring:bind path=quot;someClass.*quot;gt;   lt;c:if test=quot;${not empty status.errorMessages}quot;gt;   lt;div class=quot;errorquot;gt;           lt;c:forEach var=quot;errorquot; items=quot;${status.errorMessages}quot;gt;lt;ing src=quot;lt;c:ucl value=quot;/images/iconWarning.gifquot;/gt;quot;    alt=quot;lt;fmt:message key=quot;icon.warningquot;/gt;quot; class=quot;iconquot; /gt;lt;cut value=quot;${error}quot; escapeXml=quot;falsequot;/gt;lt;brgt;       lt;/c:forEachgt;   lt;/divgt;   lt;/c:ifgt;
lt;/spring:bindgt;
lt;ligt;       lt;spring:bind path=quot;someClass.namequot;gt;lt;span class=quot;fieldErrorquot;gt;lt;cut value=quot;${status.errorMessage}quot;/gt;lt;/spangt;lt;input type=quot;textquot; name=quot;lt;cut value=quot;${status.expression}quot;/gt;quot; id=quot;lt;cut value=quot;${status.expression}quot;/gt;quot; value=quot;lt;cut value=quot;${status.value}quot;/gt;quot; class=quot;text mediumquot;/gt;       lt;/spring:bindgt;   lt;/ligt;lt;ligt;       lt;spring:bind path=quot;someClass.valquot;gt;lt;span class=quot;fieldErrorquot;gt;lt;cut value=quot;${status.errorMessage}quot;/gt;lt;/spangt;lt;input type=quot;textquot; name=quot;lt;cut value=quot;${status.expression}quot;/gt;quot; id=quot;lt;cut value=quot;${status.expression}quot;/gt;quot; value=quot;lt;cut value=quot;${status.value}quot;/gt;quot; class=quot;text mediumquot;/gt;       lt;/spring:bindgt;   lt;/ligt;
       lt;spring:bind path=quot;someClass.bigDecValquot;gt;lt;span class=quot;fieldErrorquot;gt;lt;cut value=quot;${status.errorMessage}quot;/gt;lt;/spangt;lt;input type=quot;textquot; name=quot;lt;c:out value=quot;${status.expression}quot;/gt;quot; id=quot;lt;c:out value=quot;${status.expression}quot;/gt;quot; value=quot;lt;c:out value=quot;${status.value}quot;/gt;quot; class=quot;text mediumquot;/gt;       lt;/spring:bindgt;   lt;/ligt;

lt;/formgt;
My problem is... If I leave the name blank, it comes back with an error (as it is supposed to), but if I leave the val or the bigDecVal blank, I don't receive an error.  I have all fields marked as required in my bean.  Is there something else I have to do to make number objects required?

Please help.

Do I need something like the following in my applicationContext-validation.xml file?Code:         lt;bean id=quot;customEditorConfigurerquot; class=quot;org..beans.factory.config.CustomEditorConfigurerquot;gt; lt;property name=quot;customEditorsquot;gt; lt;mapgt;   lt;entry key=quot;java.math.BigDecimalquot;gt;   lt;bean class=quot;org..beans.propertyeditors.CustomNumberEditorquot;gt;   lt;constructor-arg type=quot;java.lang.Classquot;gt;lt;valuegt;java.math.BigDecimallt;/valuegt;lt;/constructor-arggt;   lt;constructor-arg type=quot;booleanquot;gt;lt;valuegt;truelt;/valuegt;lt;/constructor-arggt;   lt;/beangt;   lt;/entrygt;      lt;/mapgt; lt;/propertygt; lt;/beangt;
I already tried the above, and it didn't work.

What I ended up doing is taking the @spring.validator type=quot;requiredquot; stuff out of my bean and stuck it in my validation-global.xml file.  Like this:Code:
lt;formsetgt;       lt;form name=quot;someEditquot;gt;              lt;field property=quot;namequot; depends=quot;requiredquot;gt;       lt;arg0 key=quot;someClass.namequot;/gt;       lt;/fieldgt;       lt;field property=quot;valquot; depends=quot;requiredquot;gt;       lt;arg0 key=quot;someClass.valquot;/gt;       lt;/fieldgt;       lt;field property=quot;bigDecValquot; depends=quot;requiredquot;gt;       lt;arg0 key=quot;someClass.bigDecValquot;/gt;       lt;/fieldgt;              lt;/formgt;   lt;/formsetgt;
I'm sure there's a way to use @spring.validator type=quot;requiredquot; within my bean, but the above worked.
¥
Back Forum Reply New