Skip to content

How we can send message in Header from Validator interface ? #265

@ghost

Description

Could you please guide on below? I am performing field validations and send error in HTTPHeader back to AngularJS or using Jackson ObjectMapper?

ObjectMapper mapper = new ObjectMapper();
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
mapper.writeValue(????, error);

Here is what I've customized.

public class StudentValidation implements Validator{
    private static final String ID_PATTERN = "\\d+";
    private static final String EMAIL_PATTERN = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"  
            + "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";  
    private Pattern pattern;  
    private Matcher matcher;  
    @Autowired
    private MessageSource msgSource;

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

    @Override
    public void validate(Object target, Errors errors) {
        StudentResource studentResource = (StudentResource) target;

        if(studentResource.getId != null){
            pattern = Pattern.compile(ID_PATTERN);
            matcher = pattern.matcher(studentResource.getMobilePhoneNumber());
            if (!matcher.matches()) {  
                ObjectMapper mapper = new ObjectMapper();
                mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
                ErrorResource error = new ErrorResource("studentId","Enter a numeric values only");
                // TODO ?? How we can send response bacl
                //errors.rejectValue("studentId", "studentId.incorrect","Enter a numeric values only");  
                mapper.writeValue(????);
            }
        }
        if(!(studentResource.getEmail() != null && studentResource.getEmail().isEmpty())){
            pattern = Pattern.compile(EMAIL_PATTERN);  
            matcher = pattern.matcher(studentResource.getEmail());  
            if (!matcher.matches()) {  
                errors.rejectValue("Email", "Email.incorrect", "Enter a correct email");  
            }  
        }
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions