Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java

Sławomir Cieślar
PLUS
Sławomir Cieślar
Courses Plus Student 110 Points

How to validate if the view contains two forms

Hello, I have two forms in the view that add product and add category. When I give incorrect data in the form(validation), the category returns me error Error during execution of processor 'org.thymeleaf.spring4.processor.attr.SpringInputGeneralFieldAttrProcessor' (addProduct:20) Why spring also validates the product of forms.

Controler:

@PostMapping("categories")
    public String add(@Valid  Category category, BindingResult result, Model model,RedirectAttributes redirectAttrs) {
        if (result.hasErrors()) {
            return "addProduct";
        } else {
            categoryRepository.save(category);
            try {
                fileSave.save(category.getImage().getBytes(), category.getName() + ".png");
            } catch (IOException e) {
                e.printStackTrace();
            }
            return "redirect:product/add";
        }

@PostMapping("/add")
    public String add(
            Model model,
            @Valid Product product,
            BindingResult result){
        if (result.hasErrors()) {
            return "addProduct";
        } else {
            ProductDto productDto = mapper.mapToProductDto(product);
            productService.add(productDto);
            return "redirect:productList";
        }

View:

<form class="form-horizontal" th:action="@{/product/add}" method="post" enctype="multipart/form-data"
                  th:object="${product}">
                <fieldset>
                    <legend class="center-block">Dodaj nowy produkt</legend>
                    <div class="form-group">
                        <label class="col-2" for="name">Nazwa</label>
                        <input class="col-8 form-control" type="text" id="name" required="required"
                               placeholder="nazwa" th:field="*{name}"/>
                        <span th:if="${#fields.hasErrors('name')}" th:errors="*{name}"></span>

                    </div>

                    <div class="form-group">
                        <label class="col-2" for="price">Cena</label>
                        <input type="number" class="form-control col-8" id="price" th:field="*{price}"
                               required="required"
                               placeholder="cena"/>
                        <span class="col-1 input-group-addon"></span>
                        <span th:if="${#fields.hasErrors('price')}" th:errors="*{price}"></span>
                    </div>
                    <div class="form-group">
                        <label class="col-2" for="weight">Waga</label>
                        <input type="number" class="form-control col-8" id="weight" th:field="*{shippingWeight}"
                               required="required"
                               placeholder="waga"/>
                        <span class="col-1 input-group-addon">dag</span>
                        <span th:if="${#fields.hasErrors('shippingWeight')}" th:errors="*{shippingWeight}"></span>
                    </div>
                    <div class="form-group">
                        <label class="col-2" for="description">Opis</label>
                        <textarea type="text" class="col-8 form-control " id="description" th:field="*{description}"
                                  placeholder="opis">Opis produktu </textarea>
                        <span th:if="${#fields.hasErrors('description')}" th:errors="*{descriptiont}"></span>
                    </div>

                    <div class="form-group">
                        <label class="col-2" for="image">Obrazek</label>
                        <input type="file" class="col-8 form-control" id="image" th:field="*{image}"

                        />
                    </div>
                    <div class="form-group">
                        <label class="col-2" for="category">Kategoria</label>
                        <select id="category" th:field="*{categories}" class="form-control col-8">
                            <option th:each="category: ${categoryList}" th:value="${category.id}"
                                    th:text="${category.name}"></option>
                        </select>
                    </div>

                    <button type="submit" class="btn btn-primary">Dodaj product</button>
                    <a class="btn btn-danger" th:href="@{/}">Cancel</a>
                </fieldset>
            </form>
 <fieldset>
                <legend>Dodaj nową categorię</legend>

                <form class="horizontal-form" method="post" th:action="@{/categories}" enctype="multipart/form-data"
                      th:object="${category}">
                    <div class="from-group">
                        <label class="col-2" for="nameCategory">
                            Nazwa
                        </label>
                        <span th:if="${#fields.hasAnyErrors()}"></span>
                        <input class="col-8 form-control" type="text" id="nameCategory" required="required"
                               placeholder="nazwa" th:field="*{name}"/>
                        <span th:if="${#fields.hasErrors('name')}" th:errors="*{name}"></span>
                    </div>

                    <div class="form-group">
                        <label class="col-2" for="description">Opis</label>
                        <textarea type="text" class="col-8 form-control " id="descriptionCetegory" th:field="*{description}"
                                  placeholder="opis">Opis produktu </textarea>
                        <span th:if="${#fields.hasErrors('description')}" th:errors="*{description}"></span>
                    </div>

                    <div class="form-group">
                        <label class="col-2" for="image">Obrazek</label>
                        <input type="file" class="col-8 form-control" id="image" th:field="*{image}"
                        />
                    </div>
                    <button type="submit" class="btn btn-primary">Dodaj Kategorię</button>