-
Notifications
You must be signed in to change notification settings - Fork 169
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
feat: allow custom constraint validation errors #1023
feat: allow custom constraint validation errors #1023
Conversation
✅ Deploy Preview for oruga-documentation-preview ready!
To edit notification comments on pull requests, go to your Netlify site configuration. |
It might be feasible to merge the |
I'll need to extend this to cover other input types if/when we determine whether this is the right API. |
Hmm, OK, I get it now. But maybe our How about this? |
That could work, but it would be more complicated than strictly necessary for my main use case. The main goal here isn't to override the validation message for inputs that already fail a built-in validation check (e.g. <script setup lang="ts">
import { OInput } from '@oruga-ui/oruga-next';
import { computed, ref } from 'vue';
const val = ref<string>('');
const validation = computed(() => {
const value = val.value;
const valid = value === '' || Number.parseInt(value) % 2 == 0;
return valid ? '' : 'Enter a number divisible by two';
});
const onSubmit = (e: Event) => { alert('Submitted'); e.preventDefault(); };
</script>
<template>
<form @submit="onSubmit">
<OField label="Value">
<OInput v-model="val" type="number" required :customValidity="validation" />
</OField>
</form>
</template> In this example, any number that isn't a multiple of two will fail the validation checks, preventing form submission. Your suggested API does seem like it would be an improvement for the existing Perhaps the right solution (if we're OK with a breaking change) is to merge the props and allow the merged prop to take any one of the following:
If we want to minimize breakage, we could make it so the prop doesn't set a custom validation error (via |
I am just trying to stay open to a wider range of usecases where possible. With the current We are currently on a breaking change release with 0.9.0, so it is fine to merge both features into one new prop and implement it as a breaking change. So the type would be: Yes, we should call |
45ec529
to
43efa27
Compare
Yes; without cleaning up the custom message, the control will still think it's in an invalid state. I've pushed up a new version that supports the function-based syntax. |
7701964
to
6b4b70f
Compare
The diff is (unfortunately) a bit of a mess with the restructuring, but I've added some changes that should make this more robust. This seems to hold up in the test cases I've been able to think of so far. |
Maybe you can add some unit test for this composable? |
It definitely needs some tests, but implementing them outside of a browser context may be tricky. I don't think the current DOM polyfill supports the constraint validation API. |
Should we consider just adding browser-based testing back in for these kinds of cases? This definitely seems like it's getting complicated enough that it needs browser-level tests. It looks like Vitest does have experimental browser support. |
I've never done browser-based testing before. Feel free to add a new PR with the configuration changes and some primitive tests to demonstrate. |
Thanks mate, looks good now, great work! Will you finish it up with all the other components? Or should i do that? |
Sure; I can do that. Would you like me to do that after this PR, or should I consider the browser-based tests a prerequisite for this change?
I'll get the rest of the components taken care of. It should be pretty quick to sort them out. |
Maybe just finish this one before opening another bigger topic.
Gool! |
c976d71
to
5e96615
Compare
Rebased to make the next stage of review easier. I'll extend these changes to all input types next. |
I've pushed up some new code changes, When it comes to the documentation, I was trying to build the documentation to test my edits and discovered that the Markdown files seem to be automatically generated. Should I make any updates to the documentation, or should I just let that process take care of things? |
The documentation is generated by Please generate the docs and push the documentation changes to this branch as well. |
Looks good to me. Remove the draft stage when you think it`s ready to merge :) |
This feature provides a new way to override the browser's default constraint validation messages, making `validationMessage` unnecessary.
With this change, rather than having to duplicate the browser's native validation logic when selecting a custom message, the parent component can just react to the validity state that the browser reports.
For controls nested inside each other, the parent usually component handles constraint validation concerns, so there's no need for the children to also handle validation; in fact, they can cause incorrect changes to the field state if their validation isn't suppressed. The one exception is that the child control which actually owns the "main" input element must forward the `invalid` event to the parent control.
27bd868
to
0b56f32
Compare
It seems that you have an older version of the oruga theme installed. I will fix this for you! Just learned how to commit to another once pull request :D |
…uga-ui#1026) * fix(picker): solve editing directly not working * fix(picker): make locale property reactive * test: update tests
This feature provides a new way to override the browser's default constraint validation messages, making `validationMessage` unnecessary.
With this change, rather than having to duplicate the browser's native validation logic when selecting a custom message, the parent component can just react to the validity state that the browser reports.
For controls nested inside each other, the parent usually component handles constraint validation concerns, so there's no need for the children to also handle validation; in fact, they can cause incorrect changes to the field state if their validation isn't suppressed. The one exception is that the child control which actually owns the "main" input element must forward the `invalid` event to the parent control.
…768/oruga into custom-constraint-validation
Thanks for sorting out the documentation! I'll keep that in mind for my next PR. |
Fixes #928
customValidity
prop, which allows setting custom constraint validation errors. Unlike thevalidationMessage
prop, this new prop actually marks the element as invalid, preventing form submission.