red-tape/initial-data/index.html @ 1f003474d0d9

d: Update site.
author Steve Losh <steve@stevelosh.com>
date Mon, 28 Mar 2016 17:51:43 +0000
parents f1d02f8944b8
children 69831b3947d7
<html><head><title>Initial Data / Red Tape</title><link rel="stylesheet" href="../_dmedia/bootstrap.css"/><link rel="stylesheet" href="../_dmedia/tango.css"/><link rel="stylesheet/less" type="text/css" href="../_dmedia/style.less"/><script src="../_dmedia/less.js" type="text/javascript">
        </script></head><body class="content">
        <div class="wrap">
            <header><h1><a href="..">Red Tape</a></h1></header><div class="markdown">
<h1 id="initial-data"><a href="">Initial Data</a></h1><p>Sometimes you want to provide sane defaults for fields in a form.  To do this
you can use the <code>:initial</code> entry in the <code>defform</code> option map.  For example:</p>
<div class="codehilite"><pre><span class="p">(</span><span class="nf">defform</span> <span class="nv">sample-form</span> <span class="p">{</span><span class="ss">:initial</span> <span class="p">{</span><span class="ss">:email</span> <span class="s">"user@example.com"</span><span class="p">}}</span>
  <span class="ss">:email</span> <span class="p">[]</span>
  <span class="ss">:message</span> <span class="p">[])</span>
</pre></div>


<p>When creating a fresh form, this initial data will populate the <code>:data</code> map in
the result instead of the default empty string:</p>
<div class="codehilite"><pre><span class="p">(</span><span class="nf">sample-form</span><span class="p">)</span>
<span class="c1">; =&gt;</span>
<span class="p">{</span><span class="ss">:fresh</span> <span class="nv">true</span>
 <span class="ss">:results</span> <span class="nv">nil</span>
 <span class="ss">:data</span> <span class="p">{</span><span class="ss">:email</span> <span class="s">"user@example.com"</span>
        <span class="ss">:message</span> <span class="s">""</span><span class="p">}</span>
 <span class="nv">...</span><span class="p">}</span>
</pre></div>


<p>When you run some data through the form, the initial data is ignored completely:</p>
<div class="codehilite"><pre><span class="p">(</span><span class="nf">sample-form</span> <span class="p">{</span><span class="ss">:email</span> <span class="s">"foo@bar.com"</span>
              <span class="ss">:message</span> <span class="s">"hi"</span><span class="p">})</span>
<span class="c1">; =&gt;</span>
<span class="p">{</span><span class="ss">:fresh</span> <span class="nv">false</span>
 <span class="ss">:results</span> <span class="p">{</span><span class="ss">:email</span> <span class="s">"foo@bar.com"</span>
           <span class="ss">:message</span> <span class="s">"hi"</span><span class="p">}</span>
 <span class="ss">:data</span>    <span class="p">{</span><span class="ss">:email</span> <span class="s">"foo@bar.com"</span>
           <span class="ss">:message</span> <span class="s">"hi"</span><span class="p">}</span>
 <span class="nv">...</span><span class="p">}</span>
</pre></div>


<p>This means when you're rendering a form field to HTML you can simply pull the
field's entry from the <code>:data</code> map and fill the field with its contents.  It
doesn't matter whether it happened to be a fresh form or a form that had some
errors.</p>
<p>Note that initial data should always be specified as a string, since the <code>:data</code>
map will always contain strings once you've run data through it.</p>
<p>Move on to the <a href="../form-arguments/">form arguments</a> guide next.</p>
                </div>
            <footer><p>Made and <a href="http://sjl.bitbucket.org/d/">documented</a> with love by <a href="http://stevelosh.com">Steve
Losh</a>.</p>
<p><br/><a id="rochester-made" href="http://rochestermade.com" title="Rochester Made"><img src="http://rochestermade.com/media/images/rochester-made-dark-on-light.png" alt="Rochester Made" title="Rochester Made"/></a></p></footer></div>
    </body></html>