naive-ui/demo/documentation/components/form/enUS/index.md

92 lines
4.6 KiB
Markdown
Raw Normal View History

2019-11-01 19:01:55 +08:00
# Form
<!--single-column-->
2020-02-05 12:14:05 +08:00
Collect & validate data.
## Demos
2019-11-01 19:01:55 +08:00
```demo
inline
custom-rule
2020-05-29 11:22:07 +08:00
custom-validation
2020-02-05 12:14:05 +08:00
top
left
2020-02-24 15:31:03 +08:00
async
2020-02-05 12:14:05 +08:00
```
## Props
### Form Props
|Name|Type|Default|Description|
|-|-|-|-|
|inline|`boolean`|`false`||
|label-width|`number \| string`|`null`|The width of label. Can be useful when `label-placement` is `'left'`.|
2020-02-05 12:14:05 +08:00
|label-align|`'left' \| 'right'`|`'left'`|Text align in label.|
|label-placement|`'left' \| 'top'`|`'top'`||
|model|`object`|`{}`|The object to get collected value from form items.|
2020-02-05 12:14:05 +08:00
|rules|`type FormRules = { [itemValidatePath: string]: FormItemRule \| Array<FormItemRule> \| FormRules }`|`{}`|The rules to validate form items.|
|show-require-mark|`boolean`|`true`|Whether to show require mark when form item is required.|
2020-03-03 14:34:24 +08:00
|size|`'small' \| 'medium' \| 'large'`|`'medium'`||
2020-02-05 12:14:05 +08:00
#### FormItemRule Type
|Property|Type|Description|
|-|-|-|
|required|`boolean`||
|validator|`(rule: FormItemRule, value: any) => boolean \| Error`||
2020-02-24 15:31:03 +08:00
|asyncValidator|`(rule: FormItemRule, value: any, callback: boolean => void) => void`||
2020-02-05 12:14:05 +08:00
|trigger|`string \| Array<string>`||
|message|`string`||
### Form Item Props
|Name|Type|Default|Description|
|-|-|-|-|
|feedback|`string`|`null`|The feedback message of the form item. If not set to `null`, it will take place of the result of rule-based validation.|
|first|`boolean`|`false`|Whether only to show the first validation error message.|
|ingore-path-change|`boolean`|`false`|Usually, the change of `path` will cause the data source's variation. So naive-ui will clear the validation result. If it is not expected, you can set it to `true`|
2020-02-05 12:14:05 +08:00
|label|`string`|`null`||
|label-align|`'left' \| 'right'`|`null`|Text align in label. If not set, use `label-align` from wrapper form.|
|label-placement|`'left' \| 'top'`|`null`|If not set, use `label-placement` from wrapper form.|
|label-style|`object`|`{}`||
|label-width|`number \| string`|`null`|If not set, use `label-width` from the wrapper form.|
2020-02-05 12:14:05 +08:00
|path|`string`|`null`|The path to collect item value to wrapper form's model object.|
|required|`boolean`|`false`|Whether to show required mark. Note: a required rule has higher priority than this prop & this prop **won't** have any effect on validation. The validation still depends on the rules.|
|rule|`FormItemRule \| Array<FormItemRule>`|`null`|The rule to validate the form item. It will be merged with the rules acquired by `rule-path` from wrapper form's rules. It's recommend to set all rules on wrapper form.|
|rule-path|`string`|`null`|The path to get rule from wrapper form's rule object. If not set, use path of the form item instead.|
|show-require-mark|`boolean`|`true`|Whether to show require mark when form item is required. If not set, use `show-require-mark` from wrapper form.|
2020-03-03 14:34:24 +08:00
|size|`'small' \| 'medium' \| 'large'`|`'medium'`||
|validation-status|`'error' \| 'success' \| 'warning' \| null`|`null`|The validation status of the form item. If not set to `null`, it will take place of the result of rule-based validation.|
2020-03-03 14:34:24 +08:00
2020-02-05 12:14:05 +08:00
### Form Item Row Props
Accept all props from form-item & [Row](n-row#Row-Props)
### Form Item Col Props
Accept all props from form-item & [Col](n-row#Col-Props)
## Methods
### Form Methods
<n-alert type="warning" title="Caveat on Validate Method" style="margin-bottom: 16px;">
<n-ol align-text>
2020-02-17 09:55:20 +08:00
<n-li>Validation will only be executed on form items which have a <n-text code>path</n-text></n-li>
<n-li>By default, validation will use all rules regardless of the triggers of the rules.</n-li>
2020-02-05 12:14:05 +08:00
</n-ol>
</n-alert>
|Name|Type|Description|
|-|-|-|
|validate|`(validateCallback?: (errors?: Array<ValidationError>) => any, shouldRuleBeApplied?: FormItemRule => boolean) => Promise<void>`|Validate the form. The rejection value type of returned promise is `Array<ValidationError>`.|
2020-02-05 12:14:05 +08:00
|clearValidationEffect|`() => void`||
### Form Item, Form Item Row, Form Item Col Methods
|Name|Type|Description|
|-|-|-|
|validate|`({ trigger?: string, callback?: (errors?: Array<ValidationError>) => any, shouldRuleBeApplied?: FormItemRule => boolean, options?: AsyncValidatorOptions }) => Promise<void>`|Validate the form item. The rejection value type of returned promise is `Array<ValidationError>`. If trigger is not set, all rules of the item will be applied. `shouldRuleBeApplied` can filter rules after they are filtered by the trigger.|
2020-02-05 12:14:05 +08:00
|clearValidationEffect|`() => void`||
2020-02-05 12:48:15 +08:00
About AsyncValidatorOptions, see <n-a href="https://github.com/yiminghe/async-validator">async-validator</n-a>.
2020-02-05 12:14:05 +08:00
## Slots
2020-04-28 22:16:18 +08:00
### Form, Form Item, Form Item Row, Form Item Col Slots
2020-02-05 12:14:05 +08:00
|Name|Parameters|Description|
|-|-|-|
2020-04-28 22:16:18 +08:00
|default|`()`||
### Form Item, Form Item Row, Form Item Col Slots
|Name|Parameters|Description|
|-|-|-|
|label|`()`||