Skip to content

Commit e8c80a6

Browse files
committed
don't allow image and image url at the same time
1 parent 0090338 commit e8c80a6

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

src/main/java/ru/mystamps/web/validation/jsr303/RequireImageOrImageUrl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
import static java.lang.annotation.RetentionPolicy.RUNTIME;
3030

3131
/**
32-
* Validates that an image or an image URL is specified.
32+
* Validates that an image or an image URL is specified but not both.
3333
*
3434
* In order to be validated by this validator, target form must implement the
3535
* {@link HasImageOrImageUrl} interface.

src/main/java/ru/mystamps/web/validation/jsr303/RequireImageOrImageUrlValidator.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
* Implementation of the {@link RequireImageOrImageUrl} validator.
2525
*
2626
* If an image and an image URL fields are empty, it marks both fields as having an error.
27+
* If both fields were filled it also marks them as having an error.
2728
*/
2829
public class RequireImageOrImageUrlValidator
2930
implements ConstraintValidator<RequireImageOrImageUrl, HasImageOrImageUrl> {
@@ -39,12 +40,15 @@ public boolean isValid(HasImageOrImageUrl value, ConstraintValidatorContext ctx)
3940
if (value == null) {
4041
return true;
4142
}
43+
44+
boolean hasImage = value.hasImage();
45+
boolean hasImageUrl = value.hasImageUrl();
4246

43-
if (value.hasImage()) {
47+
if (hasImage && !hasImageUrl) {
4448
return true;
4549
}
46-
47-
if (value.hasImageUrl()) {
50+
51+
if (hasImageUrl && !hasImage) {
4852
return true;
4953
}
5054

0 commit comments

Comments
 (0)