Passing unused fields from form

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Passing unused fields from form

masterkwi
 I would like to get unused properties back after posting form, how to get field orderIngredients binded?

      @GetMapping("/new")
public String newIngredientForm(Model model){

    Ingredient ingredient=new Ingredient();
    OrderIngredient orderIngredient=new OrderIngredient();
    orderIngredient.setQuantity(21);
ingredient.getOrderIngredients().add(orderIngredient);
    model.addAttribute("ingredient",ingredient);

        return "ingredients/ingredientForm";
    }
I've tried to pass this field orderIngredient to form and hide it, but it didn't work

<form th:object="${ingredient}" th:action="@{'/ingredients/new'}" method="post">
    <fieldset>
            <input type="hidden" th:field="*{orderIngredients}"/>


field of ingredient class: private Set<OrderIngredient> orderIngredients= new HashSet<>();


There was an unexpected error (type=Bad Request, status=400).
Validation failed for object='ingredient'. Error count: 1
org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 1 errors
Field error in object 'ingredient' on field 'orderIngredients': rejected value [null]; codes [typeMismatch.ingredient.orderIngredients,typeMismatch.orderIngredients,typeMismatch.java.util.Set,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [ingredient.orderIngredients,orderIngredients]; arguments []; default message [orderIngredients]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Set' for property 'orderIngredients'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [java.lang.Long] for value 'null'; nested exception is java.lang.NumberFormatException: For input string: "null"]


How could I pass object containing Set<> attribute , so I could edit this Set, and how could I pass attributes unhindered when I don't want to edit them but just send them via form