5 package org
.spirit
.spring
.validate
;
7 import org
.apache
.commons
.logging
.Log
;
8 import org
.apache
.commons
.logging
.LogFactory
;
9 import org
.apache
.commons
.validator
.EmailValidator
;
10 import org
.apache
.commons
.validator
.UrlValidator
;
11 import org
.spirit
.form
.BotListPostListingForm
;
12 import org
.springframework
.validation
.Errors
;
13 import org
.springframework
.validation
.ValidationUtils
;
14 import org
.springframework
.validation
.Validator
;
17 * This is class is used by botverse.
18 * @author Berlin Brown
21 public class BotListPostListingValidator
implements Validator
{
23 private Log log
= LogFactory
.getLog(getClass());
26 * @see org.springframework.validation.Validator#supports(java.lang.Class)
28 public boolean supports(Class post
) {
29 return BotListPostListingForm
.class.isAssignableFrom(post
);
33 * Check for a valid email entry.
35 private boolean isValidEmail(String email
) {
36 return EmailValidator
.getInstance().isValid(email
);
40 * Check for a valid url entry.
42 private boolean isValidUrl(String url
) {
43 String
[] schemes
= { "http","https" };
44 UrlValidator urlValidator
= new UrlValidator(schemes
, UrlValidator
.NO_FRAGMENTS
);
45 return urlValidator
.isValid(url
);
49 * @see org.springframework.validation.Validator#validate(java.lang.Object, org.springframework.validation.Errors)
51 public void validate(Object obj
, Errors errors
) {
53 BotListPostListingForm form
= (BotListPostListingForm
) obj
;
55 ValidationUtils
.rejectIfEmptyOrWhitespace(errors
,
56 "title", "required", "* Title Field is required.");
57 ValidationUtils
.rejectIfEmptyOrWhitespace(errors
,
58 "message", "required", "* Message Field is required.");
59 ValidationUtils
.rejectIfEmptyOrWhitespace(errors
,
60 "email", "required", "* Email Field is required.");
61 ValidationUtils
.rejectIfEmptyOrWhitespace(errors
,
62 "userSolution", "required", "* Please add the following values.");
64 if ((form
!= null) && (form
.getMainUrl() != null)) {
65 if (form
.getMainUrl().length() != 0) {
66 String url
= form
.getMainUrl();
67 if (!this.isValidUrl(url
)) {
68 errors
.reject("mainUrl", "* Please enter a valid url address (for example: http://www.url.com)");
72 if ((form
!= null) && (form
.getEmail() != null)) {
73 String email
= form
.getEmail();
74 if (!this.isValidEmail(email
)) {
75 errors
.reject("email", "* Please enter a valid email address (for example: user@yourdomain.com)");
80 && (form
.getUserSolution() != null)
81 && (form
.getUserSolution() != null)) {
83 if (form
.getPrevSolution().intValue() != form
.getUserSolution().intValue()) {
84 errors
.reject("userSolution", "* Please add the following values.");