Skip to content

Form validation and submit flow  #55

@sashafirsov

Description

@sashafirsov

Form validation flow

Each form field on change can trigger either native ( by element implementation ) or DCE event-driven declarative "validation", i.e. DOM change according to input validity.

Alternatively, the "change" event is propagated from form inputs to the form element itself.

This is a natural choice for validation trigger on the form level and propagate the formData when slice is defined on the form element.

<form slice="my-form" >...

would use new FormData(formElement) as value for the form. The serializer should encode the <form-data> XML to be passed to http-request component.

<form slice="signin-form">
                    <label> Enter your email, phone, or user name
                        <input name="username" autocomplete="username"
                            placeholder="Email, phone, or username"
                        />
                    </label>
                    <fieldset>
                        <legend>Confirm by</legend>
                        <label><input type="radio" name="confirm-by" value="email"      /> email         </label>
                        <label><input type="radio" name="confirm-by" value="sms"        /> text to phone </label>
                        <label><input type="radio" name="confirm-by" value="password"   /> password      </label>
                        <if test="/datadom/slice/signin-form/form-data/confirm-by = 'password'">
                            <label>Enter password: <input type="password"/> </label>
                        </if>                      
                    </fieldset>
                    <button>Sign In</button>
                </form>

In the given example the password input would appear matching radio selection. During the selection change, the /datadom/slice/signin-form/form-data/confirm-by or simply //form-data/confirm-by if filled by the form change event handler.

custom-validity attribute

The DOM changes are not the usual outcome of validation. HTML5 has dedicated styling and special properties associated with form input elements.
image

The input form element has several read-only properties and methods
available: validationMessage, validity, etc. To set the validity the only available method is setCustomValidity(message).

custom-validity attribute would set the message as an XPath expression by API ^^. It would require an extra post-render step with traversing of whole DOM or altering the DOM + rendered template merging algorithm.

    <form slice="signin-form">
         <variable name="warn"><if test="string-length(/datadom/slice/signin-form/form-data/username) &lt; 10 ">
                            user name or email should be longer of 10 symbols
             </if></variable>
         <input name="username" 
                     custom-validity="$warn" />                    
    </form>

Q. How to allow the default validity values along with custom ones?

Setting custom validity to blank would(?) eliminate the defaults. Setting to the particular value would override default ones also.

Wish: expose the validity form field property of ValidityState type.

Perhaps as part of the event source element on the slice.

should-submit form attribute for the form submit prevention

should-submit attribute would evaluate its value as XPath expression. The value would be

  • preserved in event data to be used in UI as generic form event message
  • evaulation to boolean ( i.e. not empty) would be used as return value for submit event.

Usually the form level validation is triggered by form submission flow.

The `change` event is available on the `FORM` element
<script type="module" >
    document.querySelector('form').addEventListener('change', (event) => {
        console.log(`Form element ${event.target.name} has changed.`);
    });
</script>
In order to prevent submission on invalid form data, the `false` should be returned by the `submit` event handler. ( MDN link? )

What if the error is already covered by input field and no need for separate form level message?

The boolean false value of XPath from should-submit can serve the indicator of error without the form level message. The UI should check this value from the form event slice and hide the message for false case, otherwise display the message.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions