Last active
August 27, 2021 18:46
-
-
Save bjpeterdelacruz/8f8332a751371fdbe421 to your computer and use it in GitHub Desktop.
Validator that contains an HttpServletRequest object used to get the session
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.math.BigDecimal; | |
import javax.servlet.http.HttpServletRequest; | |
import org.springframework.validation.Errors; | |
import org.springframework.validation.Validator; | |
public class BillValidator implements Validator { | |
private HttpServletRequest request; | |
public void setRequest(HttpServletRequest request) { | |
this.request = request; | |
} | |
public boolean supports(Class clazz) { | |
return clazz.equals(Form.class); | |
} | |
public void validate(Object command, Errors errors) { | |
BigDecimal paymentDue = (BigDecimal) request.getSession().getAttribute("paymentDue"); | |
if (paymentDue.compareTo(BigDecimal.ZERO) <= 0) { | |
errors.reject("error.noPaymentDue"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How can I use it to achieve this?
DTO
Ajax post request
Spring @controller (not RestController)