Announcement Announcement Module
Collapse
No announcement yet.
Select with null option Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Select with null option

    Dear all,

    I have a hard time getting a select list, which is optional, to work correctly.

    If I remove the .ttlId from the path, the current title is not selected
    (none of the options is selected in the select list).

    The below code is the best I could figure out. The only problem with it is if you select the empty option. I get an 'Invalid type' (ongeldig type in Dutch) in the page, with the following complete error message:

    Code:
    Field error in object 'contactpersoonInput' on field 'titelByCpnBeforeTteId.ttlId': rejected value []; codes [typeMismatch.contactpersoonInput.titelByCpnBeforeTteId.ttlId,typeMismatch.titelByCpnBeforeTteId.ttlId,typeMismatch.ttlId,typeMismatch.long,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [contactpersoonInput.titelByCpnBeforeTteId.ttlId,titelByCpnBeforeTteId.ttlId]; arguments []; default message [titelByCpnBeforeTteId.ttlId]]; default message [Failed to convert property value of type [java.lang.String] to required type [long] for property 'titelByCpnBeforeTteId.ttlId'; nested exception is java.lang.NumberFormatException: For input string: ""]

    View:
    Code:
    <form:select cssClass="cpn" path="titelByCpnBeforeTteId.ttlId">
      <form:option value=""></form:option>
      <form:options items="${titelsVoor}" 
        itemValue="ttlId" itemLabel="ttlDescription"/>
    </form:select>
    SimpleFormController:
    Code:
    	/**
    	 * Registering several custom property editors
    	 */
    	@Override
    	protected void initBinder(HttpServletRequest request,
    			ServletRequestDataBinder binder) throws Exception {
    		binder.registerCustomEditor(Titel.class, 
    				"titelByCpnBeforeTteId",
    				new NullableTitelEditor());
    	}
    
    	/** Custom property editor for optional Titel fields */
    	class NullableTitelEditor extends PropertyEditorSupport {
    		@Override
    		public String getAsText() {
    			if (getValue() == null) {
    				log.debug("Titel: getAsText: title is null.");
    				return "";
    			}
    			Titel titel = (Titel) getValue();
    			log.debug("Titel: getAsText: title is " + titel.getTtlId() + " : " + titel.getTtlDescription());
    			return "" + titel.getTtlId();
    		}
    
    		@Override
    		public void setAsText(String text) throws IllegalArgumentException {
    			Titel titel;
    			if ("".equals(text)) {
    				titel = null;
    			} else {
    				titel = generalService.retrieveTitle(Long
    						.parseLong(text));
    			}
    			log.debug("Titel: setValue(" + (titel == null ? null : titel.getTtlId()) + " : " + 
    					(titel == null ? null : titel.getTtlDescription()) + ")");
    			setValue(titel);
    		}
    	}

  • #2
    You are binding a String to a long value and register a TitlePropertyEditor. So what is it you want? Bind to the title not the id of the title.

    Comment


    • #3
      Dear Marten,

      Thank you for your reply.
      Your suggestion is to use the below <form:select> right?

      I have also tried that. And it seems values are saved to the database
      correctly. Both if I select the empty option and if I select a non-empty
      option.
      However, if I go to the same person again, the database value
      is not selected correctly. None of the select options is selected (and therefore, the first option becomes the selected option).

      That is why I tried to append .ttlId to the path. That makes the right option selected, but runs into the 'invalid type' problem.
      I guess you're right about the path property, but do you also know how to get the right option selected now?

      Thanks very much for your time,
      Jonne.

      Code:
      <form:select cssClass="cpn" path="titelByCpnBeforeTteId">
        <form:option value=""></form:option>
        <form:options items="${titelsVoor}" 
          itemValue="ttlId" itemLabel="ttlDescription"/>
      </form:select>

      Comment


      • #4
        With everything else in place, the below <select> tag does work
        for me. It correctly selects the title initially, and it can save any title,
        including the null value.

        This solves it for me, but if anybody could explain it to me I would still be gratefull.

        Jonne.

        Code:
        				<spring:bind path="titelByCpnBeforeTteId">
        					<select class="cpn" name="${status.expression}">
        						<option value=""></option>
        						<c:forEach var="titel" items="${titelsVoor}">
        							<c:choose>
        								<c:when test="${titel.ttlId == contactpersoonInput.titelByCpnBeforeTteId.ttlId}">
        									<option selected="selected" value="${titel.ttlId}">${titel.ttlDescription}</option>
        								</c:when>
        								<c:otherwise>
        									<option value="${titel.ttlId}">${titel.ttlDescription}</option>
        								</c:otherwise>
        							</c:choose>
        						</c:forEach>
        					</select>
        				</spring:bind>

        Comment

        Working...
        X