Making a field required if another field is not null?

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Making a field required if another field is not null?

John W Newman
Hi,

I guess this is best illustrated with an example.

public class Person  {
    private String firstName;
    private String lastName;
}
 
ActionBean:
@ValidateNestedProperties({
    @Validate(field="p.firstName",
                 required="if lastName is not null")
    @Validate(field="p.lastName"
                 required="if firstName is not null")
    private Person p;
)}
               
They can leave the person's name totally blank, but if they enter one they must
enter the other.  Right now I'm doing a custom validation method to make this
happen, but I'm thinking there might be a way to do this with the annotations.
I looked at the expression attribute but I couldn't get anything.

I'm after something like,

expression = ((this == null) == (otherField == null))

Really I'm actually doing this social security numbers (broken into 3 fields)
and the custom method for this just feels dirty.  I'd actually like to see if
someone can come up with more concise logic than what I've got .. I think
there's a better way to write this.

@ValidationMethod(on=Events.ModifyPatient.modifyPatient)
public void checkThreeSsnFields(ValidationErrors errors)  {
    if ( ! ((pt.getSs1() == null &&
                pt.getSs2() == null &&
                pt.getSs3() == null) ||
                                       
            (pt.getSs1() != null &&
                 pt.getSs2() != null &&
                 pt.getSs3() != null))) {
                       
        if (pt.getSs1() == null)  {
            errors.add("pt.ss1", requiredError);
        }
        if (pt.getSs2() == null)  {
            errors.add("pt.ss2", requiredError);
        }
        if (pt.getSs3() == null)  {
             errors.add("pt.ss3", requiredError);
        }
    }
}

TIA
John


-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
Stripes-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/stripes-users
Reply | Threaded
Open this post in threaded view
|

Re: Making a field required if another field is not null?

John W Newman
I just tried

