Skip to content

Document variable substitutions in hidden fields in forms#6486

Merged
mkhatib merged 3 commits intoampproject:masterfrom
mkhatib:form-var-sub-docs
Dec 16, 2016
Merged

Document variable substitutions in hidden fields in forms#6486
mkhatib merged 3 commits intoampproject:masterfrom
mkhatib:form-var-sub-docs

Conversation

@mkhatib
Copy link
Copy Markdown
Contributor

@mkhatib mkhatib commented Dec 5, 2016

Closes #6420

@rudygalfi
Copy link
Copy Markdown
Contributor

A couple comments, one about implementation (sorry).

cc @bpaduch to make sure we're accounting for form analytics as part of our overall analytics documentation (along with other specialized cases like amp-carousel, amp-access, etc).

@mkhatib
Copy link
Copy Markdown
Contributor Author

mkhatib commented Dec 5, 2016

@rudygalfi You might need to submit your review for me to see the comments.

## Variable Substitutions
__(<a href="https://www.ampproject.org/docs/reference/experimental.html">experimental</a>)__

`amp-form` allows [variable substitutions](../../spec/amp-var-substitutions.md) for inside hidden inputs with `default-value` attribute. On each submission, `amp-form` finds all `input[type=hidden]` inside the form and apply variable substitutions to `default-value` attribute and fill the result in the input `value`.
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rewrite as follows:

amp-form allows variable substitutions for inputs that are hidden and that have the default-value attribute. On each form submission, amp-form finds all input[type=hidden] inside the form and applies variable substitutions to the default-value attribute and fills in the result for the input value.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

<tr>
<td width="40%"><strong>Availability</strong></td>
<td>Stable<br>(<a href="#custom-validations">Custom Validation still experimental - See below</a>)</td>
<td>Stable<br>Experimental Features:
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rewrite as follows:
Stable with the following Experimental features:

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done


`amp-form` allows [variable substitutions](../../spec/amp-var-substitutions.md) for inside hidden inputs with `default-value` attribute. On each submission, `amp-form` finds all `input[type=hidden]` inside the form and apply variable substitutions to `default-value` attribute and fill the result in the input `value`.

Here's an example of how inputs are before and after substitutions:
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this example, it shows using variable substitution for "Platform Variables" (variable substitutions doc lists Platform & Analytics vars). Does variable substitution in forms only apply to platform vars or can they substitute in any of the analytics vars? If there's a limitation, please document it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This only exposes platform variables, I believe analytics variables are only exposed through analytics. Will update to reflect.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

</form>
```

Once the user tries to submit the form, AMP will try to resolve the variables and update the fields values. For XHR submissions, all variables are likely to be substituted and resolved. However, in non-XHR GET submissions, values that requires async-resolution might not be available and might not be resolved if it has not previously been resolved. `CLIENT_ID` for example would not resolve if it wasn't resolved and cached previously.
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

change "fields" to "fields'"

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

## Variable Substitutions
__(<a href="https://www.ampproject.org/docs/reference/experimental.html">experimental</a>)__

`amp-form` allows [variable substitutions](../../spec/amp-var-substitutions.md) for inside hidden inputs with `default-value` attribute. On each submission, `amp-form` finds all `input[type=hidden]` inside the form and apply variable substitutions to `default-value` attribute and fill the result in the input `value`.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, this comment should have been in the implementation change, but wanted to ask here. Would an attribute name like data-value-for-substitution be better here. There's two question to unpack:
(a) Is using data- attributes a better approach here. (I'm not sure where to use them and when not.)
(b) "default" suggests to me something that's defined (in fact, it's the value used if another one doesn't come along to replace it). Here, however, what's really being constructed is a tokenized string. I wonder if we can make the attribute name clearer.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am happy to change this, default-value was a quick suggestion from @cramforce and I took it and ran with it 😄.

(a) Is using data- attributes a better approach here. (I'm not sure where to use them and when not.)

Me and you both. I am never sure whether to choose to go with data- or not. I'd love for us to decide on a better way to choose and set of rules to follow when evaluating attributes naming.

(b) "default" suggests to me something that's defined (in fact, it's the value used if another one doesn't come along to replace it). Here, however, what's really being constructed is a tokenized string. I wonder if we can make the attribute name clearer.

We could rename default-value to resolve-value or something clearer, value-for-substitution also sounds weird but might be clearer.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I changed my mind. Lets just use value instead of default-value. Could this be implemented in terms of a modified urlReplacements…maybeExpandLink? And then use the same data- attribute to opt into replacement

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah sure, would this look like the following:

<input value="can: CANONICAL_URL, rand: RANDOM" data-amp-replace="CANONICAL_URL RANDOM">

amp-form would then call a modified maybeExpandLink with the expected attribute to replace (value in this case and href in links case).

@rudygalfi @cramforce does this sound better?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SG

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Should data-amp-replace be a comma separated list?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should change it to be space separated because that is more idiomatic on the web.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sent out #6621 to update the format we're doing here. PTAL there and I'll update docs PR after we get it in.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

</form>
```

Once the user tries to submit the form, AMP will try to resolve the variables and update the fields values. For XHR submissions, all variables are likely to be substituted and resolved. However, in non-XHR GET submissions, values that requires async-resolution might not be available and might not be resolved if it has not previously been resolved. `CLIENT_ID` for example would not resolve if it wasn't resolved and cached previously.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AMP will try to resolve the variables and update the value attribute of all fields with the appropriate substitutions.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

might not be available due to having not been resolved previously.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@mkhatib
Copy link
Copy Markdown
Contributor Author

mkhatib commented Dec 14, 2016

PTAL 👀

<td>Stable with the following Experimental features:
<ul>
<li><a href="#custom-validations">Custom Validation</a></li>
<li><a href="#variable-substitution">Variable Substitutions</a></li>
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing s: variable-substitutions

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done


`amp-form` allows [platform variable substitutions](../../spec/amp-var-substitutions.md) for inputs that are hidden and that have the `data-amp-replace` attribute. On each form submission, `amp-form` finds all `input[type=hidden][data-amp-replace]` inside the form and applies variable substitutions to its `value` attribute and replaces it with the result of the substitution.

It's important to provide the variables you are using for each substitution on each input through a space-separated strings in `data-amp-replace`, see examples below. AMP will not replace variables that are not explicitly provided.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this be cleaned up to: You must provide the variables you are using for each substitution on each input by specifying a space-separated string of the variables used in data-amp-replace (see example below). AMP will not replace variables that are not explicitly specified.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@rudygalfi
Copy link
Copy Markdown
Contributor

LGTM, thanks!

@mkhatib mkhatib merged commit 7d7b1cc into ampproject:master Dec 16, 2016
@mkhatib mkhatib deleted the form-var-sub-docs branch December 16, 2016 20:59
Lith pushed a commit to Lith/amphtml that referenced this pull request Dec 22, 2016
Lith pushed a commit to Lith/amphtml that referenced this pull request Dec 22, 2016
torch2424 pushed a commit to torch2424/amphtml that referenced this pull request Jan 3, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants