-
Notifications
You must be signed in to change notification settings - Fork 813
Add example of validation in custom field type #4215
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
Conversation
|
Thanks for the PR, @bjarnef 🙌 We will review this soon 🙂 |
|
I think @AndyButland may have some input and the "CompareField" setting currently just contain alias of the other field. It may need to field to specificy validation message as there are tree validation types:
Email (regex) could be optional. In other scenarios in may be Phone instead or another custom regex. If including https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.19.5/additional-methods.js there's also a I think in "Compare" filed type could be useful in a future release of Umbraco Forms. |
| : string.Empty; | ||
| } | ||
|
|
||
| private static Field? GetPostField(Form form, string key) => form.AllFields.SingleOrDefault(f => f.Alias == key); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@AndyButland I noticed these methods in other examples, but maybe Forms should include these extension/helper methods?
E.g.
GetField(Form form, string key)
GetFieldValue(Form form, HttpContext context, string key)
Using `icon-legal` (weight icon) https://nicbell.github.io/ucreate/icons.html
AndyButland
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a nice solution, but I think it might be better as a blog post we link to (which I'd be happy to do so from the docs) rather than going in as official documentation. There's a few reasons for this:
- There's a hard-coded reference to a form ID. Whilst that might work for a particular scenario, it's not a very general approach that we can really recommend people do.
- You've got client-side validation working with jquery which is great, but not sure if it also works with
aspnet-client-validation? I think it would need to given people are often moving away from jquery now for something more lightweight. - The compare field is missing most of the settings available on the text field. And it would be an overhead in code and in docs to maintain this.
This is just an idea, but I think the best way to support this in Forms may be something you configure at the level of the form rather than the field. So like you can add workflows for a form, perhaps we have some sort of means to add "form validation rules". Validation on single fields would be set as is, but rules that span fields across the form would be set here. So you could have "compare email = email", "end date > start date" etc. without having to have "compare" versions of many of the field types.
We would then need to have client side and server side validation of these rules as the form and/or page is submitted.
|
@AndyButland regarding the hard-coded reference to a form ID. Yes, I don't want it to be hard-coded, but I didn't found a way to get current form reference in field types (like in form template itself). Do field types have any access to the form context, e.g. get current form or just a Currently it is using Which specific settings are missing for Compare field? It is basically a copy / extended version of I was thinking about compare rules as well, but it isn't a concept in Forms at the moment, so this was an attempt for now with the current features. |
From within the partial view, I don't believe so - they just have access to the
I suspect the new library tries to follow the same conventions, as it's intended as a replacement for using jquery. But it would need testing to confirm.
I meant the settings on the field type definition - so autocomplete attribute, field type, placeholder etc. |
|
@AndyButland okay, I am not sure how Not sure if Yes, you're are right .. I added the settings to the view, but not to the field type. I tried originally to inherit from the Can we extend/inherit a field type from an existing field type e.g. |
|
@AndyButland I tried removing jQuery + jQuery validation and instead included The compare client side validation doesn't quite seem to work, but the JS included /App_Plugins/UmbracoForms/Assets/aspnet-client-validation/dist/aspnet-validation.min.js does contain a But while I was using However in my tests jQuery validation re-validated when changing values in both fields, where |
sophie-neale
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Spotted a little duplication 👀
Co-authored-by: Sophie <35264991+sophie-neale@users.noreply.github.com>
|
Hi all 👋 What is the status of this PR? |
|
@sofietoft I don't really write blog posts, but even anyone shared that I am not sure it is discoverable as the documentation. I think it could make sense to show a few examples / code snippets to extend CMS, Forms etc. I think it is a typical example to add custom validation and often it probably depends on another form field like in this use-case. |
|
I completely understand @bjarnef ! 💪 Let me just have another chat with Andy about it, and we'll figure out what we should do with it. |
|
Hi @bjarnef ! I've had a chat with @AndyButland about this, and we've agreed to not add this sample to our docs. We do agree, though, that it's a sample that would definitely be valuable to other people. The optimal solution here would be to have the option to add this sample in the docs and then marking it as "community content". I'll be closing this PR for now though. And thanks again for putting in the work here - we really appreciate it! |
Uh oh!
There was an error while loading. Please reload this page.