label validator finished

This commit is contained in:
Ezequiel Bergamaschi
2014-01-28 02:21:14 -03:00
committed by Ezequiel Bergamaschi
parent b19ac2c4c3
commit 72a46367d6
8 changed files with 44 additions and 2 deletions

View File

@@ -1,6 +1,9 @@
package com.wisemapping.validator;
import com.wisemapping.model.Constants;
import com.wisemapping.model.Label;
import com.wisemapping.model.User;
import com.wisemapping.service.LabelService;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.springframework.validation.Errors;
@@ -8,6 +11,13 @@ import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;
public class LabelValidator implements Validator {
private final LabelService service;
public LabelValidator(@NotNull final LabelService service) {
this.service = service;
}
@Override
public boolean supports(Class<?> clazz) {
return clazz.equals(Label.class);
@@ -26,7 +36,18 @@ public class LabelValidator implements Validator {
private void validateLabel(@NotNull final Label label, @NotNull final Errors errors) {
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "title", Messages.FIELD_REQUIRED);
String title = label.getTitle();
//todo hacer otras validaciones como si supera el maximo o el label existe
final String title = label.getTitle();
ValidatorUtils.rejectIfExceeded(
errors,
"title",
"The description must have less than " + Constants.MAX_LABEL_NAME_LENGTH + " characters.",
title,
Constants.MAX_LABEL_NAME_LENGTH);
final User user = com.wisemapping.security.Utils.getUser();
assert user != null;
final Label foundLabel = service.getLabelByTitle(title, user);
if (foundLabel != null) {
errors.rejectValue("title", Messages.LABEL_TITLE_ALREADY_EXISTS);
}
}
}

View File

@@ -24,6 +24,7 @@ public interface Messages {
String FIELD_REQUIRED = "FIELD_REQUIRED";
String IMPORT_MAP_ERROR = "IMPORT_MAP_ERROR";
String MAP_TITLE_ALREADY_EXISTS = "MAP_TITLE_ALREADY_EXISTS";
String LABEL_TITLE_ALREADY_EXISTS = "LABEL_TITLE_ALREADY_EXISTS";
String PASSWORD_MISSMATCH = "PASSWORD_MISSMATCH";
String CAPTCHA_ERROR = "CAPTCHA_ERROR";
String CAPTCHA_LOADING_ERROR = "CAPTCHA_LOADING_ERROR";