Validation - how to check that string is not null but can be empty?

637 views
Skip to first unread message

Tomas

unread,
Sep 30, 2011, 3:05:47 AM9/30/11
to play-framework
Hi, this might be simple, but I don't see which validation annotation
to use if I want to accept empty string but not null.

christian sarnataro

unread,
Sep 30, 2011, 9:26:14 AM9/30/11
to play-framework
Hi, I think (but I may be wrong) that there is no single annotation
for this kind of check, but you can achieve the same result with
something like this:

if (parameter == null) Validation.addError("parameter",
"message.parameter.isnull");

Hope it helps

Tomas

unread,
Sep 30, 2011, 10:44:09 AM9/30/11
to play-framework
Thanks for your answer, Christian. I think that it would be nice to
have it as a single annotation, e.g. @NotNullable.

Tomas

On Sep 30, 3:26 pm, christian sarnataro

canavar

unread,
Oct 1, 2011, 10:41:00 AM10/1/11
to play-fr...@googlegroups.com

Can @Match(".*") work?

30 Eyl 2011 17:44 tarihinde "Tomas" <gugl...@zoznam.sk> yazdı:
> --
> You received this message because you are subscribed to the Google Groups "play-framework" group.
> To post to this group, send email to play-fr...@googlegroups.com.
> To unsubscribe from this group, send email to play-framewor...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/play-framework?hl=en.
>

Tomas

unread,
Oct 7, 2011, 9:11:31 AM10/7/11
to play-framework
not really, since all checks except for @Required ommit null value
(return true). But I found a solution: The Oval framework actually the
right class that I need - NotNullCheck. The only annoyance is that I
would need to override the message each time, so I implemented my own
annotation:

@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.FIELD, ElementType.PARAMETER })
@Constraint(checkWith = NotNullCheck.class)
public @interface IsNotNull {
String message() default "validation.required";
}



On Oct 1, 4:41 pm, canavar <fehmican.sag...@gmail.com> wrote:
> Can @Match(".*") work?
> 30 Eyl 2011 17:44 tarihinde "Tomas" <guglj...@zoznam.sk> yazdı:

canavar

unread,
Oct 7, 2011, 9:42:24 AM10/7/11
to play-fr...@googlegroups.com

Hmm. Good solution.

Maybe a parameter like allowEmpty could be added to @Required. What do you think?

07 Eki 2011 16:11 tarihinde "Tomas" <gugl...@zoznam.sk> yazdı:

Ronald Haring

unread,
Oct 7, 2011, 10:07:14 AM10/7/11
to play-fr...@googlegroups.com
And when would allowEmpty return true? Is that true for a string if it contains only spaces? or is that false then because allowEmpty is only true if there is nothing in it but an empty string?

And what for a boolean? or a Boolean?

I think that based on each context that is wanted, you should  use a specific annotation, or method, instead of updating the @Required with this.

Regards
Ronald

Fehmi Can SAĞLAM

unread,
Oct 9, 2011, 6:25:39 AM10/9/11
to play-fr...@googlegroups.com, Ronald Haring
I see your point Ronald. Then Tomas' IsNotNull annotation may be made
built-in?

> --
> You received this message because you are subscribed to the Google
> Groups "play-framework" group.

> To view this discussion on the web visit
> https://groups.google.com/d/msg/play-framework/-/2a26NiqUBXIJ.

Ronald Haring

unread,
Oct 9, 2011, 6:44:48 AM10/9/11
to play-fr...@googlegroups.com
Or perhaps NotEmpty, because he wanted a check for an empty string. I think that NotNull is already available through the oval framework on which play depends

Regards
Ronald
WebRep
Algemene beoordeling
 

Fehmi Can SAĞLAM

unread,
Oct 9, 2011, 6:58:09 AM10/9/11
to play-fr...@googlegroups.com, Ronald Haring

On Sun Oct 9 13:44:48 2011, Ronald Haring wrote:
> Or perhaps NotEmpty, because he wanted a check for an empty string.

No, he wants to allow empty string but @Required does not allow it.

> I think that NotNull is already available through the oval framework on
> which play depends

Yes, it is available as in his code sample. But it seems that there is
no annotation.

>
> Regards
> Ronald
> WebRep
> Algemene beoordeling


>
> --
> You received this message because you are subscribed to the Google
> Groups "play-framework" group.
> To view this discussion on the web visit

> https://groups.google.com/d/msg/play-framework/-/qJNrIGS5grkJ.

Reply all
Reply to author
Forward
0 new messages