@Validate(field = "pt.firstName",
            required = ((this == null) == (pt.getLastName() == null))

but that must be a constant expression.  :(

Any chance of making the annotation parameters dynamic?  That would be really
really slick!!  I've never wrote an annotation so I don't know if that's
possible or not.  But if it is and it wouldn't require a team from nasa to do it
I think it would be a great feature.


-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
Stripes-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/stripes-users
Reply | Threaded
Open this post in threaded view
|

Re: Making a field required if another field is not null?

Aaron Porter-2
Try something like this:

@Validate(expression="(!empty pt.firstName) || (empty pt.lastName)")

It looked to me like you're trying to require the first name if the last
name is not null but I may have missed your intent.

You use standard JSP EL inside of the expression and it is evaluated
with a root context of the ActionBean.

Aaron

John Newman wrote:

> I just tried
>
> @Validate(field = "pt.firstName",
>             required = ((this == null) == (pt.getLastName() == null))
>
> but that must be a constant expression.  :(
>
> Any chance of making the annotation parameters dynamic?  That would be really
> really slick!!  I've never wrote an annotation so I don't know if that's
> possible or not.  But if it is and it wouldn't require a team from nasa to do it
> I think it would be a great feature.
>
>
> -------------------------------------------------------------------------
> Using Tomcat but need to do more? Need to support web services, security?
> Get stuff done quickly with pre-integrated technology to make your job easier
> Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
> http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642


-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
Stripes-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/stripes-users
Reply | Threaded
Open this post in threaded view
|

Re: Making a field required if another field is not null?

Ben Gunter
In reply to this post by John W Newman
The Java language spec says they have to be constants, I believe.

John Newman wrote:

> I just tried
>
> @Validate(field = "pt.firstName",
>             required = ((this == null) == (pt.getLastName() == null))
>
> but that must be a constant expression.  :(
>
> Any chance of making the annotation parameters dynamic?  That would be really
> really slick!!  I've never wrote an annotation so I don't know if that's
> possible or not.  But if it is and it wouldn't require a team from nasa to do it
> I think it would be a great feature.
>
>
> -------------------------------------------------------------------------
> Using Tomcat but need to do more? Need to support web services, security?
> Get stuff done quickly with pre-integrated technology to make your job easier
> Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
> http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
> _______________________________________________
> Stripes-users mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/stripes-users
>  

-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
Stripes-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/stripes-users
Reply | Threaded
Open this post in threaded view
|

Re: Making a field required if another field is not null?

Tim Fennell-3
It does unfortunately.  Not only does it require it to be constant, but
you're actually limited to a fairly small set of well known and
immutable JDK classes :(

When I've had the need to do this, I've done as Aaron suggested earlier
in this thread:
    @Validate(expr="(ss1 == null && ss2 == null) || ss3 != null")

-t

On 2006-10-13 15:58:24 -0400, Ben Gunter
<[hidden email]> said:

> The Java language spec says they have to be constants, I believe.
>
> John Newman wrote:
>> I just tried
>> @Validate(field = "pt.firstName",
>> required = ((this == null) == (pt.getLastName() == null))
>>
>> but that must be a constant expression.  :(
>>
>> Any chance of making the annotation parameters dynamic?  That would be really
>> really slick!!  I've never wrote an annotation so I don't know if that's
>> possible or not.  But if it is and it wouldn't require a team from nasa
>> to do it
>> I think it would be a great feature.
>>
>>
>> -------------------------------------------------------------------------
>> Using Tomcat but need to do more? Need to support web services, security?
>> Get stuff done quickly with pre-integrated technology to make your job easier
>> Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
>> http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
>> _______________________________________________
>> Stripes-users mailing list
>> [hidden email]
>> https://lists.sourceforge.net/lists/listinfo/stripes-users
>>
>
> -------------------------------------------------------------------------
> Using Tomcat but need to do more? Need to support web services, security?
> Get stuff done quickly with pre-integrated technology to make your job easier
> Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
> http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642


--
-Tim Fennell
http://stripes.mc4j.org/
Stripes: Because web development should just be easier



-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
Stripes-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/stripes-users
Reply | Threaded
Open this post in threaded view
|

Re: Making a field required if another field is not null?

John W Newman
Tim Fennell <tfenne@...> writes:

>
> It does unfortunately.  Not only does it require it to be constant, but
> you're actually limited to a fairly small set of well known and
> immutable JDK classes :(
>
> When I've had the need to do this, I've done as Aaron suggested earlier
> in this thread:
>      <at> Validate(expr="(ss1 == null && ss2 == null) || ss3 != null")
>
> -t

Good stuff guys thank you.  I was unaware that the expression param uses EL,
that's odd.  I thought el was just a presentation thing and wasn't used anywhere
but jsp.  But I guess since it's quoted that would explain why it's possible.

Since the language spec is so restricted i guess you can't make expression = to
a dynamic java boolean expression.  That's really unfortunate I wonder if that
will ever be improved, it would really make some interesting things possible.


Hey aaron, i haven't forogot about you.  I was out sick most of last week so
I've been swamped at work.  We did a server switch that did NOT go well at all
thanks to the incompetence of some network support eggs, and we've all been
scrambling to get everything restored.  Now my projects are way behind schedule.

But _eventually_ maybe in a few weeks to a month I'll send you something nice to
put up there.

PS i found a bug in stripernate I've been meaning to tell you about.  I guess
i'll just do it now since you'll probably read this and I'm here.

In HibernateTypeConverter.java, line 74 i think, after that big long line where
you do the conversion, o can be null.  A good way to reproduce this is:

Model:
public class Thing  {
    private Long id;
    ...
}

action bean:
public class ActionBean  {
   Thing t;
}


jsp:
Find thing by id: <stripes:text name="t" /> <stripes:submit name="find" />

If you type in an id number like 4 it works great.  Leave it blank or type in
asdfasdf and it will throw an exception because o is null.  A nice side effect
though is the box will light up yellow.  To fix, I just put this after that line
and all is well:

try  {
    // o = big long conversion thing
} catch (something)  {
}
if (o == null)  {
   return null;
}
Log.something(o.class) // o can be null


-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
Stripes-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/stripes-users
Reply | Threaded
Open this post in threaded view
|

Re: Making a field required if another field is not null?

Aaron Porter-3
Thanks John, the fix is in. I might get a new version out today. There
are some significant changes like a Formatter that need to be released.

Aaron

John Newman wrote:
> PS i found a bug in stripernate I've been meaning to tell you about.  I guess
> i'll just do it now since you'll probably read this and I'm here.
>
> In HibernateTypeConverter.java, line 74 i think, after that big long line where
> you do the conversion, o can be null.  A good way to reproduce this is:
>  


-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
Stripes-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/stripes-users