Example usage for org.apache.commons.validator GenericValidator isBlankOrNull

List of usage examples for org.apache.commons.validator GenericValidator isBlankOrNull

Introduction

In this page you can find the example usage for org.apache.commons.validator GenericValidator isBlankOrNull.

Prototype

public static boolean isBlankOrNull(String value) 

Source Link

Document

Checks if the field isn't null and length of the field is greater than zero not including whitespace.

Usage

From source file:com.myproject.validator.CustomValidator.java

/**
 * Example validator for comparing the equality of two fields
 *
 * http://struts.apache.org/userGuide/dev_validator.html
 * http://www.raibledesigns.com/page/rd/20030226
 *///from   ww w.ja  v  a  2 s . c o  m
public static boolean validateTwoFields(Object bean, ValidatorAction va, Field field, ActionMessages errors,
        HttpServletRequest request) {

    String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
    String property2 = field.getVarValue("secondProperty");
    String value2 = ValidatorUtils.getValueAsString(bean, property2);

    if (!GenericValidator.isBlankOrNull(value)) {
        try {
            if (!value.equals(value2)) {
                errors.add(field.getKey(), Resources.getActionMessage(request, va, field));

                return false;
            }
        } catch (Exception e) {
            errors.add(field.getKey(), Resources.getActionMessage(request, va, field));
            return false;
        }
    }
    return true;
}

From source file:net.sourceforge.fenixedu.presentationTier.validator.form.ValidateCompareTwoFields.java

/**
 * Compares the two fields using the given comparator
 * /*  w  w w.  ja  v a 2  s  .co  m*/
 * @param bean
 * @param va
 * @param field
 * @param errors
 * @param request
 * @param comparator
 * @return
 */
private static boolean validate(Object bean, ValidatorAction va, Field field, ActionMessages errors,
        HttpServletRequest request, Comparator comparator) {
    String greaterInputString = ValidatorUtils.getValueAsString(bean, field.getProperty());
    String secondProperty = field.getVarValue("secondProperty");
    String lowerInputString = ValidatorUtils.getValueAsString(bean, secondProperty);

    if (!GenericValidator.isBlankOrNull(lowerInputString)
            && !GenericValidator.isBlankOrNull(greaterInputString)) {
        try {
            Double lowerInput = new Double(lowerInputString);
            Double greaterInput = new Double(greaterInputString);
            // if comparator result != VALUE then the condition is false
            if (comparator.compare(lowerInput, greaterInput) != VALUE) {
                errors.add(field.getKey(), Resources.getActionMessage(request, va, field));
                return false;
            }
            return true;
        } catch (NumberFormatException e) {
            errors.add(field.getKey(), new ActionMessage(va.getMsg()));
            return false;
        }
    }
    return true;
}

From source file:jamm.webapp.JammValidators.java

/**
 * Checks if a field is identical to a second field.  Useful for
 * cases where you are comparing passwords.  Based on work in the
 * <i>Struts in Action</i> published by Manning.
 *
 * @param bean The bean validation is being performed on.
 * @param va The ValidatorAction that is currently being performed.
 * @param field The Field object associated with the current field
 * being validated.//from  w w  w. jav  a  2 s  . co  m
 * @param errors The ActionErrors object to add errors to if any
 * validation errors occur.
 * @param request Current request object.
 * @return True if valid or field blank, false otherwise.
 */
public static boolean validateIdentical(Object bean, ValidatorAction va, Field field, ActionErrors errors,
        HttpServletRequest request) {
    String value = ValidatorUtil.getValueAsString(bean, field.getProperty());
    String sProperty2 = field.getVarValue("secondProperty");
    String value2 = ValidatorUtil.getValueAsString(bean, sProperty2);

    if (!GenericValidator.isBlankOrNull(value)) {
        if (!value.equals(value2)) {
            errors.add(field.getKey(), Resources.getActionError(request, va, field));
            return false;
        }
    }
    return true;
}

From source file:com.codeup.auth.application.authentication.LoginInput.java

private void validateUsername(String username) {
    ArrayList<String> messages = new ArrayList<>();

    if (GenericValidator.isBlankOrNull(username))
        messages.add("Enter your username");
    if (username != null && !GenericValidator.matchRegexp(username, "[A-Za-z0-9._-]+"))
        messages.add("Your username does not have a valid format");

    if (messages.size() > 0)
        this.messages.put("username", messages);
}

