2

I have this input:

Masa: <input type="number"  class="form-control form-text"   name="masa"/>
    <div class="text col-sm-12 error" th:if="${wzrost}" >
        <p class="text text-center">
            To pole jest wymagane
        </p>
    </div>
    Wzrost: <input type="number" class="form-control form-text "   name="wzrost"/>
    <div class="text col-sm-12 error" th:if="${wzrost}" >
        <p class="text text-center">
            To pole jest wymagane
        </p>
    </div>

And this controller;

String x = String.valueOf(masa);
        String y = String.valueOf(wzrost);


        if(x==null ){
        model.addAttribute("wzrost",true);
        return"views/success";
    }
        if(y==null ){
            model.addAttribute("wzrost",true);
            return"views/success";
        }

When I click form submit button I always get error nullpointerexception.

How do I validate input, so that when it is empty the message pops up

1

2 Answers 2

0
@PostMapping("/cal-bmi")
public String calculateBmiForm(Model model, Integer masa, Integer wzrost) {

String x = String.valueOf(masa);
    String y = String.valueOf(wzrost);


    if(x==null ){
    model.addAttribute("wzrost",true);
    return"views/success";
}
    if(y==null ){
        model.addAttribute("wzrost",true);
        return"views/success";
    }
}

ANd when i get a valu form masa and wzrost i check from null, i click submit alwas nullpointerexception

 <form th:action="@{/cal-bmi}" method="post">

    <ul class="gender-options">
        <input id="man" type="radio" name="gender" value="male" required  />
        <label for="man">mężczyzna</label> &frasl;
        <input id="woman" type="radio" name="gender" value="female"/>
        <label for="woman">kobieta</label>
    </ul>


    Masa: <input type="number" class="form-control form-text" required placeholder="(kg)" name="masa"/>

    <!--<div class="text col-sm-12 error" th:if="${wzrost}">-->
        <!--<p class="text text-center">-->
            <!--To pole jest wymagane-->
        <!--</p>-->
    <!--</div>-->
    Wzrost: <input type="number" class="form-control form-text " required placeholder="(cm)" name="wzrost"/>

    <!--<div class="text col-sm-12 error" th:if="${wzrost}">-->
        <!--<p class="text text-center">-->
            <!--To pole jest wymagane-->
        <!--</p>-->
    <!--</div>-->
    <input type="submit" class="col-lg-10 btn btn-primary" value="Oblicz"/>
</form>

Now i used required but is not good solution

Sign up to request clarification or add additional context in comments.

Comments

0

It seems like you want to implement server side validation. For this the best approach is to use validators and its bindingResult. Steps to implement server side validation is

  1. Have for model

    public class PersonForm {
      private String name;
    
     public String getName() {
        return this.name;
    }
    
     public void setName(String name) {
        this.name = name;
    }
    }
    
  2. Use form model in html

    <form action="#" th:action="@{/personForm}" th:object="${personForm}" method="post">
    <table>
        <tr>
            <td><label th:text="#{label.name}+' :'"></label></td>
            <td><input type="text" th:field="*{name}" /></td>
            <td th:if="${#fields.hasErrors('name')}" th:errors="*{name}">Generic Error</td>
        </tr>
        <tr>
            <td><button type="submit">Submit</button></td>
        </tr>
    </table>
    </form>
    
    1. Have validator class

    @Component

    public class PersonFormValidator implements Validator {
    @Override
    public boolean supports(Class<?> clazz) {
        return PersonForm.class.equals(clazz);
    }
    
    @Override
    public void validate(Object target, Errors errors) {
        ValidationUtils.rejectIfEmpty(errors, "name", "field.name.empty");
        PersonForm p = (PersonForm) target;
    
        if (p.getName().equalsIgnoreCase("XXX")) {
            errors.rejectValue("name", "Name cannot be XXX");
        }
    }}
    
    1. Bind validator to controller and let spring do the magic.

    @Controller

    public class WebController {
    @Autowired
    PersonFormValidator personFormValidator;
    
    
    @InitBinder("personForm")
    protected void initPersonFormBinder(WebDataBinder binder) {
    binder.addValidators(personFormValidator);
    }
    
    @PostMapping("/personForm")
    public String checkPersonInfo(@Validated PersonForm personForm, BindingResult bindingResult, final RedirectAttributes redirectAttributes) {
    if (bindingResult.hasErrors()) {
        return "personForm";
    }
    redirectAttributes.addFlashAttribute("personResult",  apiClientService.getPersonResult(personForm));
    return "redirect:/spouseForm";
    }
    }
    

3 Comments

But i dont use any th:object="${personForm}" is only th:action
I can see that you as using th:if="${wzrost}" which is used in case of you want to show error message from server, so you must implement server side validation, my answer is example how i did it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.