# Form
### Install
```js
import Vue from 'vue';
import { Form } from 'vant';
Vue.use(Form);
```
## Usage
### Basic Usage
```html
Submit
```
```js
export default {
data() {
return {
username: '',
password: '',
};
},
methods: {
onSubmit(values) {
console.log('submit', values);
},
},
};
```
### Validate Rules
```html
Submit
```
```js
import { Toast } from 'vant';
export default {
data() {
return {
value1: '',
value2: '',
value3: '',
pattern: /\d{6}/,
};
},
methods: {
validator(val) {
return /1\d{10}/.test(val);
},
asyncValidator(val) {
return new Promise(resolve => {
Toast.loading('Validating...');
setTimeout(() => {
Toast.clear();
resolve(/\d{6}/.test(val));
}, 1000);
});
},
onFailed(errorInfo) {
console.log('failed', errorInfo);
},
},
};
```
### Field Type - Switch
```html
```
```js
export default {
data() {
return {
switchChecked: false,
};
},
};
```
### Field Type - Checkbox
```html
Checkbox 1
Checkbox 2
```
```js
export default {
data() {
return {
checkbox: false,
checkboxGroup: [],
};
},
};
```
### Field Type - Radio
```html
Radio 1
Radio 2
```
```js
export default {
data() {
return {
radio: '1',
};
},
};
```
### Field Type - Stepper
```html
```
```js
export default {
data() {
return {
stepper: 1,
};
},
};
```
### Field Type - Rate
```html
```
```js
export default {
data() {
return {
rate: 3,
};
},
};
```
### Field Type - Slider
```html
```
```js
export default {
data() {
return {
slider: 50,
};
},
};
```
### Field Type - Uploader
```html
```
```js
export default {
data() {
return {
uploader: [{ url: 'https://img.yzcdn.cn/vant/leaf.jpg' }],
};
},
};
```
### Field Type - Picker
```html
```
```js
export default {
data() {
return {
value: '',
columns: ['Delaware', 'Florida', 'Georqia', 'Indiana', 'Maine'],
showPicker: false,
};
},
methods: {
onConfirm(value) {
this.value = value;
this.showPicker = false;
},
},
};
```
### Field Type - DatetimePicker
```html
```
```js
export default {
data() {
return {
value: '',
showPicker: false,
};
},
methods: {
onConfirm(time) {
this.value = time;
this.showPicker = false;
},
},
};
```
### Field Type - Area
```html
```
```js
export default {
data() {
return {
value: '',
showArea: false,
areaList: {},
};
},
methods: {
onConfirm(values) {
this.value = values.map(item => item.name).join('/');
this.showArea = false;
},
},
};
```
### Field Type - Calendar
```html
```
```js
export default {
data() {
return {
value: '',
showCalendar: false,
};
},
methods: {
onConfirm(date) {
this.value = `${date.getMonth() + 1}/${date.getDate()}`;
this.showCalendar = false;
},
},
};
```
## API
### Props
| Attribute | Description | Type | Default |
|------|------|------|------|
| label-width | Field label width | *number \| string* | `90px` |
| label-align | Field label align, can be set to `center` `right` | *string* | `left` |
| input-align | Field input align, can be set to `center` `right` | *string* | `left` |
| error-message-align | Error message align, can be set to `center` `right` | *string* | `left` |
| validate-trigger `v2.5.2` | When to validate the form,can be set to `onChange`、`onSubmit` | *string* | `onBlur` |
| colon | Whether to display `:` after label | *boolean* | `false` |
| validate-first | Whether to stop the validation when a rule fails | *boolean* | `false` |
| scroll-to-error `v2.5.2` | Whether to scroll to the error field when validation failed | *boolean* | `false` |
| show-error `v2.5.9` | Whether to highlight input when validation failed | *boolean* | `true` |
| show-error-message `v2.5.8` | Whether to show error message when validation failed | *boolean* | `true` |
### Data Structure of Rule
| Key | Description | Type |
|------|------|------|
| required | Whether to be a required field | *boolean* |
| message `v2.5.3` | Error message | *string \| (value, rule) => string* |
| validator `v2.5.3` | Custom validator | *(value, rule) => boolean \| Promise* |
| pattern `v2.5.3` | Regex pattern | *RegExp* |
| trigger `v2.5.2` | When to validate the form,can be set to `onChange`、`onBlur` | *string* |
| formatter `v2.5.3` | Format value before validate | *(value, rule) => any* |
### Events
| Event | Description | Arguments |
|------|------|------|
| submit | Triggered after submitting the form and validation passed | *values: object* |
| failed | Triggered after submitting the form and validation failed | *errorInfo: { values: object, errors: object[] }* |
### Methods
Use [ref](https://vuejs.org/v2/api/#ref) to get Form instance and call instance methods
| Name | Description | Attribute | Return value |
|------|------|------|------|
| submit | Submit form | - | - |
| validate | Validate form | *name?: string* | *Promise* |
| resetValidation | Reset validation | *name?: string* | - |
| scrollToField `v2.5.2` | Scroll to field | *name: string* | - |
### Slots
| Name | Description |
| ---- | ----------- |
| default | Form content |