Working with form in Elm without storing raw form state in Elm, aka "Use the Platform".
Before
value someValue to set form field value when we render a form (e.g. edit form)someValue in sync with user's inputonInput msg to update someValueString on behalf of a Time.Posix model stateAfter
defaultValue someValue to set form field value when we render a form (e.g. edit form)someValue in sync with user's inputNativeForm.decoder anytime to retrieve the form values from browser document.formsonInput nor a msg for every form field; only do so for fields that really need handlingid attribute and form fields must be given a name attributeCheckout the demo at https://nativeform.netlify.app and its source code at example subdirectory.
Pass the browser document.forms into your Elm app through Flags
<script>
var app = Elm.Main.init({
node: document.getElementById('myapp'),
flags: {
documentForms: document.forms // <-- important!
}
})
</script>Store the documentForms : Json.Encode.Value from your Flags in your Model
Wire up any (or many) events, e.g. form [ on "change" OnFormChange ] or input [ onInput (always OnFormChange) ]
And call Json.Decode.decodeValue (NativeForm.decoder formId) model.documentForms to get the list of form field name and values.
No matter how many fields you have, you only need one Msg
Always give your form an id value
Always give your form fields a name value
Does not handle input[type=file] in a useful manner; manage file uploads by other means.
All user input values are either OneValue String or ManyValues (List String), including input[type=number]; do your own conversion to Int or Time.Posix etc.
When using this library, your form id and field names are "stringly typed". You should use a custom type to manage a fixed set of form fields, but this library does not require you to do so.
Do not deal with the List (String, Value) returned from NativeForm.decoder. Instead, you should parse them into a value of your desired type. e.g.
parseDontValidate :
List ( String, NativeForm.Value )
-> Result String UserInfo