Closed (won't fix)
Project:
FileField
Version:
6.x-3.x-dev
Component:
Code
Priority:
Normal
Category:
Task
Assigned:
Reporter:
Created:
18 Jun 2010 at 20:09 UTC
Updated:
6 Dec 2010 at 02:01 UTC
It would be nice to allow other modules to add additional validators that can fire directly upon file upload, rather than doing it in hook_nodeapi (op = validate) or using sneaky tricks. To my knowledge, this is not currently possible, so I added a hook called hook_filefield_validators(). Patch attached.
| Comment | File | Size | Author |
|---|---|---|---|
| filefield_additional_validators.patch | 645 bytes | cwgordon7 |
Comments
Comment #1
quicksketchYou can already add additional validators by implementing hook_elements(), then adding additional validators in a #process callback after FileField has expanded its widget. This is how modules like ImageField Extended and FileField Sources already add their own validation and additional functionality to FileField.
Comment #2
cwgordon7 commentedThat didn't occur to me, clever. Thanks! I guess this is by design then.
Comment #3
cwgordon7 commentedActually, I can't seem to make that work. Here is my hook_elements code:
And here is my process callback code:
I know the _process() callback is being called because one I put a drupal_set_message() in there, it shows up. When I print_r() the $element variable at the end of my _process() callback, it seems to have worked - the fourth #upload_validators validator is there. However, I don't think this is actually doing anything because a) my validate function is never called, and b) when I print out the form in hook_form_alter(), there are only the default three #upload_validators in the form. Any ideas as to what I'm doing wrong?
Thanks!
Comment #4
cwgordon7 commentedOk, I've done a bit of investigating, and here's what I think is the problem, which is why I believe the patch is necessary:
The validation is done in #value_callback. This is called before #process is called in http://api.drupal.org/api/function/_form_builder_handle_input_element/6. Therefore, there is no way for other modules to alter the #upload_validators FAPI property as far as I see. The other modules you referenced do not attempt to alter the #upload_validators property. Setting back to needs review.
Comment #5
quicksketchHmm sure enough. We may just be out of luck here. FileField has been moved into core and I'm not going to add any new APIs that don't have a corresponding piece in Drupal 7 core. My feeling is that this really doesn't belong in FileField anyway. If this functionality is needed for FileField, it'd also be needed for other widget types. I'd say the better way to go is to have a hook_field_widget_alter() that allows you to modify the widget before it goes into the form. Of course you'll have the same battle in CCK/Field module as you would here, both modules are frozen.
Comment #6
quicksketchMoving to won't fix as FileField cannot introduce new APIs that do not also exist in core.