OrchardCMS / OrchardCore

Orchard Core is an open-source modular and multi-tenant application framework built with ASP.NET Core, and a content management system (CMS) built on top of that framework.
https://orchardcore.net
BSD 3-Clause "New" or "Revised" License
7.41k stars 2.39k forks source link

All fields should have validation rules #8964

Open MikeAlhayek opened 3 years ago

MikeAlhayek commented 3 years ago

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 and Required are rules that make sense to have. But NumericField should have MinValue, MaxValue and Required

Describe alternatives you've considered

Nothing yet

deanmarcussen commented 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.

Piedone commented 3 years ago

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

PiemP commented 3 years ago

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. htm5 _validation

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.

deanmarcussen commented 3 years ago

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!

chinasqzl commented 3 years ago

The required field label adds attr.

label[required]:after { content: ' *'; color: #999; }

image

image