Open MikeAlhayek opened 3 years ago
Potentially we could support javascript based rules for field validation.
More flexible, if we just add MaxLength
and MinLength
to a text field, someone will want something else.
Related but for form fields: #3980. We could use the same JS validation, check out this WIP PR: https://github.com/Lombiq/OrchardCore/pull/4
Hi all,
I have almost the same problem: I would like to introduce some client validation to my content type. I have created a TextField option's with a couple of "validation settings" that allows me to set the HTML5 attributes to validate it's input field and set some specific error message, if I have some special needs on my field (like when I use pattern attribute to validate the field with a regular expression...mmmm...this could be a possible translation issue. Probably is better to have a dictionary with some message and relative translations that could be recalled in the "validation settings").
But this solution is really limited because I need something that could be applied to all the fields like a "part" that give the possibilities to have the "validation settings" all over the orchard core fields and inject the HTML5 attributes in all the fields shapes (I hope it`s the right way to use this term) to enable client validation over all content types fields.
Is it possible way to introduce this feature? The "validation settings" could be used in the server side to replicate validation on the field by a dedicated service. For more complex validation we could redirect devs to create a custom ContentDisplayDriver
specific for its content type.
I leave you a demo to show what I have done.
In this moment the issue I see it's about maxlength
attribute that limit the maximum number of character inside an input field without warn the user: if you cut and paste some text that goes over the limit, the value in the input field is truncated to the limit without any notifications. I have solved this using pattern+maxlength
but it's not beautiful because if you want to use pattern for other things, the user have to include the maxlength
check in the regular expression.
I ask you sorry if I'm not explained well and if I make you lose some time.
Thank you.
That looks nice @PiemP but I think will only provide client side validation, which can be worked around just by editing the html, and removing the attributes?
The suggestion from the last discussion we had around this, was to consider having a ValidationPart
, which could (potentially) use the same javascript on the front end and backend, to provide both client side and server side validation rules, localized error messages etc.
The pr @Piedone refers to had some good ideas as well.
Look forward to seeing what you come up with!
The required field label adds attr.
label[required]:after { content: ' *'; color: #999; }
Is your feature request related to a problem? Please describe.
I am trying to create a part with a text field of a type TextField. However, I don't want to allow the user to type more than 120 characters. #8963
Describe the solution you'd like
During adding field, the user should be able to add some validation rules. For example, for a text field
MaxLength
,MinLength
andRequired
are rules that make sense to have. ButNumericField
should haveMinValue
,MaxValue
andRequired
Describe alternatives you've considered
Nothing yet