Skip to content

Conversation

@theigl
Copy link
Contributor

@theigl theigl commented Nov 19, 2017

This PR adds support for Bean Validation 2.0 to wicket-bean-validation.

It extends scanning of @NotNull annotations to @NotEmpty and @NotBlank and supports registering further annotations that should be treated as not-null constraints to guard against future changes.

See https://issues.apache.org/jira/browse/WICKET-6499

@theigl theigl force-pushed the WICKET-6499-bean-validation-2x branch 2 times, most recently from 40824aa to fd07047 Compare November 19, 2017 10:49

HashSet<Class<?>> validatorGroups = new HashSet<Class<?>>();
HashSet<Class<?>> validatorGroups = new HashSet<>();
validatorGroups.addAll(Arrays.asList(getGroups()));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would use Set here


for (NotNull constraint : constraints)
for (Annotation constraint : constraints.keySet())
{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

entrySet() can be used here to avoid double search

@theigl theigl force-pushed the WICKET-6499-bean-validation-2x branch from fd07047 to 46b4634 Compare November 19, 2017 11:09
@theigl
Copy link
Contributor Author

theigl commented Nov 19, 2017

@solomax: Thanks for the quick feedback. I adjusted the PR accordingly.

{
if (canApplyToDefaultGroup(constraint) && validatorGroups.isEmpty())
final Annotation annotation = entry.getKey();
ConstraintDescriptor<?> constraintDescriptor = constraints.get(annotation);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can use entry.getValue() here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, totally overlooked that. Changed.

@theigl theigl force-pushed the WICKET-6499-bean-validation-2x branch 2 times, most recently from 36fbc18 to 65e9d88 Compare November 20, 2017 08:50
<entry key="javax.validation.constraints.Max.message">Le champ '${label}' doit être inférieur ou égal à ${value}</entry>
<entry key="javax.validation.constraints.Min.message">Le champ '${label}' doit être supérieur ou égal à ${value}</entry>
<entry key="javax.validation.constraints.NotNull.message">Le champ '${label}' ne doit pas être null</entry>
<entry key="javax.validation.constraints.NotBlank.message">Le champ '${label}' ne peut pas être vide</entry>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A couple of other annotations have been added to BV 2.0 as well:

new built-in constraints: @Email, @notempty, @notblank, @positive, @PositiveOrZero, @Negative, @NegativeOrZero, @PastOrPresent and @FutureOrPresent

@solomax: Should we add translations for these new constraints?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 for new labels, I'll add _ru localization ...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added properties for English and French. My French is a bit rusty, but the translations should be ok.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is APL licensed so, I guess, we can copy
I would better use existing ones, but not sure how is this possible :(

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The existing ones could probably be loaded via a custom IStringResourceLoader but this is out of the scope of this PR.

@solomax: What else do you want me to do for this PR so it can be merged?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK with me :)

@theigl theigl force-pushed the WICKET-6499-bean-validation-2x branch from 65e9d88 to a6e3268 Compare November 20, 2017 09:09
@svenmeier
Copy link
Contributor

Actually I'm wondering whether we should just add another method to ITagModifier (possibly with better names):

public interface ITagModifier<T extends Annotation>
{
  void modify(FormComponent<?> component, T annotation);
	
  void modify(FormComponent<?> component, ComponentTag tag, T annotation);
}

This way we can use the identical lookup BeanValidationConfiguration#getTagModifier() to alter the component itself from #onConfigure() and the component tag from #onComponentTag().

All that's required for the new annotations is then:

register(Size.class, new SizeTagModifier());
register(NotNull.class, new RequiredTagModifier<NotNull>());
register(NotEmpty.class, new RequiredTagModifier<NotEmpty>());
register(NotBlank.class, new RequiredTagModifier<NotBlank>());

@theigl
Copy link
Contributor Author

theigl commented Nov 22, 2017

@svenmeier: This makes a lot of sense. I'm not sure however how much time I'll find the next weeks to work on this.

I suggest the following approach:

  1. I'll look into this this week and try to rework the code as you suggested.
  2. If It takes too much time and I can't finish it, I vote for merging a very simple version of this PR that only applies the changes required for making wicket-bean-validation work with BV 2.0. I'll remove the API changes I introduced in BeanValidationConfiguration and hard-code the list of built-in annotations that imply a not-null constraint. This way, we do not introduce any new methods that we would later remove if your solution is implemented.

WDYT?

@svenmeier
Copy link
Contributor

That's a very good approach, thank you.

@theigl theigl force-pushed the WICKET-6499-bean-validation-2x branch from 7579651 to 145e12e Compare November 23, 2017 09:58
@theigl
Copy link
Contributor Author

theigl commented Nov 23, 2017

I pushed the simplified implementation without API changes in 145e12e.

import java.util.List;
import java.util.Set;
import java.lang.annotation.Annotation;
import java.util.*;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No * imports please

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed

@solomax
Copy link
Contributor

solomax commented Nov 27, 2017

I believe this can be closed
@theigl could you please close this PR?

@theigl theigl closed this Nov 27, 2017
@theigl theigl deleted the WICKET-6499-bean-validation-2x branch November 27, 2017 13:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants