Using if/else on a form's edit-text

What if the edit text widget is left blank

<$edit-text field=“transaction.verb.number” default=<<now v#YYYY0MM0DDhh0mm0ss>> value=<<now v#YYYY0MM0DDhh0mm0ss>> placeholder=<<now v#YYYY0MM0DDhh0mm0ss>> class=“full”/>

How can I use the edit text widget as a variable (to display/use its value elsewhere) that holds a costumed time widget value as long as the edit-text widget remains blank; and its value changes if the user overrides it?

1 Like

You wouldn’t ever really be (technically) using the edit text widget as a variable. (The edit text widget doesn’t itself hold data — it’s just an interface that makes it easier for users to add and interact with field values.)

But whatever variable you use to display within an edit-text widget can also be used elsewhere (including conditionally).

Placeholder text (like default text) can be determined by a variable or macro (such as the now macro), and that same variable or macro can be used elsewhere in a conditional — so if the field is empty, you use that now macro value; but any user-specified value overrides that value.

If you have a placeholder value, this is just a matter of what displays (usually in muted color) in the edit-text widget. Placeholder text instantly evaporates as soon as the user starts typing. The field (transaction.verb.number in this case) remains blank until the user interacts with it.

With a default value, even that displays only as long as the field/tiddler in question doesn’t yet exist. It doesn’t get written into the field until/unless the user adds further keystrokes to the field (or some other action puts the value into the field). The difference is that the user’s input “begins with” that default value as something to modify.

Default values are good for data-entry situations where there’s a template, but the user is expected to type in further specific information (often for a section that is pre-selected with a focusSelectFromEnd parameter, or similar).

Note, if you really want to invite the user to accept the whole default timestamp-based value, I suggest making a button to set the field so that the timestamp-based string (exactly that string) becomes the field value. (See below follow-up)

Whatever you choose as a method of getting the field value set, you then just need a conditional display. This could look something like this:

<% if [<currentTiddler>has[transaction.verb.number]] %>
entered value: {{!!transaction.verb.number}}
<% else %>
default value: <<now v#YYYY0MM0DDhh0mm0ss>>
<% endif %>
1 Like

Just a small further note:

In your actual tiddlywiki, this code can get you into trouble because it has “smart” or “curly” quotes. It’s possible that these were added by accident somewhere along the way.

But if your edit text widget specifies field=“transaction.verb.number” then you’ll end up with a field called “transaction.verb.number” (and your css will look for a class called “full” etc.)

So what you need is more like:

transaction.verb.number: <$edit-text 
field="transaction.verb.number" 
default=<<now v#YYYY0MM0DDhh0mm0ss>> 
placeholder=<<now v#YYYY0MM0DDhh0mm0ss>> 
class="full"/> 

<% if [<currentTiddler>has[transaction.verb.number]] %><br>
(Note `transaction.verb.number` has an existing value: {{!!transaction.verb.number}})
<% endif %>

<$button>
<$action-setfield $field="transaction.verb.number" $value=<<now v#YYYY0MM0DDhh0mm0ss>> />
set `transaction.verb.number` to <<now v#YYYY0MM0DDhh0mm0ss>>
</$button>

In addition to “straightening” the quote-marks, I’ve removed the “value” attribute from your version of the edit-text widget (since it is not a legit attribute for edit-text — it makes sense only for the setfield action) and also the placeholder attribute, because placeholder is redundant if you already have a default value. Default is a “stronger” attribute than placeholder.*

*EDIT/CORRECTION to my mistaken claim about redundancy The default attribute shows only if the relevant tiddler/field does not exist. Once it exists, default value will not display, even if the field/tiddler in question is blank. (This will happen if someone enters the edit-text widget, erases the default value, and then does nothing else, so the field gets created, but is empty.) So, the placeholder attribute — which appears even for existing but blank fields — is not simply redundant. It can still be helpful as a backup for that empty-field situation, as a visual reminder of what an example/suggested value is.)

One problem with having a default value is that it’s not as obvious to the user that the field will not even be created until they interact with it (unlike placeholder, which is visually clear as a “ghost” value). So, if you use default attribute, you may want a separate way to make it visually clear whether the field has a value already. So the conditional-shortcut <% if ... %> note above is designed to help with that problem.

I’ve also added a button to just set the field to its preferred default value.

So you should be able to paste all this into your solution, see what you like, and come back if you need help understanding how to modify it to fit your needs.

2 Likes

suppose that snippet was part of a form that was using a temp tiddler to hold that data; how/where could I specify?

tiddler="$:/temp/journal.entry.new"

The <$edit-text> widget (see documentation here) also has a tiddler attribute which can be specified exactly as you wrote in your post:

So modify your code block to start like this (order of attributes doesn’t matter, but I always like to put tiddler right before field):

transaction.verb.number: <$edit-text 
tiddler="$:/temp/journal.entry.new" 
field="transaction.verb.number" 
default=<<now v#YYYY0MM0DDhh0mm0ss>> 
placeholder=<<now v#YYYY0MM0DDhh0mm0ss>> 
class="full"/>