New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
trigger() behaves differently for normal fields and field arrays #2379
Comments
That's expected behavior, when using scheme you are validating against the inputs with your schema, however for build-in validator and it's field level, you will have to trigger setError manually for |
In the absence of a schema they behave differently though. I know if i use a schema it works and how to validate it, but if one type of field passes when no schema and the other one fails then that's unexpected. I'd posit that they should both do the same, i.e. if no validation schema exists then all fields should pass validation. trigger() on an array field when no validation is required should return true, not false. When describing it logically it seems to make little sense. is validation required on this field? no because there is no schema. does it validate? no. If the argument an argument can be made for the above, then surely the same is true for normal non array field types. currently a normal field follows the opposite logic. is validation required on this field? no because there is no schema. does it validate? yes. |
Yes, I understand your concern above. As today, we are not supporting |
in the meantime, you can send a feature request for |
That's what I'm doing, I'm explicitly calling in I am looking to pass validation on the name of a field when that field is an array if there is no validation set. I'm not looking for i.e. if there is no schema provided
https://codesandbox.io/s/react-hook-form-usefieldarray-template-f3iv0?file=/src/index.js In the above CSB you can see examples of calling trigger from your screenshot above. A key point i suppose there is
|
That |
Right that makes a bit more sense! So it's the fact that RHF sees that field as absent. https://codesandbox.io/s/react-hook-form-usefieldarray-template-cy2op?file=/src/index.js Demo of an absent field fails too. Thanks very much for your help and feedback there, that's cleared things up for me. Really appreciate how involved in the GH issues you are, it's really refreshing and no doubt a huge help to many. Thanks again. |
no worries mate, sorry I wasn't clear enough in the first reply, but please send us a feature request, I will log this in our project. Just be mindful, we are pretty cautious about the feature request because as you know each feature increases the package size. today we are still one of the small form libs in the industry. so be patient with us as we move/hold feature into progress, we tend to wait for community vote first as well. |
Of course, that makes sense, understandable. Where do people vote on features? |
I wrote a quick helper function for someone who wants to recursively trigger all objects in an array field. Its pretty simple:
The main downside is that it calls const getAllNamesByKey = (values, key) => {
// Choose which parser we are using, as arrays use [ ] notation in useForm
const parseArray = (stack, property) => `${stack}[${property}]`;
const parseObj = (stack, property) => `${stack}.${property}`;
// Recursively unpack the keys
const unpackKeys = (obj, stack, toReturn) => {
// Check which parser we will use
const parser = Array.isArray(obj) ? parseArray: parseObj;
for (const property in obj) {
if (obj.hasOwnProperty(property)) {
const returnString = parser(stack, property);
// If its an object, recurse
if (typeof obj[property] == 'object') {
unpackKeys(obj[property], returnString, toReturn);
} else {
// This has a leading "." in it due to the start step in the
// recursion. Remove it using slice.
toReturn.push(returnString.slice(1));
}
}
}
};
const toReturn = [];
unpackKeys(values, '', toReturn);
return toReturn.filter((fullKey) => fullKey.startsWith(key));
};
const triggerByKeyGenerate = (getValues, trigger) => (key) => {
const values = getValues();
const namesToTrigger = getAllNamesByKey(values, key);
namesToTrigger.map((name) => trigger(name));
}; The structure above lets you build const {handleSubmit, setValue, getValues, errors, control, trigger} = useForm();
const triggerByKey = triggerByKeyGenerate(getValues, trigger);
.....
triggerByKey("highlevel[0]"); // trigger every field in the first element in the array
triggerByKey("highlevel"); // trigger every field in every element of the array, recursively Hopefully this will save others some time if they want the same functionality. Thanks for the great library btw, love it! |
Describe the bug
Calling trigger when no schema is in place passes normal fields (as expected) but fails array fields.
To Reproduce
Steps to reproduce the behavior:
schema
. normal field passes, array field passes when criteria met.emptySchema
. normal field passes, array field passes.Codesandbox link (Required)
Include a codesandox will help us to investigate the issue quicker.
https://codesandbox.io/s/react-hook-form-usefieldarray-template-4ruie?file=/src/index.js
Expected behavior
I would expect the behaviour of trigger to be consistent for normal fields and array fields in the absence of a provided schema.
The text was updated successfully, but these errors were encountered: