Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package cz.muni.fi.dndtroopsweb.validation; import cz.muni.fi.dndtroopsapi.dto.TroopCreateDTO; import java.math.BigDecimal; import org.springframework.validation.Errors; import org.springframework.validation.Validator; /** * * @author Karel Auf */ public class TroopCreateDTOValidator implements Validator { @Override public boolean supports(Class<?> clazz) { return TroopCreateDTO.class.isAssignableFrom(clazz); } /** * Validates data for about to be created troop */ @Override public void validate(Object target, Errors errors) { TroopCreateDTO troopCreateDTO = (TroopCreateDTO) target; if (troopCreateDTO.getName() == null || troopCreateDTO.getName().length() < 3) { errors.rejectValue("name", "TroopCreateDTOValidator.bad.name"); } try { if (troopCreateDTO.getMoney() == null || (troopCreateDTO.getMoney().compareTo(new BigDecimal("0"))) == -1) { errors.rejectValue("money", "TroopCreateDTOValidator.bad.money"); } } catch (Exception e) { errors.rejectValue("money", "TroopCreateDTOValidator.bad.money"); } if (troopCreateDTO.getMission() == null || troopCreateDTO.getMission().length() < 3) { errors.rejectValue("mission", "TroopCreateDTOValidator.bad.mission"); } } }