Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
By pub497 on
When people copy/paste into a wysiwyg it will sometimes paste with html formatting, and as a result add additional characters (html entities). This is taking up the character limit on some of my fields, so I want to try and get rid of these characters and then send it to validate. I've tried making my custom validation function but it still goes through the default validation before my custom one. Is there a way to check for character limit after a user clicks "preview" but before validation?
Thanks
Comments
array_unshift
you can call your validation first before default validation using array_unshift(), then you can change value using form_set_value()
An example of what Josh is
An example of what Joshi is saying is as follows. First, look at the original form definition (an example):
We then add our own validation handler in hook_form_alter() to be run before any of the default handlers:
And then we make our changes in our custom validation handler, that will now be run before any of the default validation handler and submit functions:
I tried the solutions above
I tried the solutions above but it kept giving me the character limit error.
What I ended up doing was clearing the error in my validate function, filtering the text, and recheck if it is still above the limit and setting my own error message if it still is
There was probably something
There was probably something wrong with your code.
Set the #validated attribute to TRUE
If you want a certain element to skip core validation and just be validated by your own validation function, set the validated param to TRUE: