By Richard Blackborder on
I'm building a basic hook_block implementation much like the example:
http://api.drupal.org/api/function/block_example_block/6
There's one thing I can't figure out: is there any way I can validate the form returned if $op is "configure" for hook_block? Say, for example, that I have a text field that needs to follow a particular format, or that can't be left blank?
Comments
The best I've managed so far
The best I've managed so far is something like this:
Whilst that stops bad data going in, the form isn't reprinting; the page goes back to the blocks list page and prints a success message under the form error message. And any changes to the block that aren't based in my $op=='save' code still take effect.
...
By the time you hook into the 'save' op of hook_block, it's too late. Instead, add an element validation handler in the 'config' op of hook_block. So if your form looks something like this:
then add an element validation handler to the field, like this:
and then provide the handler, like this:
I did and it works,
I did and it works, thanks!
Previously, I had also tried adding a form validation handler, based off the forms api reference:
but it didn't work. The individual element validation handler you suggested does, though. Thanks :)
Drupal 5.x
The above example illustrates using the #element_validate attribute, which is part of Forms API for Drupal 6.
Here's how to achieve the same thing using #validate for Drupal 5.
and then provide the handler, like this:
-------------------------------------------------------
"If you don't read the newspaper you are uninformed;
if you do read the newspaper you are misinformed."
-- Mark Twain
Generic Validation
Thanks roopletheme!
It may be useful to some to implement more generic error handlers; e.g. if there are five fields to test to see if they are numeric, it would be nice to send them all to a single "is numeric" function rather than build out five identical blocks of code. For an example of what this might look like: