BY Ajay Singh10 Jan 2022 Edit
WebAtoms - Form

This article provides you with the first experience of writing the simple basic Form. After that, we will show you how to do the simple validation and cover some of writing the CSS on this lightweight Form. We will cover all these things with some code snippets.

What is WebAtoms?

WebAtoms is an advanced MVVM framework to write cross-platform applications in HTML5 and Xamarin.Forms.

Why do we Need a Form?

A Form is the most vital aspect of the user-application interaction for gathering input from the user and storing it in the database.

A Form should contain basic validation to collect valid and accurate data from the user to provide a seamless experience for the user.

What is WebAtoms Form?

Earlier in the WebAtoms, we have defined the AtomForm as a class component. To validate the AtomForm, we have isolated the logic and validation part to the ViewModel section. We will guide you on how to write the simple Form using the WebAtoms Form component.

Form and FormField are javaScript functional components, they are nothing but simple javascript functions.

How to use the Form?

 <Form>
     <FormField 
               label="First Name"
               error={Bind.oneWay(() => 
               validate("first name", this.data.firstName,))}>
        <input 
               value={Bind.twoWaysImmediate(() =>
               this.data.firstName)} />
     </FormField>
</Form>

How to apply multiple validations?

const validate = (label: string, value: string, r?: RegExp) => {
    if (!value)
    {
        return `${label} is required`;
    }
    if (r) {
        if (!r.test(value)) {
            return `${label} is invalid`;
        }
    }
    return "";
};
const validateEmail = (label: string, value: string) =>
validate(label, value, /[a-z0-9\-\.]+\@[a-z0-9\-\.]+[a-z0-9\-]+/i);

We have described the validate function for input validation. validateEmail function validates the email using the regex. Below is the simple code snippet which shows the email validation.

 <FormField 
      label="Email Address"
      error={Bind.oneWay(() => validateEmail("Email Address", 
                 this.data.emailAddress))}>
       <input 
            value={Bind.twoWaysImmediate(() =>
            this.data.emailAddress)} />
  </FormField>

In the above example, we have just displayed a couple of simple validation. In the same manner, one can achieve complicated validation.

Benefit of WebAtoms Form.

  • Form component is very lightweight, meaning it is simple to implement.

  • In large applications, there is a requirement of having small UI components like having multiple Forms in a single form, so it becomes easier to implement the validation logic either in ViewModel or in View itself.

  • The error, label, and class attributes of FormField components help us write the neater and cleaner UI with complex validations.

  • You don't need to install any separate NPM library for building the simple form.

  • It will improve the speed of writing the code and also reduce the number of lines of code, it becomes easier to maintain the code.

Check out this example.

BY Ajay Singh
LikeCommentSave
LikeCommentSaveShare
0
Categories
General
YantraJS
Developer Guides
Tutorials
Web Atoms Updates

POPULAR POSTS
17 Mar 2021
LATEST ACTIVITY
Simmi Kava
liked this post.
Simmi Kava
liked this post.
Show more
ARCHIVES
2022
2021
2020
2019
TAGS
javascript (51)
developer (25)
javascriptdeveloper (16)
Xamarin.Forms (16)
webatoms (11)
xamarin (11)
coding (10)
typescript (10)
android (8)
arrays (8)
csharp (7)
dotnet (7)
javascript-developer (6)
update (6)
web-atoms (6)
dotnet-standard (5)
function (5)
Html (5)
iOS (5)
methods (4)
object (4)




Web Atoms: JSX (TSX + TypeScript) for Xamarin.Forms, Hot Reload Your App in Production Environment

PlaygroundSamples Repository