1

I have the same form that displays more or less fields depending on the user's role. So I use @JsonView to hide/ignore the fields that are not related to the current user's role. But the validation is still enabled and the @NotNull rule is triggered.

@JsonView({View.Admin.class, View.Tech.class})
@NotNull
private String name;

I would like to find a way to enable or disable some validation annotations for example when the user's role is not administrator.

Is it possible to use these view (View.Admin.class / View.Tech.class) as a group for validation please?

xerx593
  • 12,237
  • 5
  • 33
  • 64
Jedupont
  • 401
  • 1
  • 4
  • 12
  • Check this out: https://stackoverflow.com/questions/9257673/conditional-validations-in-spring – Rafael Guillen Oct 12 '22 at 14:29
  • I think "validation groups" offers the same functionality (for validation) as "json views" (for json;) – xerx593 Oct 12 '22 at 14:30
  • 1
    @xerx593 Yes, but I can't use my views as groups: `groups = { View.Admin.class, View.Tech.class }` – Jedupont Oct 12 '22 at 14:34
  • @RafaelGuillen I looked at it but I have to pass the group in @Validated but now I want it to be dynamic depending on my user's role, that's my problem. I also looked at using `@GroupSequenceProvider`, but I can't @Autowired my user in the custom provider :/ – Jedupont Oct 12 '22 at 14:37

1 Answers1

-1

use @JsonIgnore may help you to achieve,because it will ignore the column and validation annotation as well

mildgrey
  • 61
  • 4
  • Thanks for your answer, but could you add a little more context please? I'm not sure I understand how to use it with the`@JsonView` that already takes into account my roles – Jedupont Oct 12 '22 at 15:23
  • when we create multiple related table with bidirectional relationship, in that case when we fetch column data and we want from parent table, we use @jsonIgnore for getting data through parent table – mildgrey Oct 12 '22 at 15:30
  • I think in this situation we also ignore the column by using @JsonIgnore annotation – mildgrey Oct 12 '22 at 15:31
  • I hope it will help you in resolving your problem – mildgrey Oct 12 '22 at 15:35