Giter VIP home page Giter VIP logo

Comments (3)

logaretm avatar logaretm commented on July 24, 2024 1

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.

logaretm avatar logaretm commented on July 24, 2024

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.

russback avatar russback commented on July 24, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.