Package com.example.bookstore.validation

Source Code of com.example.bookstore.validation.OrderValidator

package com.example.bookstore.validation;

import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

import com.example.bookstore.domain.Address;
import com.example.bookstore.domain.Order;

/**
* Validates {@link Order} domain objects
*
*
*
*
*/
public class OrderValidator implements Validator {

  @Override
  public boolean supports(Class<?> clazz) {
    return (Order.class).isAssignableFrom(clazz);
  }

  @Override
  public void validate(Object target, Errors errors) {
    Order order = (Order) target;
    validateAddress(order.getShippingAddress(), errors, "shippingAddress");
    if (!order.isBillingSameAsShipping()) {
      validateAddress(order.getShippingAddress(), errors, "billingAddress");
    }

  }

  private void validateAddress(Address address, Errors errors, String type) {
    ValidationUtils.rejectIfEmpty(errors, type + ".street", "required", new Object[] { "Street" });
    ValidationUtils.rejectIfEmpty(errors, type + ".city", "required", new Object[] { "City" });
    ValidationUtils.rejectIfEmpty(errors, type + ".country", "required", new Object[] { "Country" });

  }

}
TOP

Related Classes of com.example.bookstore.validation.OrderValidator

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.