Is there a way to set the required validation dynamicly? #1006
fdverwoerd
started this conversation in
General
Replies: 3 comments 5 replies
-
validation does not accept object notation, it is string or array, with that said this might work: :validation="[
...(cond ? [['required']] : []),
['length', 1, 5]
]" |
Beta Was this translation helpful? Give feedback.
4 replies
-
I think I have a working version now :) import type { FormKitNode } from '@formkit/core';
/**
* File: lisy_required.ts
*
* This is a custom validation rule for FormKit. If the current args is required then do required check.
*/
const lisy_required = function (node: FormKitNode, ...args: any[]) {
if (args[0] === 'required') {
console.log(typeof node.value)
if (typeof node.value === 'string') {
console.log('length', node.value.length);
console.log('string type:', typeof node.value === 'string')
console.log('lenght=0:', node.value.length === 0);
}
return typeof node.value === 'string' && node.value.length !== 0;
} else {
// passing is true
return true;
}
}
lisy_required.skipEmpty = false;
export default lisy_required; And Vue template <FormKit v-if="lisyConfig.fieldOptions.fieldOptionsFamilyNumber !== 'hidden'"
type="text"
name="familyNumber"
id="familyNumber"
:validation="[
['lisy_required', lisyConfig.fieldOptions.fieldOptionsFamilyNumber],
['length', 1,5]
]"
label="Gezinsnummer"
outer-class="form-grid-column-12"
></FormKit> |
Beta Was this translation helpful? Give feedback.
1 reply
-
Thank you very much! 💯 |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hello there! Love formkit!
I was looking for a way to set required dynamicly based on a setting in an object in Vue.:
The options are
required
|optional
|hidden
.So when option is
required
i need `validation="required|length:1,5".Is this possible? I tried using an object notation like so: but that wont work.
Beta Was this translation helpful? Give feedback.
All reactions