Using Enum with Select control

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Using Enum with Select control


----------------------------------------------------

My Todo object has field:

  private Status status;

with standard getters and setters.

----------------------------------------------------

The HTML renders fine, the right item gets selected from the list but when I activate the form, the selected item cannot get mapped into the Todo object:

Endpoint:

  @RequestMapping(path = "/todo/{id}/edit", method = RequestMethod.POST)
  public String updateTodo(@PathVariable Long id, @ModelAttribute Todo todo) {


----------------------------------------------------

Error:

There was an unexpected error (type=Bad Request, status=400).
Validation failed for object='todoDTO'. Error count: 1
org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 1 errors
Field error in object 'todoDTO' on field 'status': rejected value [On hold]; codes [typeMismatch.todoDTO.status,typeMismatch.status,typeMismatch.com.task.todo.enums.Status,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [todoDTO.status,status]; arguments []; default message [status]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'com.task.todo.enums.Status' for property 'status'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [com.task.todo.enums.Status] for value 'On hold'; nested exception is java.lang.IllegalArgumentException: No enum constant com.task.todo.enums.Status.On hold]


Can someone help me how to solve this problem?

Riel
I am trying to create a Select control which works with Enum values.

My enum looks like:

----------------------------------------------------

public enum Status {
  NOT_STARTED("Not started"),
  PROGRESS("In progress"),
  ON_HOLD("On hold"),
  BLOCKED("Blocked"),
  FINISHED("Done");

  private final String displayName;

  Status(final String displayName) {
    this.displayName = displayName;
  }

  @Override
  public String toString() {
    return this.displayName;
  }

  //region Reverse lookup
  private static final Map<String, Status> lookup = new HashMap<>();

  static {
    for (Status status : Status.values()) {
      lookup.put(status.displayName, status);
    }
  }

  public static Status getByDisplayName(String displayName) {
    return lookup.get(displayName);
  }
  //endregion
}

----------------------------------------------------

In my controller I use:

model.addAttribute("statuses", Status.values());

----------------------------------------------------

HTML:

           
<select name="status">
            <option th:each="status : ${statuses}"
                    th:value="${status}"
                    th:text="${status}"
                    th:selected="${todo.getStatus().equals(status)}">
            </option>
            </select>
Reply | Threaded
Open this post in threaded view
|

Re: Using Enum with Select control

Riel
The Enum gets mapped well when the displayed value is the same like the Enum value:

public enum Status {
  ON_HOLD("ON_HOLD"),