Hope that helps?
Since any value coming in from an HTML form is a string .. no such
thing as type there, i've been using beans that accept strings for all
values. This allows me to let anything in and validate it once it's in
the bean.
So i tried setting the nullable value for date to "",
<nullValues>
<date value="" />
<boolean value="0" />
</nullValues>
And found out that ColdSpring doesn't like that. So it seems there's a
minor incompatibility there. Here's the error i get:
Bean creation exception during init() of transfer.TransferFactory
"" is an invalid date or time string.
The error occurred in
C:\CFusionMX7\wwwroot\coldspring\beans\DefaultXmlBeanFactory.cfc: line
539
Called from
C:\CFusionMX7\wwwroot\coldspring\beans\DefaultXmlBeanFactory.cfc: line
332
Called from
C:\CFusionMX7\wwwroot\Unity\trunk\ModelGlue\unity\loader\XmlConfigurationLoader.cfc:
line 293
Ok, no problem, let's try something else:
<property name="dateLastFeatured" type="date" nullable="true"
nullvalue="" />
When i use this, ColdSpring doesn't complain, but a new transfer object
defaults to a datetime of now() instead of "", and i get an error if i
try to set the date to "". OK, that's not going to work if i can't set
the null value i've chosen.
So it seems i need to use a magic value of some sort that is a date to
represent null, rather than an empty string. So let's forget about
trying to set a different nullable value and use the default value of
1/1/100.
<property name="dateLastFeatured" type="date" nullable="true" />
Ok, so now when i create a new transferObject, the default date value
is Now(), which is OK, but a little useless from a certain perspective
because i can't easily test if the intention of the user is "null" or
not when they fill out the form.
I think everyone is familiar with this difficulty. In a web form, an
null date is an empty string. There's no other way to represent it to
the user that would make sense to them. Numerics are in a similar
situation, but many times a 0 will do when the intention is "nothing".
This is the main reason i've come to the conclusion that beans that
accept values from web forms shouldn't use typed arguments,
particularly typed date arguments. In the world of HTML, dates are
strings, and if i want to validate a date in this world, i'm looking
for either an empty string or a date. If my bean doesn't accept an
empty string, then it can't accept a valid value for a date from a
form, none.
And then of course on insert or update to the db, I'll use cfqueryparam
to set the value to null if it's an empty string.
So then i started thinking that it would be very nice if transfer had a
method where i could create a dumb bean that would accept any value
from a form. Then, i could pass that bean to my validation object, and
then if it validated, pass that on to the TransferObject to persist it.
I'm still thinking that would be a good enhancement, because then my
dumb beans would automatically be updated with any change to the
transfer.xml. But maybe it's beyond Transfer's scope.
But even with a dumb bean, i'm still left with the problem that to a
user, a null date is an empty string. I can of course work around this
issue, setting my magic null value on any dates immediately after any
transfer.new() call, looking for that magic value in the form and
resetting it to "", creating my own dumb bean to accept the values from
the form, looking for "" and resetting it to the magic value, and
proceeding from there.
But perhaps life would be easier if Transfer would accept an empty
string as a null value for a date?
Or maybe i'm not seeing a better solution to reconcile the typeless
world of the web with the typed world of databases?
thanks,
Nando
if(isDate(form.value))
{
obj.setDate(form.value);
}
else
{
obj.setDateNull();
}
?
Or is that too tricky? :oD
But yes - if you want to do it how you just described, with empty
strings, decorators are the key.
Mark
On 1/3/07, Nando <d.n...@gmail.com> wrote:
>
You can see the generated methods for handling NULL values.
Now() was just the arbitrary value for Date fields. If you want to
change default values, create a configure() method either in your
decorator, or in the config XML, and set it to whatever value you
wish.
Details on that, can be seen here:
http://www.compoundtheory.com/transfer/documentation/custommethods.html
and
http://www.compoundtheory.com/transfer/documentation/decorators.html
> I can manage in one direction with the above approach, from the form toward
> Transfer, but in the other direction, from Transfer toward the form, i'd
> still need to employ the decorator approach in kind of a funky way, looking
> for Now() or something close or the magic null value and converting it to an
> empty string.
Not sure what you mean here.
Does this solve your problem?
if(obj.getDateIsNull())
{
value = "";
}
else
{
value = obj.getDate();
}
Mark
I'll look into it.
Oops.
Mark
When you said;
> are correct. I'm guessing. Couldn't find any documentation to confirm my
> attempt.
Couldn't find documentation on what exactly? On the generated methods?
Just wanted to know where the confusion was so I can either point you
in the right direction, or update the documentation.
Mark
I'm guessing this section:
http://www.compoundtheory.com/transfer/documentation/decorators.html
Needs to be fleshed out a little more then?
An explanation or example under:
--
If a method exists in the Decorator, it will overwrite the method of
the generated TransferObject's method.
--
Would probably be good, wouldn't it?
Or maybe break up the sections inside the documentation?
Just looking to make the documentation better.
Let me know if that explains it a bit better.