Please excuse me if this has been answered elsewhere but I did spend quite a bit of time looking and reading different posts and still couldn't accomplish what I'm trying to. Also, I have tried the Conditional Fields plugin as this seemed to address exactly what I was looking for, but unfortunately it does not seem to work with making a file upload field required.
The problem is this:
I have a custom content type (workshop proposal), wherein the user is required to enter information for the "Lead Presenter". This information consists of a name, email, address (all text), and a resume/cv (file upload). The user also has the option to enter information for a second (third, fourth, and fifth) presenter. None of these can be required as there may only be one presenter for any given proposal, but we do want to require that if someone starts to complete the information for a second presenter that they enter ALL the information associated with that presenter.
I was thinking that custom module might do the trick, but I haven't had any luck with getting it to work.
<?php
// $Id$
/**
* @file
* Simple module to run some custom validation on a Workshop Proposal node
*/
/*
* Implementation of hook_form_alter()
*
*/
function workshop_proposal_form_alter(&$form, $form_state, $form_id) {
if ($form_id == 'workshop_proposal') {
$form['#validate'] = array('_workshop_proposal_form_validate');
}
return $form;
}
/*
* Validates the Workshop Proposal form, and throws an error to the user if validation fails
*
*/
function _workshop_proposal_form_validate($form, &$form_state)
{
form_set_error('Yup. This is an error');
}
?>Yeah, I couldn't even output the error without the actual validation I was trying to do.
Any help is much appreciated. Thanks.
Comments
In Drupal 7, you should be
In Drupal 7, you should be able to use AJAX to change the #required field of the second form element when the first one is modified. You can even hide the second element if you want.
So, the user fills in the first field. As soon as he does, AJAX updates the part of the form with the second field to make it required. It's pretty easy, and you don't have to mess with your validate function.
The same thing should be possible with Drupal 6, but I'm not that familiar with it.
If you don't want to use AJAX, you can just put a check in your validation function. Again, I don't know how to do this with Drupal 6, but with Drupal 7, it would be something like this (completely untested code):
<?phpfunction your_form_validate($form, &$form_state) {
if (!empty($form_state['values']['weight']) && empty($form_state['values']['weight_units'])) {
form_set_error('weight_units', t('If you enter a weight, you must enter weight units, too.'));
}
}
?>
--
www.ztwistbooks.com. Math books that are actually fun.
Also, you probably don't want
Also, you probably don't want to do this in your *_form_alter():
<?php$form['#validate'] = array('_workshop_proposal_form_validate');
?>
That will override any existing validation functions. Unless that's really what you want, do this:
<?php$form['#validate'][] = '_workshop_proposal_form_validate';
?>
That will append '_workshop_proposal_form_validate' to the end of the array, so it gets executed after any existing validation functions.
--
www.ztwistbooks.com. Math books that are actually fun.