how to initialize instance data with computed value?

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

how to initialize instance data with computed value?

Ken Yap-2
I have a date field in the the instance data and it comes up empty
when the form loads.

Ok, to initialize it I just put the value inline:

  <date>2008-05-30</date>

But I don't want any old date decided when I wrote the model, I would
like it to be today's date. I can get that from substring(now(),1,10),
but how to I set date to that? The only way I can think of is to do a
setvalue after the instance has loaded. And I would have to set it
only if the field is blank.

Is there a simpler notation for what I want?
_______________________________________________
dev-tech-xforms mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-xforms
Reply | Threaded
Open this post in threaded view
|

Re: how to initialize instance data with computed value?

dr.cw.ray
On May 30, 9:48 am, Ken Yap <[hidden email]> wrote:

> I have a date field in the the instance data and it comes up empty
> when the form loads.
>
> Ok, to initialize it I just put the value inline:
>
>   <date>2008-05-30</date>
>
> But I don't want any old date decided when I wrote the model, I would
> like it to be today's date. I can get that from substring(now(),1,10),
> but how to I set date to that? The only way I can think of is to do a
> setvalue after the instance has loaded. And I would have to set it
> only if the field is blank.
>
> Is there a simpler notation for what I want?

I think what you're looking for is something like this:
<xf:bind nodeset="//dateToday" type="xs:date" calculate="now()"/>

Today's date should appear in the input box.
_______________________________________________
dev-tech-xforms mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-xforms
Reply | Threaded
Open this post in threaded view
|

Re: how to initialize instance data with computed value?

dr.cw.ray
On Jun 1, 11:49 am, [hidden email] wrote:

> On May 30, 9:48 am, Ken Yap <[hidden email]> wrote:
>
> > I have a date field in the the instance data and it comes up empty
> > when the form loads.
>
> > Ok, to initialize it I just put the value inline:
>
> >   <date>2008-05-30</date>
>
> > But I don't want any old date decided when I wrote the model, I would
> > like it to be today's date. I can get that from substring(now(),1,10),
> > but how to I set date to that? The only way I can think of is to do a
> > setvalue after the instance has loaded. And I would have to set it
> > only if the field is blank.
>
> > Is there a simpler notation for what I want?
>
> I think what you're looking for is something like this:
> <xf:bind nodeset="//dateToday" type="xs:date" calculate="now()"/>
>
> Today's date should appear in the input box.

Sorry, I didn't ask if you knew how to do a bind; I'll just assume you
may not.

Put the above code in your model.  I always put my binds after my
instance and submissions, but i think you can put them anywhere in the
model. I set mine like so:

<xf:model>
    <xf:instance>
      ...
    </xf:instance>

   <xf:submission>
      ... if any
   <xf:submission>

   <xf:bind nodeset="//dateToday" type="xs:date" calculate="now()"/>
   <xf:bind  ...

</xf:model>

of course, change "now()" to "substring(now(),1,10)" if you like.

If you're using xf:setvalue or xf:output, then you can use
value="substring(now(),1,10)" or you can just use <xf:output ref="//
dateToday" />
_______________________________________________
dev-tech-xforms mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-xforms
Reply | Threaded
Open this post in threaded view
|

Re: how to initialize instance data with computed value?

dr.cw.ray
On Jun 1, 12:13 pm, [hidden email] wrote:

> On Jun 1, 11:49 am, [hidden email] wrote:
>
>
>
> > On May 30, 9:48 am, Ken Yap <[hidden email]> wrote:
>
> > > I have a date field in the the instance data and it comes up empty
> > > when the form loads.
>
> > > Ok, to initialize it I just put the value inline:
>
> > >   <date>2008-05-30</date>
>
> > > But I don't want any old date decided when I wrote the model, I would
> > > like it to be today's date. I can get that from substring(now(),1,10),
> > > but how to I set date to that? The only way I can think of is to do a
> > > setvalue after the instance has loaded. And I would have to set it
> > > only if the field is blank.
>
> > > Is there a simpler notation for what I want?
>
> > I think what you're looking for is something like this:
> > <xf:bind nodeset="//dateToday" type="xs:date" calculate="now()"/>
>
> > Today's date should appear in the input box.
>
> Sorry, I didn't ask if you knew how to do a bind; I'll just assume you
> may not.
>
> Put the above code in your model.  I always put my binds after my
> instance and submissions, but i think you can put them anywhere in the
> model. I set mine like so:
>
> <xf:model>
>     <xf:instance>
>       ...
>     </xf:instance>
>
>    <xf:submission>
>       ... if any
>    <xf:submission>
>
>    <xf:bind nodeset="//dateToday" type="xs:date" calculate="now()"/>
>    <xf:bind  ...
>
> </xf:model>
>
> of course, change "now()" to "substring(now(),1,10)" if you like.
>
> If you're using xf:setvalue or xf:output, then you can use
> value="substring(now(),1,10)" or you can just use <xf:output ref="//
> dateToday" />

Oh, good gosh. I always tell myself to just stop when I haven't slept
enough, but nooooo.....
I nearly steered you wrong.

Of course, if you bind your date element to any kind of calculation,
then you'll never be able to update the date element because it will
always immediately revert back again to the bound calculation.
So, try this instead:
your model/instance elements
   <date/>
   <date2/>

<xf:bind nodeset="//date" type="xs:date"  calculate="if( . ='', //
date2, .)"/>
<xf:bind nodeset="//date2" type="xs:date"  calculate="substring(now(),
1,10)"/>

<body>
...
<xf:input ref="//date">

 //date2 just holds the current date,

Be aware that the    =''   in the if statement if( . ='', //date2, .)
are two single quotes, not a single double quote.  Also, anyone
following this thread who has never used the dot (.) for self-node
annotation, the above if statement could be rewritten as if( //date
='',  //date2,  //date).

and I also apologize, I didn't see your earlier post, and obviously,
you know what a bind is.
_______________________________________________
dev-tech-xforms mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-xforms
Reply | Threaded
Open this post in threaded view
|

Re: how to initialize instance data with computed value?

Ken Yap-2
>    <date/>
>    <date2/>
>
> <xf:bind nodeset="//date" type="xs:date"  calculate="if( . ='', //
> date2, .)"/>
> <xf:bind nodeset="//date2" type="xs:date"  calculate="substring(now(),
> 1,10)"/>

Works great, thanks. I thought of using calculate but as you pointed
out, it makes the date always today, but I didn't think of using
another element to hold the current date.
_______________________________________________
dev-tech-xforms mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-xforms
Reply | Threaded
Open this post in threaded view
|

Re: how to initialize instance data with computed value?

Ken Yap-2
On Jun 1, 9:30 pm, Ken Yap <[hidden email]> wrote:

> >    <date/>
> >    <date2/>
>
> > <xf:bind nodeset="//date" type="xs:date"  calculate="if( . ='', //
> > date2, .)"/>
> > <xf:bind nodeset="//date2" type="xs:date"  calculate="substring(now(),
> > 1,10)"/>
>
> Works great, thanks. I thought of using calculate but as you pointed
> out, it makes the date always today, but I didn't think of using
> another element to hold the current date.

It turns out that I don't even need the element to hold today's date,
I can just use substring(now(),1,10) directly, must have got the
calculate wrong when I tried it. This also avoids issues with
evaluation order of today's date.

Furthermore I found out that a CSS rule of display:none has the same
effect as a width of 0pt. So here is the form I ended up with:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://
www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:xf="http://www.w3.org/
2002/xforms" xmlns:ev="http://www.w3.org/2001/xml-events"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://
www.w3.org/2001/XMLSchema-instance" xmlns:xlink="http://www.w3.org/
1999/xlink">
  <head>
    <title>Date form</title>
    <xf:model id="model">
      <xf:instance id="test">
        <test xmlns="">
          <date special="false" />
        </test>
      </xf:instance>
      <xf:bind nodeset="date" type="xsd:date" relevant="not(boolean-
from-string(@special))" calculate="if(.='',substring(now(),
1,10),instance('test')/date)" />
      <xf:bind nodeset="date/@special" type="xsd:boolean" />
    </xf:model>
    <style type="text/css">
.datefield .xf-value {display:none}
    </style>
  </head>
  <body>
    <xf:input ref="date/@special">
      <xf:label>See submission instructions</xf:label>
    </xf:input>
    <xf:input ref="date" incremental="true" class="datefield">
      <xf:label>Date</xf:label>
    </xf:input>
    <xf:output value="date" />
  </body>
</html>
_______________________________________________
dev-tech-xforms mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-xforms
Reply | Threaded
Open this post in threaded view
|

RE: how to initialize instance data with computed value?

Klotz, Leigh
You can also express the intent of the display:none hack more clearly by
doing this:

<style type="text/css">
 @namespace xf url(http://www.w3.org/2002/xforms);
 @namespace mozType
url(http://www.mozilla.org/projects/xforms/2005/type);
xf|input[appearance="compact"][mozType|typelist~="http://www.w3.org/2001
/XMLSchema#date"] .xf-value { display:none; }
</style>

...
    <xf:input ref="date" incremental="true" appearance="compact">
      <xf:label>Date</xf:label>
    </xf:input>


 

-----Original Message-----
From: dev-tech-xforms-bounces+leigh.klotz=[hidden email]
[mailto:dev-tech-xforms-bounces+leigh.klotz=[hidden email]]
On Behalf Of Ken Yap
Sent: Sunday, June 01, 2008 8:02 PM
To: [hidden email]
Subject: Re: how to initialize instance data with computed value?

On Jun 1, 9:30 pm, Ken Yap <[hidden email]> wrote:

> >    <date/>
> >    <date2/>
>
> > <xf:bind nodeset="//date" type="xs:date"  calculate="if( . ='', //
> > date2, .)"/> <xf:bind nodeset="//date2" type="xs:date"  
> > calculate="substring(now(), 1,10)"/>
>
> Works great, thanks. I thought of using calculate but as you pointed
> out, it makes the date always today, but I didn't think of using
> another element to hold the current date.

It turns out that I don't even need the element to hold today's date, I
can just use substring(now(),1,10) directly, must have got the calculate
wrong when I tried it. This also avoids issues with evaluation order of
today's date.

Furthermore I found out that a CSS rule of display:none has the same
effect as a width of 0pt. So here is the form I ended up with:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://
www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:xf="http://www.w3.org/
2002/xforms" xmlns:ev="http://www.w3.org/2001/xml-events"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://
www.w3.org/2001/XMLSchema-instance" xmlns:xlink="http://www.w3.org/
1999/xlink">
  <head>
    <title>Date form</title>
    <xf:model id="model">
      <xf:instance id="test">
        <test xmlns="">
          <date special="false" />
        </test>
      </xf:instance>
      <xf:bind nodeset="date" type="xsd:date" relevant="not(boolean-
from-string(@special))" calculate="if(.='',substring(now(),
1,10),instance('test')/date)" />
      <xf:bind nodeset="date/@special" type="xsd:boolean" />
    </xf:model>
    <style type="text/css">
.datefield .xf-value {display:none}
    </style>
  </head>
  <body>
    <xf:input ref="date/@special">
      <xf:label>See submission instructions</xf:label>
    </xf:input>
    <xf:input ref="date" incremental="true" class="datefield">
      <xf:label>Date</xf:label>
    </xf:input>
    <xf:output value="date" />
  </body>
</html>
_______________________________________________
dev-tech-xforms mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-xforms
_______________________________________________
dev-tech-xforms mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-xforms