It would be nice if we could have a library of validation functions which could be added to any field's validation stack. For example, for weburl I wrote a function to validate whether a text field contains a valid URL. If there were a way to map the existing and available validations to a field's validation, it wouldn't have been necessary to write a new module for weburl; I would have been able to just add the validation.

Of course, the roadblock to doing this really correctly is the missing rules toolkit. Adding validation rules would ideally include AND/OR logic, and this gets tricky. I'd settle for only AND logic at the beginning.

Comments

dww’s picture

Component: Code » General

For example, for weburl I wrote a function to validate whether a text field contains a valid URL

robert, can you comment on weburl should support internal links?

thanks,
-derek

moshe weitzman’s picture

huh? drupal has valid_url() already. is this more of a form api request?

robertDouglass’s picture

Yeah. I guess. FAPI field types would be kewl.

I have to re-look at the drupal function but I think the validation I did was a bit more... something. Anyway, its clear that the field type needs work, and all suggestions are welcome.

sodani’s picture

I think that this would be a very valuable feature.

KarenS’s picture

Status: Active » Fixed

Removed weburl from cvs, please use link instead (http://drupal.org/project/link). Weburl is not being maintained.

KarenS’s picture

Status: Fixed » Active

Oops, I read this too quickly and closed it because of the reference to weburl. The idea of a validation library still makes sense, so I'm re-opening.

yched’s picture

http://drupal.org/node/97806 by webchick has been marked duplicate.

mki’s picture

This isn't new problem so I think we should get acquainted with the ready solutions. I value the way, especially simplicity, that validation is implement in Zend Framework. Look at Zend_Filter_Input and Zend_Filter classes. If we can't adapt the code, just take the concept.

KarenS’s picture

Another duplicate of this issue is at http://drupal.org/node/104339.

KarenS’s picture

Another duplicate of this issue is at http://drupal.org/node/104412.

KarenS’s picture

This is obviously an issue lots of people are interested in, since it keeps getting added (note all the duplicates), and JonBob has said in the past that he is interested in having a flexible validation system that the fields can hook into, so I don't think there is any question whether or not something like this would be accepted. The problem is that no one has proposed even the beginnings of code to actually do it.

This is a complex subject that probably needs to be hashed out in a way that isn't easily done on the issue queue. I suggest that some of the people who are interested in it go over to the CCK group on Drupal Groups and start a discussion about how best to go about this. When it matures to the point that someone can actually create some code to do it, bring it back here and propose it as a patch that we can work on.

Thanks!

sstrudeau’s picture

"would be nice" - if the validation rules could be written once to produce both javascript (yay jQuery) AND server-side validation, so if users have js turned on the form can alert them before they get all the way to "submit" that the form isn't valid... that'd be hot.

floretan’s picture

I think it's important to make a difference between single-field validation and form validation involving some relationship between fields.

For validating individual fields, one possibility would be to add the "custom php code returns true" option to the "allowed values" setting of the text field widget (something similar to what is done for custom visibility for blocks).

I'm not sure how this should be implemented (maybe with the same method mentioned above if it is possible to access other fields like computed_field.module does, but it would be nice to have a more user-friendly interface), but validating the relationship between different fields would allow much more robust forms.
We could then check that a "start date" comes before an "end date" field, or make sure that when using fieldgroup.module that every single row has correct values for every column (right now we can only guarantee that every column isn't empty, so we're dealing with a list of lists instead of an actual 2-dimensional table).

mfb’s picture

I've found the jQuery "validate" plugin to be quite useful and easy. it's available via a little helper module for miscellaneous jQuery plugins: http://drupal.org/project/jquery_plugin

Of course, more work would be needed for write-once validation rules on the server and client sides...

moshe weitzman’s picture

Project: Content Construction Kit (CCK) » Drupal core
Version: 6.x-1.x-dev » 7.x-dev
Component: General » forms system

Now that widgets are pure fapi elements, i think this request applies to core, not to cck. IOW, "make a validation directory for any form element." Once such a directory exists, it isn't too hard to imagne a UI for managing validators.

Moving to core project, forms component.

xjm’s picture

Has there been any recent activity on this issue? I think integrated client- and server-side per-field validation would be an amazing feature.

nadavoid’s picture

Re. #15 - Is there a way to browse just forms component issues in the core project? I'm not seeing a way to filter to just one component.

dww’s picture

Re #17: Yes.

Start at the core issue queue here:
http://drupal.org/project/issues/drupal

Click on "Advanced search", brings you here:
http://drupal.org/project/issues/search/drupal

Use the multi-select form element called "Components", scroll down and select "forms system". Press "search"
If you click on the little "permalink" # character at the bottom of the page, you'll have something you can bookmark for later:

http://drupal.org/project/issues?projects=3060&components=forms%20system...

nadavoid’s picture

Perfect. Thanks! Advanced Search is what I was missing. That permalink will be very useful too.

steinmb’s picture

Version: 7.x-dev » 8.x-dev

Let's move this to D8 then. Not much have changed I guess, but there is work done in #1318938: An all-embracing attempt for URL validation and Input / Output Form Filtering regarding this issue and similar ones.

nod_’s picture

Issue tags: +html5

An issue back from the dead :)

new HTML5 input types have client side validation in supported browsers, a required attribute and there is also a pattern attribute holding a regex. Polyfill available for IE8+ and other browsers.

kenorb’s picture

I'm not sure if it's outside of scope of this issue (server validation), but my proposal in #912698: Validation on CCK Core was to create 'Validation function' textfield, the same as there is already: 'Allowed values function' for selectlist widget.
Or create pre-populated dropdown with most popular Drupal validation functions from the core (like valid_email_address) and modules can add their own validations by defining them in dedicated hook.
Or this issue is focused on HTML5 validation only?

nod_’s picture

Well if there is a revamp of the validation API/UI, HTML5 things needs to be included as well as server-side work.

kenorb’s picture

Issue tags: +html5

Tested field_validation module, and actually it does a good job in Drupal 7.

Niklas Fiekas’s picture

tl;dr: We have some new HTML5 elements, that obviously can be used as they are, with all the server side validation, but you can also use their FAPI validation callbacks independently: form_validate_url(), form_validate_color(), form_validate_number(), form_validate_email().

Niklas Fiekas’s picture

We also have support for regexes as of #1174766: Support the #pattern FAPI property for native HTML5 pattern attribute. What other common things would we like to have?

nod_’s picture

We should look into the setCustomValidity thing of HTML5 forms #1797438: HTML5 validation is preventing form submit and not fully accessible

mgifford’s picture

Priority: Normal » Critical

Adding a related issue #742344: Allow forms to set custom validation error messages on required fields

and bumping this issue up to critical as per @nod_'s comment in the other issue queue.

attiks’s picture

I created #1845546: Implement validation for the TypedData API to try to solve this on all levels, since there are only 10 days left, we need to figure out what needs to be done before feature freeze and what can be done later.

effulgentsia’s picture

This issue in #30 landed, and we have #1696648: [META] Untie content entity validation from form validation open as a critical task. Given that, is there something distinctly useful being tracked in this issue, or should we close it?

moshe weitzman’s picture

Status: Active » Closed (duplicate)

No reply so closing this. Reopen if we really do need this.