Basic Usage
Use with Items Config
Attention
If use with items config. Make sure use Casual UI as global plugin.
Or manually register all the form components
Sizes
Gutter sizes
Label Direction
Label Align
Label Width
Col Span
Validation Basic
The form validation of Casual UI,need to used with field rules of CFOrmItem.rules is the validators. Each validator is a function that accept current value. And it need return false | string | Promise<false | string>.
The meaning of this return:
falsemeans validation passed.stringmeans validation failed and it's the error messagePromisemeans a async validation and the result is the same with above
For example. This is a validator for some field is required.
const rule = v => (v ? false : 'This field is required')
It's async validator would be like this:
const asyncRule = v =>
new Promise((resolve) => {
setTimeout(() => {
resolve(v ? false : 'This field is required')
}, 1000)
})
Validation Demo
Customize Form Item
Use #[field] dynamic slot to customize some form item.
Every #[field] slot have these bindings: { validate, clearValidate, hasError }
validateis a function to validate current itemclearValidateis a function to clear current item validate statushasErroris the error status, can be a string or afalse, string is the error message andfalsemeans no error
Customize Form Component Advanced
Use useValidator API to fetch current form item context
<script setup lang="ts">
import { useDefaultVModel, useValidator } from '@casual-ui/vue'
const props = defineProps<{
modelValue: string
}>()
const emit = defineEmits<{
(e: 'update:modelValue', newValue: string): void
}>()
const innerValue = useDefaultVModel(props, emit)
const { validate, clearValidate, hasError } = useValidator()
</script>
<template>
<div class="custom-input" :class="[{ 'custom-input--has-error': hasError }]">
<input
v-model="innerValue"
@focus="clearValidate"
@blur="validate(innerValue)"
>
</div>
</template>
<style lang="scss" scoped>
.custom-input {
input {
outline: none;
border: 1px solid var(--casual-copywriting-normal);
}
&--has-error {
input {
color: var(--casual-warning);
border-color: var(--casual-warning);
}
}
}
</style>
Attention
All the form item's props which has the same name with form will override the form's value.
It's useful when you want to a special item to have different styles.
CForm API
Array<CFormItemConfig>() => []The form items config
Record() => ({})The whole form data. Can be used with v-model.
string'100px'The label width.
number6The col span
LabelDirection'row'The label and form component direction.
"left" | "center" | "right"'left'The label align method.
CSize'md'The size. It will affect all components within.
CSize'md'The gutter size.
booleanfalseDetermine whether the whole form is in validating or not. Can be used with v-model:validating. It maybe helpful when some async status loading is required
CFormItem API
string''The filed key of whole form data.
string''The label text.
string'100px'The label width.
number6The col span of each item. Total cols are 12.
CSize'md'The size of all components within.
Array<CRule>() => []The validators.
LabelDirection'row'The label and form component direction.
"left" | "center" | "right"'left'The label align method.