Update InputText form demo

pull/3758/head
Tuğçe Küçükoğlu 2023-03-16 11:37:38 +03:00
parent 2f6c9311e5
commit abb4d27756
1 changed files with 63 additions and 50 deletions

View File

@ -3,12 +3,12 @@
<p><a href="https://vee-validate.logaretm.com/v4/">VeeValidate</a> is a popular library for handling forms in Vue.</p> <p><a href="https://vee-validate.logaretm.com/v4/">VeeValidate</a> is a popular library for handling forms in Vue.</p>
</DocSectionText> </DocSectionText>
<div class="card flex justify-content-center p-fluid"> <div class="card flex justify-content-center p-fluid">
<form @submit="onSubmit" class="flex flex-column gap-2 w-16rem"> <form @submit="onSubmit" class="flex flex-column gap-2">
<span class="p-float-label"> <span class="p-float-label">
<InputText id="value" v-model="value" @input="onInput" type="text" :class="{ 'p-invalid': errors.value }" /> <InputText id="value" v-model="value" type="text" :class="{ 'p-invalid': errorMessage }" />
<label for="value">Name - Surname</label> <label for="value">Name - Surname</label>
</span> </span>
<small class="p-error">{{ errors.value || '&nbsp;' }}</small> <small class="p-error">{{ errorMessage || '&nbsp;' }}</small>
<Button type="submit" label="Submit" @click="onSubmit" /> <Button type="submit" label="Submit" @click="onSubmit" />
</form> </form>
</div> </div>
@ -16,28 +16,31 @@
</template> </template>
<script> <script>
import { useToast } from 'primevue/usetoast';
import { useField, useForm } from 'vee-validate'; import { useField, useForm } from 'vee-validate';
export default { export default {
setup() { setup() {
const { handleChange } = useField(); const { handleSubmit, resetForm } = useForm();
const { errors, useFieldModel, handleSubmit } = useForm(); const { value, errorMessage } = useField('value', validateField);
const value = useFieldModel('value'); const toast = useToast();
const onInput = (newValue) => { function validateField(value) {
handleChange(newValue); if (!value) {
onSubmit(); return 'Name - Surname is required.';
}; }
const onSubmit = handleSubmit((values, actions) => { return true;
if (values.value === null || values.value === undefined || values.value === '') { }
actions.setErrors({ value: 'Name - Surname is required.' });
} else { const onSubmit = handleSubmit((values) => {
actions.setErrors({}); if (values.value && values.value.length > 0) {
toast.add({ severity: 'info', summary: 'Form Submitted', detail: values.value, life: 3000 });
resetForm();
} }
}); });
return { value, errors, handleSubmit, onInput, onSubmit }; return { value, handleSubmit, onSubmit, errorMessage };
}, },
data() { data() {
return { return {
@ -45,86 +48,96 @@ export default {
basic: ` basic: `
<template> <template>
<div class="card flex justify-content-center p-fluid"> <div class="card flex justify-content-center p-fluid">
<form @submit="onSubmit" class="flex flex-column gap-2 w-16rem"> <form @submit="onSubmit" class="flex flex-column gap-2">
<span class="p-float-label"> <span class="p-float-label">
<InputText id="value" v-model="value" @input="onInput" type="text" :class="{ 'p-invalid': errors.value }" /> <InputText id="value" v-model="value" type="text" :class="{ 'p-invalid': errorMessage }" />
<label for="value">Name - Surname</label> <label for="value">Name - Surname</label>
</span> </span>
<small class="p-error">{{ errors.value || '&nbsp;' }}</small> <small class="p-error">{{ errorMessage || '&nbsp;' }}</small>
<Button type="submit" label="Submit" @click="onSubmit" /> <Button type="submit" label="Submit" @click="onSubmit" />
</form> </form>
<Toast />
</div> </div>
</template>`, </template>`,
options: ` options: `
<template> <template>
<div class="card flex justify-content-center p-fluid"> <div class="card flex justify-content-center p-fluid">
<form @submit="onSubmit" class="flex flex-column gap-2 w-16rem"> <form @submit="onSubmit" class="flex flex-column gap-2">
<span class="p-float-label"> <span class="p-float-label">
<InputText id="value" v-model="value" @input="onInput" type="text" :class="{ 'p-invalid': errors.value }" /> <InputText id="value" v-model="value" type="text" :class="{ 'p-invalid': errorMessage }" />
<label for="value">Name - Surname</label> <label for="value">Name - Surname</label>
</span> </span>
<small class="p-error">{{ errors.value || '&nbsp;' }}</small> <small class="p-error">{{ errorMessage || '&nbsp;' }}</small>
<Button type="submit" label="Submit" @click="onSubmit" /> <Button type="submit" label="Submit" @click="onSubmit" />
</form> </form>
<Toast />
</div> </div>
</template> </template>
<script> <script>
import { useToast } from 'primevue/usetoast';
import { useField, useForm } from 'vee-validate'; import { useField, useForm } from 'vee-validate';
export default { export default {
setup() { setup() {
const { handleChange } = useField(); const { handleSubmit, resetForm } = useForm();
const { errors, useFieldModel, handleSubmit } = useForm(); const { value, errorMessage } = useField('value', validateField);
const value = useFieldModel('value'); const toast = useToast();
const onInput = (newValue) => { function validateField(value) {
handleChange(newValue); if (!value) {
onSubmit(); return 'Name - Surname is required.';
}; }
const onSubmit = handleSubmit((values, actions) => { return true;
if (values.value === null || values.value === undefined || values.value === '') { }
actions.setErrors({ value: 'Name - Surname is required.' });
} else { const onSubmit = handleSubmit((values) => {
actions.setErrors({}); if (values.value && values.value.length > 0) {
toast.add({ severity: 'info', summary: 'Form Submitted', detail: values.value, life: 3000 });
resetForm();
} }
}); });
return { value, errors, handleSubmit, onInput, onSubmit }; return { value, handleSubmit, onSubmit, errorMessage };
} }
}; };
<\/script>`, <\/script>`,
composition: ` composition: `
<template> <template>
<div class="card flex justify-content-center p-fluid"> <div class="card flex justify-content-center p-fluid">
<form @submit="onSubmit" class="flex flex-column gap-2 w-16rem"> <form @submit="onSubmit" class="flex flex-column gap-2">
<span class="p-float-label"> <span class="p-float-label">
<InputText id="value" v-model="value" @input="onInput" type="text" :class="{ 'p-invalid': errors.value }" /> <InputText id="value" v-model="value" type="text" :class="{ 'p-invalid': errorMessage }" />
<label for="value">Name - Surname</label> <label for="value">Name - Surname</label>
</span> </span>
<small class="p-error">{{ errors.value || '&nbsp;' }}</small> <small class="p-error">{{ errorMessage || '&nbsp;' }}</small>
<Button type="submit" label="Submit" @click="onSubmit" /> <Button type="submit" label="Submit" @click="onSubmit" />
</form> </form>
<Toast />
</div> </div>
</template> </template>
<script setup> <script setup>
import { useToast } from 'primevue/usetoast';
import { useField, useForm } from 'vee-validate'; import { useField, useForm } from 'vee-validate';
const { handleChange } = useField();
const { errors, useFieldModel, handleSubmit } = useForm();
const value = useFieldModel('value');
const onInput = (newValue) => { const { handleSubmit, resetForm } = useForm();
handleChange(newValue); const { value, errorMessage } = useField('value', validateField);
onSubmit(); const toast = useToast();
};
const onSubmit = handleSubmit((values, actions) => { function validateField(value) {
if (values.value === null || values.value === undefined || values.value === '') { if (!value) {
actions.setErrors({ value: 'Name - Surname is required.' }); return 'Name - Surname is required.';
} else { }
actions.setErrors({});
return true;
}
const onSubmit = handleSubmit((values) => {
if (values.value && values.value.length > 0) {
toast.add({ severity: 'info', summary: 'Form Submitted', detail: values.value, life: 3000 });
resetForm();
} }
}); });
<\/script> <\/script>