2 years ago

#35937

test-img

CS1999

How to change custom validation message in the validator class in spring boot?

Below is my code

package com.jockeyclub.races.validation;

import com.gargoylesoftware.htmlunit.HttpMethod;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.WebRequest;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import org.springframework.beans.BeanWrapperImpl;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.io.IOException;
import java.net.URL;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class RaceReqConstraintValidator implements ConstraintValidator<RaceReq, Object> {

    private String raceNum;
    private String raceDate;
    private String message;

    @Override
    public void initialize(RaceReq constraintAnnotation) {
        ConstraintValidator.super.initialize(constraintAnnotation);
        raceDate = constraintAnnotation.raceDate();
        raceNum = constraintAnnotation.raceNum();
    }

    @Override
    public boolean isValid(Object value, ConstraintValidatorContext context) {
        String rn = (String) new BeanWrapperImpl(value).getPropertyValue(raceNum);
        String rd = (String) new BeanWrapperImpl(value).getPropertyValue(raceDate);

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");
        String d = formatter.format(LocalDate.parse(new String((String) rd), DateTimeFormatter.ofPattern("yyyy-MM-dd")));
        String u = String.format("https://racing.hkjc.com/racing/information/English/Racing/LocalResults.aspx?RaceDate=%s", d);

        try {
            URL url = new URL(u);
            WebRequest request = new WebRequest(url, HttpMethod.POST);
            WebClient webClient = new WebClient();
            webClient.getOptions().setJavaScriptEnabled(true);
            webClient.getOptions().setCssEnabled(false);
            webClient.getCookieManager().setCookiesEnabled(true);
            HtmlPage page = webClient.getPage(request);

            if (page.getElementById("errorContainer") != null) {
                context.disableDefaultConstraintViolation();
                context.buildConstraintViolationWithTemplate(context.getDefaultConstraintMessageTemplate()).addPropertyNode(raceDate).addConstraintViolation(); //message: "No race on this date"
                return false;
            }
            if (!raceNum.equals("1") && page.getByXPath(String.format("/html/body/div/div[2]/table/tbody/tr/td[%s]/a", raceNum)).isEmpty()) {
                context.disableDefaultConstraintViolation();
                context.buildConstraintViolationWithTemplate(context.getDefaultConstraintMessageTemplate()).addPropertyNode(raceNum).addConstraintViolation(); //message: "Invalid race number"
                return false;
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
        return true;
    }
}

this is my validator class wherein I return false under 2 conditions. First if condition will return false if there is no race on that date. Second, if condition will return false if the race number is invalid Here I want to send different messages if my first if condition returns false and if my second if condition returns false. Is there any way to do this? I have mentioned the messages as comments

java

spring-boot

validation

customvalidator

0 Answers

Your Answer

Accepted video resources