Well i tried setting a bunch of $form required properties for my cck image field in after_build -> to no avail.

I did get the style to show the required * asterisks, but required validation is not triggered.

any ideas?

CommentFileSizeAuthor
#5 better_validation.patch699 bytesamelfe
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

quicksketch’s picture

File fields in Drupal do not support #required (this applies to all #type => 'file', not just those provided by FileField). It's a bug in Drupal core that has existed since Drupal 4.7. FileField works around this by providing it's own validation on the node form that does this checking manually. The only way to add your own required validation for different FileFields is to make another validation handler to the node form, then check if the field has a value.

So in short you'll need to add $form['#validate'][] = 'mymodule_node_form_validate' in your hook_form_alter(). Then see the filefield_node_form_validate() function in filefield_widget.inc and see how it returns a validation error if a file is not uploaded.

Hunabku’s picture

Thanks quicksketch - starting with your instructions i came up with the simplest route that works for me.

in mymodule_form_alter function

//at the end of form_alter i always put an after_build to handle cck fields, etc. 
$form['#after_build'][] = 'mymodule_after_build';

in mymodule_after_build function

//inside corresponding form id test statement goes the following two lines of code
$form['#validate'][] = 'mymodule_validate_image';
$form['field_myimage'][0]['#required'] = TRUE; // So the "required" asterisks shows up
//at the end of after_build remember to return $form 
return $form;

also in mymodule

function mymodule_validate_image ($form, &$form_state) {
  if (!$form_state['values']['field_myimage'][0]['fid']) {
    form_set_error('field_myimage', t('Please choose a file to upload.'));
  }
}
quicksketch’s picture

Status: Active » Fixed

Looks great to me! Marking this as fixed.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

amelfe’s picture

Category: support » task
Status: Closed (fixed) » Needs review
FileSize
699 bytes

Sorry for opening this old post, but wouldn't be better to change the validation process of the fllefield widget (filefield_node_form_validate()) so that the modifications in a form_alter (in $form['#field_info']) are taken into consideration.
Here is a patch to do that :

quicksketch’s picture

Sounds like a good idea to me. I'll review this when I get a chance.

quicksketch’s picture

Title: How to make filefield image required via form_alter? » Use $form['#field_info'] for required file validation
Version: 6.x-3.1 » 6.x-3.2
Status: Needs review » Fixed

Committed amefle's patch from #5. Works great. Thanks!

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

jduhls’s picture

Thanks for this! This helped me figure out how to programmatically make a field "required" that was not defined as such in the original field configuration. Still trying to figure out how to add that pesky asterisk, though.

teju’s picture

Title: Use $form['#field_info'] for required file validation » conditional required field...

hi friends,

Thanks for nice help. It works for me. I used it for conditional required field.
Here is the code

function scube_register_form_alter(&$form, &$form_state, $form_id) {
if($form_id='scube_register_account_formuser_login_block')
{
$form['#after_build'][] = 'scube_register_after_build';
}
}

function scube_register_after_build($form, &$form_state)
{
//inside corresponding form id test statement goes the following two lines of code

$form['#validate'][] = 'scube_register_validate_image';
$form['subject'][0]['#required'] = TRUE; // So the "required" asterisks shows up
$form['proficiency'][0]['#required'] = TRUE;
//at the end of after_build remember to return $form
return $form;

}

function scube_register_validate_image ($form, &$form_state) {
if (!$form_state['values']['subject']) {
form_set_error('subject', t('Subject field is required.'));
}
if (!$form_state['values']['proficiency']) {
form_set_error('proficiency', t('Proficiency field is required.'));
}
}

Thank you,
teju.
www.caarcher.biz