I have couple linked dropdowns and want to prevent form submit on change event if linked dropdown is empty.
<form id="foo">
<select name="first" on="change:AMP.setState({settings: {first: event.value}}),foo.submit" >
...
</select>
<select name="second" on="change:AMP.setState({settings: {second: event.value}}),foo.submit" >
<option value=0> Please select </option>
...
</select>
</form>
Can i check state and then submit form if the value is appropriate? Something like:
<select name="first" on="change:AMP.setState({settings: {first: event.value}}),settings.second ? foo.submit : nothing to do" >
for the first dropdown to avoid form submit when I select new value, because second not welected yet.
Tried make it with validation, but it not work with dropdown, input works fine.
<input type="text"
id="show-first-on-submit-name"
name="name"
placeholder="Digit..."
required
on="change:AMP.setState({settings: {testing: event.value}}),requirements-form.submit"
pattern="^([1-9][0-9]*)$">
<span visible-when-invalid="valueMissing"
validation-for="show-first-on-submit-name"></span>
<span visible-when-invalid="patternMismatch"
validation-for="show-first-on-submit-name">
Please enter not '0'
</span>
<select name="purpose"
id="purpose"
pattern="^([1-9][0-9]*)$"
required
on="change:AMP.setState({settings: {purpose: event.value}}),requirements-form.submit">
<option value="0">Please select</option>
<option value="1">One</option>
</select>
<span visible-when-invalid="valueMissing"
validation-for="purpose">missing</span>
<span visible-when-invalid="patternMismatch"
validation-for="purpose">wrong</span>
Validating form by converting input status into an
amp-state
object and checking them when change event occurs is a little circuitous.In my opinion it would be more easier to use HTML5 Form validation API (AMP use it as well) to handle input validation works. All you need to do is add
required
attribute to your<select>
element and removeonChange
validation logic.Here is a simple example:
Example screenshot: