turn off "required" to a field

gafir777 - January 22, 2009 - 20:36
Project:Validation API
Version:6.x-1.0
Component:Code
Category:support request
Priority:critical
Assigned:Unassigned
Status:closed
Description

Hi,

I've created a custom content type using both the hook_form and CCK with three fields, a radios set to select a category, and depending on the category selected make the two other fields (Filefields) required or not required. These two fields are a filefield for pdf documents, and another filefield for images.
if the category selected (with radio button) is "image", then I would like to make the pdf filefied not required, and the image upload filefield required, if "document" is selected, I would like to reverse this. Is this something Validation API can do? I did not find any information about this.

Thank you!

#1

gafir777 - January 23, 2009 - 03:27

Taking the example from a previous post, here is somehow what I'd like to do:

if ( $form_state['values']['subject'] == 'document') {
   // here set filefield 2 as not required
  // set filefield 1 as required
}
else if  ( $form_state['values']['subject'] == 'image') {
   // here set filefield 1 as not required
  // set filefield 2 as required
}

I'm not sure how to define the $form_state, and even less how to change the "required" setting of the fields. Is it the same syntax than for the form API?

Thanks,

#2

TapocoL - January 23, 2009 - 06:13
Status:active» fixed

Required is a option in Drupal's core FAPI, and is tested automatically in Drupal before the validation rules are applied in my module. So, first off you would not want to enable/disable the required option in the FAPI from my module.

Here is a way I was able to set it up on my localhost. I used textfields instead of filefields, so make appropriate modifications:

Set the CCK textfields as not required in CCK settings, and CCK radio field as required.

Setup a validator using the code above. Just add the checks for empty on each field:

if ($value == 'choose1') {
  return (!empty($form_state['values']['field_test1'][0]['value']));
} else if ($value == 'choose2') {
  return (!empty($form_state['values']['field_test2'][0]['value']));
}
return false;

$value: radio button's passed value
'choose1': value of first selection for radiogroup
'field_test1': CCK name of first textfield
'choose2': value of second selection for radiogroup
'field_test2': CCK name of second textfield

Attach this validator to the CCK radio field. And, it was working. I don't know how filefields are determined to be empty, so you may need to find out if an !empty check is correct and which field to check for that.

Hope this helps.

#3

System Message - February 6, 2009 - 06:20
Status:fixed» closed

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

 
 

Drupal is a registered trademark of Dries Buytaert.