From source file:info.bluefoot.componentsexample.web.HeroBean.java

@Override
protected Map<String, String> getSearchFields(Hero searchObj) {
    Map<String, String> filters = new HashMap<String, String>();
    if (searchObj != null) {
        if (!GenericValidator.isBlankOrNull(searchObj.getName())) {
            filters.put("name", searchObj.getName());
        }/*w  w  w.j av  a  2  s  . co m*/
        if (!GenericValidator.isBlankOrNull(searchObj.getLocation())) {
            filters.put("location", searchObj.getLocation());
        }
    }
    return filters;
}

From source file:com.panet.imeta.job.entry.validator.EmailValidator.java

public boolean validate(CheckResultSourceInterface source, String propertyName,
        List<CheckResultInterface> remarks, ValidatorContext context) {
    String value = null;/*from  w  ww.j  a v  a  2s .  c  o m*/

    value = getValueAsString(source, propertyName);

    if (!GenericValidator.isBlankOrNull(value) && !GenericValidator.isEmail(value)) {
        JobEntryValidatorUtils.addFailureRemark(source, propertyName, VALIDATOR_NAME, remarks,
                JobEntryValidatorUtils.getLevelOnFail(context, VALIDATOR_NAME));
        return false;
    } else {
        return true;
    }
}

From source file:com.raven.loginn.service.AccountService.java

public Account findAccountByEmail(String email) throws Exception {
    if (GenericValidator.isBlankOrNull(email)) {
        throw new WarningMessageException("E-mail bo olamaz");
    }//w  w w  .jav  a  2s  . com
    if (!EmailValidator.getInstance().isValid(email)) {
        throw new WarningMessageException("E-mail hatal");
    }
    return accountDao.findAccountByEmail(email);
}

From source file:com.draagon.meta.validator.LengthValidator.java

/**
 * Validates the value of the field in the specified object
 *///from   w  ww  . ja va 2  s  .  c om
public void validate(Object object, Object value)
//throws MetaException
{
    int min = 0;
    int max = getMetaField(object).getLength();

    if (hasAttribute(ATTR_MIN))
        min = Integer.parseInt((String) getAttribute(ATTR_MIN));
    if (hasAttribute(ATTR_MAX))
        max = Integer.parseInt((String) getAttribute(ATTR_MAX));

    String msg = getMessage("A valid length between " + min + " and " + max + " must be entered");
    String val = (value == null) ? null : value.toString();

    if (!GenericValidator.isBlankOrNull(val) && (val.length() < min || val.length() > max)) {
        throw new InvalidValueException(msg);
    }
}

From source file:alpha.portal.webapp.util.ValidationUtil.java

/**
 * Validates that two fields match.//ww w  . j a va  2  s .  c  om
 * 
 * @param bean
 *            the bean
 * @param va
 *            the va
 * @param field
 *            the field
 * @param errors
 *            the errors
 * @return true, if successful
 */
public static boolean validateTwoFields(final Object bean, final ValidatorAction va, final Field field,
        final Errors errors) {
    final String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
    final String sProperty2 = field.getVarValue("secondProperty");
    final String value2 = ValidatorUtils.getValueAsString(bean, sProperty2);

    if (!GenericValidator.isBlankOrNull(value)) {
        try {
            if (!value.equals(value2)) {
                FieldChecks.rejectValue(errors, field, va);
                return false;
            }
        } catch (final Exception e) {
            FieldChecks.rejectValue(errors, field, va);
            return false;
        }
    }

    return true;
}

From source file:com.panet.imeta.job.entry.validator.NotBlankValidator.java

public boolean validate(CheckResultSourceInterface source, String propertyName,
        List<CheckResultInterface> remarks, ValidatorContext context) {
    String value = ValidatorUtils.getValueAsString(source, propertyName);
    if (GenericValidator.isBlankOrNull(value)) {
        JobEntryValidatorUtils.addFailureRemark(source, propertyName, VALIDATOR_NAME, remarks,
                JobEntryValidatorUtils.getLevelOnFail(context, VALIDATOR_NAME));
        return false;
    } else {//from ww  w . jav a 2 s . c  om
        return true;
    }
}