0

I have a List<List<String>> field in my entity. At some point I add a new String to this.

I tried to create a custom annotation, but for some reason it triggers an error when I'm trying to submit the form.

My annotation (I used one for a List<String> I found on here and modified):

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = NotEmptyFieldsValidator.class)
public @interface NotEmptyFields {

    String message() default "List cannot contain empty fields";

    Class<?>[] groups() default {};

    Class<? extends Payload>[] payload() default {};

}

My Validator:

public class NotEmptyFieldsValidator implements ConstraintValidator<NotEmptyFields, List<List<String>>> {

    @Override
    public void initialize(NotEmptyFields notEmptyFields) {
    }

    @Override
    public boolean isValid(List<List<String>> lists, ConstraintValidatorContext context) {

        List<String> objects = lists.stream()
        .flatMap(Collection::stream)
        .collect(Collectors.toList());
        return objects.stream().allMatch(nef -> nef != null && !nef.trim().isEmpty());
    }

}

My Controller part:

    @RequestMapping(value = "/insert", method = { RequestMethod.POST })
    public ModelAndView insert(@ModelAttribute("sprav") @Valid Spravochnik sprav, ModelMap model, BindingResult bindingResult) {

        if(bindingResult.hasErrors()) {
            model.addAttribute("sprav", sprav);
            return new ModelAndView("insert", model);
        }

        if(spravochnikService.ins(sprav.getTableName(), sprav) == true) {

        model.addAttribute("tableName", sprav.getTableName());
        model.addAttribute("id", sprav.getId());

        return new ModelAndView("redirect:/spravochnik/list/{tableName}", model);
        }
        else {
            return new ModelAndView("error", model);
        }
    }

The Spravochink is the structure with this complex field. It looks like:

public class Spravochnik extends BaseEntity {

    private String tableName;
    private List<String> columnName;
    @NotEmptyFields
    private List<List<String>> valuesInRows;

    public Spravochnik() {

    }

    public String getTableName() {
        return tableName;
    }
    public void setTableName(String tableName) {
        this.tableName = tableName;
    }
    public List<String> getColumnName() {
        return columnName;
    }
    public void setColumnName(List<String> columnName) {
        this.columnName = columnName;
    }
    public List<List<String>> getValuesInRows() {
        return valuesInRows;
    }
    public void setValuesInRows(List<List<String>> valuesInRows) {
        this.valuesInRows = valuesInRows;
    }


rgettman
  • 176,041
  • 30
  • 275
  • 357
guido
  • 65
  • 1
  • 9
  • Check [here](https://stackoverflow.com/questions/31132477/java-annotation-for-null-but-neither-empty-nor-blank). But also experiment with marking validator like this: `private List> valuesInRows;` There is also a validator `@NotBlank` which fits Your needs but You need to add one more dependency `hibernate-validator` – zolv May 20 '19 at 20:40
  • Thank you all for your time! I resolved it, I need to change the validator as it swears at the first value in the list which is null, the one I'm not inserting and keeping hidden – guido May 20 '19 at 20:42
  • Also I didn't know you could use validator annotations INSIDE the <> brackets! Thanks so much for knowledge! – guido May 20 '19 at 20:47

0 Answers0