Comments (5)
Thank you for your detailed feedback. I'll give it some thought.
from modular-forms.
I don't quite understand the problem you are describing. As described here, small helper functions can be used for validation. For individual validations custom
can be used. Alternatively you can use custom
to create your own validation functions.
from modular-forms.
@nzdeep's text is quite a mess (sorry 😄). But I understand it because I have the same issue.
Validators have only one parameter, the value. That's not enough. It would be great to have a second parameter, eg. ref to field or some object containing ref to field, ref to input or something like that. See Angular's ValidatorFn.
Some people use validation messages like: "Field 'Firstname' cannot be empty" (or something like that) for the required
. How can you get the name of validated field? Creating unique validation messages for each input is not a good solution. We already have labels, so we can just grab label of the field.
Something like:
type ValidationContext = {
field: FieldStore<T, U>;
element: FieldElement;
}
function required<TFieldValue extends FieldValue>(error?: string): (value: Value<TFieldValue>, context: ValidationContext) => string {
return (value, context) => {
// ...
const label = context.element.form.querySelector(`[for='${context.element.id}']`).innerHTML;
return (error ?? "Field '{label}' is required.").replace("{label}", label);
};
}
And it's not just about validation messages; it's handy in several cases.
PS: With access to FieldStore we don't even need the value parameter, right? But it would be breaking change.
PS2: I hope my description is a little better. 😅
from modular-forms.
Thanks for your description. If I understand correctly, you want to outsource the generation of the error message instead of passing it as an argument. I don't know if I would recommend that. It just makes your code harder to follow. However, I understand that a context can be useful in general.
from modular-forms.
you want to outsource the generation of the error message instead of passing it as an argument
Well, I want to create custom validation rules with messages defined inside those rules (consistency, maintainability etc.), but I'd like to have those messages dynamic. Dumb example return `Value of '${fieldLabel}' must be greater than ${minValue}`;
. This is just an example with messages, but like you said, context can be useful in general.
[A little bit OT]
I prefer React and Solid but I work with Angular too and even I don't like it much, I have to admit that validation system in combination with directives is great.
Eg. simple required
validation. I have a validation-required
directive that I can add to any form control. It adds Angular's validator with predefined, localized error message and it even adds required
css class on the element, so I can handle this in CSS and idk change border to red, add the asterisk or something like that.
Here's quick StackBlitz demo
I'd love to achieve similar behavior/DX in SolidJS.
But eg. that asterisk would require some complex validation rule factory with some kind of mount/unmount logic. I'm just thinking out loud...
export type ContextValidator = (message: string, ...params: any[]) => {
onMount: (context: ValidationContext) => void;
onCleanup: (context: ValidationContext) => void;
validator: (value: Value, context: ValidationContext) => string;
};
export type FieldProps<TFieldValues extends FieldValues, TResponseData extends ResponseData, TFieldName extends FieldPath<TFieldValues>> = {
// ... \/
validate?: Maybe<MaybeArray<ContextValidator | ValidateField<FieldPathValue<TFieldValues, TFieldName>>>>;
// ...
};
from modular-forms.
Related Issues (20)
- Qwik: Latest qwik release breaks types HOT 14
- Enhance FieldStore to Return Multiple Errors per Field HOT 2
- Phone Number Input HOT 1
- Allow setting initial form values of a mounted component HOT 2
- Preact: What is the best method to pull values of fields in the form for using in computed() HOT 4
- [Question] Is it possible to have optional fields? HOT 7
- Import error in formAction for qwik HOT 1
- Checkbox works improperly when use modular forms with kobalte HOT 7
- [Question] Is it possible to have custom input without ref? HOT 5
- When useFormStore is in a seperate file, it fails HOT 1
- Select component with number types HOT 3
- Using modular form Fields inside solidjs Dynamic component HOT 1
- Qwik: Vite is trying to externalize packages used in formAction$ HOT 3
- `requestSubmit` is not a function on Safari < 16 HOT 3
- Does this library support React Native? HOT 1
- Internal issue: Cannot read properties of null (reading 'internal') HOT 4
- SolidJS: Form reset action doesn't preventDefault like submit does. HOT 1
- [Qwik] Form state is reset after action returns an error HOT 1
- [React]: Preact Signals Dependency Update HOT 11
- [SolidJS]: How to handle errors from server? HOT 3
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 modular-forms.