Develop a form in one minute
Tags: vue, javascript, form, json
Published: 2019-10-17 08:52:31


![image](http://upload-images.jianshu.io/upload_images/2195795-fe69d968e77801c2?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

The previous article "[How to choose a open source project that generate forms from a JSON schema](https://dev.to/danieldx/form-generation-scheme-from-a-json-schema-2b0e)" I recommend ncform, today I will talk about how to use ncform for development form fast.    
<(￣︶￣)>。

The form is actually a visual description of a data, presented to the user in a friendly way, to achieve the purpose of collecting information filled by the user.

Today, let's put aside the traditional form development way to understand the new and efficient way to develop forms (after reading, you may be able to get rid of the boring form development career)  
╰(￣▽￣)╭

---

- Form development first step: know your data structure

The following is the data structure of the form to be developed today, not complicated, but not simple

```
{
  firstname: 'daniel',
  lastname: 'xiao',
  fullname: 'daniel.xiao',
  gender: 'man',
  language: [ 'english', 'chinese' ],
  birthday: '',
  luckyNum: 9,
  luckyColor: '',
  email: 'danieldx666@126.com',
  favoriteMusics: [
    {
      type: '',
      score: 5
    }
  ],
  remarks: ''
}
```

---

- Form development second step: generate ncform schema

We can quickly generate the basic structure of the ncform schema through the ncform schema generator.

![](https://camo.githubusercontent.com/70996cb0290a7bfdbc8b8db48f11b382a2a19c6a/68747470733a2f2f757365722d676f6c642d63646e2e786974752e696f2f323031392f332f32322f313639613333333162303436366336373f773d3133323926683d35313926663d67696626733d313933343037)

OK, in less than a few seconds, a form is generated.

Of course, this form is now ready to use, but it's too simplistic, let's optimize it. <(￣︶￣)↗[GO!]

> ncform provides a playground, you can copy the generated ncform schema to the playground for optimization. The following demos are all performed at the playground.

---

- Optimize the name field: let users fill in as little as possible

![](https://user-gold-cdn.xitu.io/2019/3/22/169a352d7c7191f6?w=855&h=299&f=gif&s=88138)

```js
// The following is the configuration information of the modified related fields.
    "firstname": {
      "type": "string",
      "ui": {
        "label": "First Name",
        "columns": 6
      }
    },
    "lastname": {
      "type": "string",
      "ui": {
        "label": "Last Name",
        "columns": 6
      }
    },
    "fullname": {
      "type": "string",
      "valueTemplate": "dx: {{$root.firstname}} + '.' + {{$root.lastname}}",
      "ui": {
        "label": "Full Name"
      }
    },
```
---

- Optimize the gender field: pick one of two, the best choice is radio

![](https://user-gold-cdn.xitu.io/2019/3/22/169a3a53dd452712?w=855&h=299&f=gif&s=107407)

```js
// The following is the configuration information of the modified related fields.
    "gender": {
      "type": "string",
      "default": "man",
      "ui": {
        "label": "Gender",
        "widget": "radio",
        "widgetConfig": {
            "enumSource": [
              {
                  "value": "man",
                  "label": "Man"
              },
             {
                  "value": "woman",
                  "label": "Woman"
              }
            ]
        }
      }
    },
```
---

- Optimize the language field: not too many items and support multiple choices, the check box is a good choice

![](https://user-gold-cdn.xitu.io/2019/3/22/169a3af1d5247500?w=855&h=299&f=gif&s=82719)

```js
// The following is the configuration information of the modified related fields.
    "language": {
      "type": "array",
      "ui": {
        "label": "Language",
        "widget": "checkbox",
        "widgetConfig": {
            "enumSource": [
                {
                    "label": "English",
                    "value": "eng"
                },
                {
                    "label": "Chinese",
                    "value": "cn"
                }
            ]
        }
      }
    },
```

---
 
- Optimize the birthday field: date we will use the date picker

![](https://user-gold-cdn.xitu.io/2019/3/22/169a3b8910f18030?w=855&h=299&f=gif&s=66192)

```js
// The following is the configuration information of the modified related fields.
    "birthday": {
      "type": "string",
      "ui": {
        "label": "Birthday",
        "widget": "date-picker"
      }
    },
```
---

- Optimize luckyNum and luckyColor: for numbers and colors, there are corresponding widgets

![](https://user-gold-cdn.xitu.io/2019/3/22/169a3cee12d6f0d9?w=855&h=299&f=gif&s=107758)

```js
// The following is the configuration information of the modified related fields.
    "luckyNum": {
      "type": "number",
      "ui": {
        "label": "Lucky Num",
        "widget": "input-number"
      }
    },
    "luckyColor": {
      "type": "string",
      "ui": {
        "label": "lucky Color",
        "widget": "color-picker"
      }
    },
```

---

- Optimize the email field: For email, it is necessary to add a email format validation rule. Let's add another rule - required.

![](https://user-gold-cdn.xitu.io/2019/3/22/169a3f6e0e6a7fb4?w=855&h=299&f=gif&s=68953)

```js
// The following is the configuration information of the modified related fields.
    "email": {
      "type": "string",
      "ui": {
        "label": "Email"
      },
      "rules": {
          "required": true,
          "email": true
      }
    },
```
---

- Optimize the favoriteMusics field: I think the table is quite nice.
- Optimize the type field: There are many items in the music type, so it's better to use the drop-down box
- Optimize the score field: give a few stars feels good

![](https://user-gold-cdn.xitu.io/2019/3/22/169a4120339fa21f?w=855&h=299&f=gif&s=252975)

```js
// The following is the configuration information of the modified related fields.
    "favoriteMusics": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "type": {
            "type": "string",
            "ui": {
              "label": "Type",
              "widget": "select",
              "widgetConfig": {
                  "enumSource": [
                    {
                        "value": "1",
                        "label": "Pop Music"
                    },
                    {
                        "value": "2",
                        "label": "Rock Music"
                    }
                  ]
              }
            }
          },
          "score": {
            "type": "number",
            "ui": {
              "label": "score",
              "widget": "rate"
            }
          }
        },
        "ui": {
          "label": "favoriteMusics"
        }
      },
      "ui": {
        "label": "favoriteMusics",
        "showLegend": false,
        "widget": "array-table"
      }
    },
```
---

- Optimize the remarks field: people who write notes may write more, so the textarea is better.

![](https://user-gold-cdn.xitu.io/2019/3/22/169a41a9a3f6956e?w=855&h=299&f=gif&s=45901)

```js
// The following is the configuration information of the modified related fields.
    "remarks": {
      "type": "string",
      "ui": {
        "label": "remarks",
        "widget": "textarea"
      }
    }
```

---

Now, the ncform schema for this form has been finished, let’s take a big photo. ♪(^∇^*)

![](https://user-gold-cdn.xitu.io/2019/3/22/169a428cc1e3ba07?w=656&h=299&f=gif&s=221931)

---

Advertising time: ncform v1.0.0 officially released ( Star the repo to support ncform if you like it. O (∩ _ ∩) O)

Main update features:
1. Fully tested: This version incorporates a large number of automated tests to ensure the quality of the project.
2. Automatic support for dx expressions: User-defined widget's widgetConfig automatically supports dx expressions, which is more friendly to developers.

The relevant link:

- [ncform](https://github.com/ncform/ncform)
- [ncform schema generator](https://ncform.github.io/ncform/ncform-show/schema-gen/index.html)
- [ncform playground](https://ncform.github.io/ncform/ncform-show/playground/index.html)
- [ncform standard compoments](https://ncform.github.io/ncform/ncform-theme-elementui/index.html)

Thanks for reading, bye!

---

tags: vue, json-schema, form, generator