Document variable substitutions in hidden fields in forms#6486
Document variable substitutions in hidden fields in forms#6486mkhatib merged 3 commits intoampproject:masterfrom
Conversation
|
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). |
|
@rudygalfi You might need to submit your review for me to see the comments. |
extensions/amp-form/amp-form.md
Outdated
| ## 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`. |
There was a problem hiding this comment.
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.
extensions/amp-form/amp-form.md
Outdated
| <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: |
There was a problem hiding this comment.
Rewrite as follows:
Stable with the following Experimental features:
extensions/amp-form/amp-form.md
Outdated
|
|
||
| `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: |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
This only exposes platform variables, I believe analytics variables are only exposed through analytics. Will update to reflect.
extensions/amp-form/amp-form.md
Outdated
| </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. |
extensions/amp-form/amp-form.md
Outdated
| ## 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`. |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
LGTM. Should data-amp-replace be a comma separated list?
There was a problem hiding this comment.
I think we should change it to be space separated because that is more idiomatic on the web.
There was a problem hiding this comment.
Sent out #6621 to update the format we're doing here. PTAL there and I'll update docs PR after we get it in.
extensions/amp-form/amp-form.md
Outdated
| </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. |
There was a problem hiding this comment.
AMP will try to resolve the variables and update the value attribute of all fields with the appropriate substitutions.
There was a problem hiding this comment.
might not be available due to having not been resolved previously.
|
PTAL 👀 |
extensions/amp-form/amp-form.md
Outdated
| <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> |
There was a problem hiding this comment.
missing s: variable-substitutions
extensions/amp-form/amp-form.md
Outdated
|
|
||
| `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. |
There was a problem hiding this comment.
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.
|
LGTM, thanks! |
Closes #6420