By icecreamyou on
In a module I'm working on, I have a couple of blocks. I've edited the block configuration form using hook_form_alter() so that settings specific to that block will appear. But I need to validate those settings so users can't put values like -1 in a field that asks how many results they want displayed. How would I go about doing that?
Thanks in advance.
Comments
More
Alright, so I just found out I can use '#validate' to specify the validation function I want to use for a form element. But will that override the default validation function? And if so, how can I keep it from doing that?
*Bump.*
*Bump.*
*Bump.*
*Bump.*
I didn't try this
but you may want to use the following format:
you put this in yout hook_form_alter (as you can see it's the '+=' that does the trick in ensuring other validations are untouched)
then you have that function (for example this checks if the emails match)
please let me know if this works - i'm interested myself :)
Will try it
Sorry I didn't get to this yesterday - I was really busy. But I'll try it out tonight and see how it goes. It looks right to me - I seem to remember seeing similar code somewhere...
It turns out that the block
It turns out that the block configuration page doesn't have any built-in validation, so it *should* be okay to just add custom validation with
'#validate' => array('your_function_validate' => array());except that this *could* cause problems if another module edits the block configuration form.However, using
+=causes aFatal Error: Invalid Operator. The only remaining option as far as I can tell is to use array_merge(), but since we don't know what array we need to merge with, that won't help anything. :-/This is how I'm using that
This is how I'm using that code. It's almost identical to the code in login toboggan module (which is where i got it :)
It seems to work for me - no problems so far
That's nearly exactly what I
That's nearly exactly what I did. But maybe "+=" only works if there's already a value for '#validate', and there isn't one for the block configuration form. Maybe I should do a check first. Will look into it.
The problem turned out to be
The problem turned out to be that I wasn't using $form['#validate'] - I was using $form['...']['#validate'] which is broken in 5.x. The solution is to use the validate function for the entire form, as you did.
Thanks.
can you shed some more light on this?
I've been trying to use this method effectively using $form['#submit'] but haven't been able to figure out a way to get it to work. If I do it with $form[..]['#submit'] in 5.x then it fails to work. I tried the += bit and it worked, but then Drupal returns errors (which is highly annoying although I remember reading something about how += isn't supported in php 5 or somthing along those lines)
So, i'm stuck. Using submit on the whole form works, but it interrupts the normal node submit. I tried using validate instead and got the same effect.
I have no idea, honestly.
I have no idea, honestly. But if you want to take a look at my implementation you can look at facebook_status.module... although I've been having some problems getting the _validate and _submit functions to run at all on that one lately.
I got some help from #drupal
Heine in #drupal chat told me to use hook_nodeapi() instead, once we tweaked it right, using case insert and case update and $node->xxxxx to get form variable data, it worked like a charm and was able to get rid of the need for a function_submit or ['#submit']
That's not a generic
That's not a generic solution and won't work for the original question about block configuration forms. But good to know nonetheless.
This was changed in D6
$form[#validate][] = 'your_validation_function';I'm plugging my validation function into FeedAPI's add/edit node form. I've got a couple of CCK ImageFields planted in that form as well that I need to validate in context with the form. When I look at the dump of the form array being passed into hook_form_alter I see:
Using the above approach generates:
... which is what you want. The += approach does this to that array: