Skip to content
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

Allow Developers to Edit the Regex Validation Dropdown #936

Closed
Attackmonkey opened this issue Dec 13, 2022 · 2 comments
Closed

Allow Developers to Edit the Regex Validation Dropdown #936

Attackmonkey opened this issue Dec 13, 2022 · 2 comments

Comments

@Attackmonkey
Copy link

The text type fields have a dropdown for common regexs to use for validation. Currently there is no way for developers to add additional regular expressions to this list, which only contains a few basic options, without modifying core forms JS files. This is even trickier now that most of the files are compiled into the assembly. You can paste the regex in manually, but that's not really feasible/practical for your average CMS user.

We've had a bunch of requirements over the years to create fields that validate to various approved regexs (for example NI numbers, or customer account numbers), that you might need to use in more that one form. You end up having to create a custom field type for each time you want a re-usable regex, which is fine, but you can soon end up with a large number of custom fields if you need a lot of them. It would be really handy if there was a way to hook into the list of regexs for validation as a developer and add in additional items.

@AndyButland
Copy link

We've prepared this now for the next minor release of Forms 10+. It'll work like the other "providers" that you can register and update what we ship by default in the product. So for example, with the following code, you can remove one of the core ones and add one of your own.

public class TestSiteComposer : IComposer
{
    public void Compose(IUmbracoBuilder builder)
    {
        builder.FormsValidationPatterns()
            .InsertAfter<Email, NumberOneToFive>()
            .Remove<Number>();
    }
}

And to implement one, you would use the IValidationPattern interface, like this:

    public class NumberOneToFive : IValidationPattern
    {
        public string Alias => "numberOnToFive";

        public string Name => "Number (1-5)";

        public string LabelKey => string.Empty;

        public string Pattern => @"^[1-5]*$";

        public bool ReadOnly => true;
    }

You can either provide Name or a LabelKey (and if the latter is provided, it'll look for this translation key to present the correct language for the label in the backoffice).

There'll be a docs update detailing this too once the release is ready.

@Attackmonkey
Copy link
Author

@AndyButland that's amazing! Thank you so much for adding this to Forms!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants