Comments (3)
false
is a valid value to yup
's .required
so it won't make sure the user has checked the box.
instead try oneOf
yup.boolean().oneOf([true], 'Field must be checked')
from vee-validate.
I spotted a few issues in your snippets.
You should use either useField
or Field
component, not both.
You should also pass the type
to the useField
options.
const { value, errorMessage, checked } = useField(() => props.name, undefined, {
type: 'checkbox'
uncheckedValue: false,
checkedValue: true
});
What is left is to bind it with a input
element instead of the Field
component and you can use v-model
then or use the checked
property. It is up to you how to hook it up.
Now for setting the initial value, you can either pass it to useField
options, which would make sense if you want to set it via a prop. Alternatively you can set it on the form level.
useField(() => props.name, undefined, {
// ...
initialValue: true
});
// OR
useForm({
initialValues: { checkboxField: true }
});
You can also use resetForm
to toggle the checkbox initial value there, please check the docs in that area.
from vee-validate.
Thanks @logaretm.
I have updated this to the below and can see that the checked
property is updating according to the initial value passed in via props. However the yup schema validation on the parent component isn't seeing that
<script setup>
import { useField } from 'vee-validate';
const props = defineProps({
class: {
type: String,
default: '',
},
name: {
type: String,
required: true,
},
intro: {
type: String,
required: false,
},
initialValue: {
type: Boolean,
default: false,
},
});
const { value, errorMessage, checked, handleChange } = useField(
() => props.name,
undefined,
{
type: 'checkbox',
uncheckedValue: false,
checkedValue: true,
initialValue: props.initialValue,
}
);
</script>
<template>
<div class="mb-6 md:max-w-[530px]">
<label :for="name" class="o-label">
<span class="o-instructions" v-if="intro">{{ intro }}</span>
<div class="flex">
<input
type="checkbox"
:name="name"
:id="name"
:value="value"
:checked="checked"
@change="handleChange"
class="sr-only o-checkbox" />
<span class="o-checkbox__box"></span>
<div class="ml-3 o-label__text">
<slot></slot>
</div>
</div>
</label>
<p class="o-error" v-if="errorMessage">
{{ errorMessage }}
</p>
</div>
</template>
That's using
const currentSchema = const schema = [
yup.object({
copyrightOwnershipConfirmed: yup.boolean().required('Please check the box to confirm ownership of copyright.'),
})
];
const { values, handleSubmit, resetForm } = useForm({
validationSchema: currentSchema,
keepValuesOnUnmount: true
});
And including the checkbox component above:
<Checkbox
name="copyrightOwnershipConfirmed"
:initial-value="false">
<span>Label text here</span>
</Checkbox>
I've tried using v-model to bind copyrightOwnershipConfirmed
on the component and have also added a watch to try and force the model value but I can't get the yup validator to pick up that this field isn't checked.
from vee-validate.
Related Issues (20)
- Unable to setFieldError from API with multi-step form wizard in before step HOT 1
- Pass submit function to useForm composable
- best way to set useField value from props ? HOT 2
- useForm not working after build when using shadcn components from a shared package in monorepo HOT 2
- defineField ref's value is possibly undefined, even if the schema doesn't allow it HOT 3
- When you remove initial value, form doesn't validate again
- @vee-validate/valibot 4.13.1 (latest) not compatible with valibot 0.33.0 HOT 2
- Field Array's Errors Slot HOT 1
- logical operations between global rules HOT 2
- Add `syncVModel` to `defineField` HOT 1
- v-bind strange behavior
- Email validator Regex
- Vee Validate error , This dependency was not found vee-validate in ./src/utils/vee-validate.ts HOT 1
- From action `setDirty` HOT 1
- Binding Field to Vuetify v-autocomplete causes it to display [object Object]
- handleSubmit doesnt show and return values HOT 1
- Infer model type from schema when using defineField() HOT 1
- Regex issue in prefix/suffix options [@vee-validate/i18n] HOT 1
- useFieldArray field errors remain when removing array items HOT 1
- Pass validation mode through `handleSubmit` HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from vee-validate.