Skip to main content
Share Your Experience: Take the 2024 Developer Survey
2 of 3
added 983 characters in body
jozi
  • 494
  • 1
  • 4
  • 14

How to load a variable through different nodeapi's $op?

I have a registration form on which I ask if user want to participate to a certain program ; if he clicks "yes", then i have a conditional field which requires to upload a picture. There are some problems for validating a picture with drupal, so I decided to create my own validation system.

If the user clicks on "yes" but doesn't upload a picture, then there must be an error message. If the user clicks on "no", the user can't upload a picture (thanks to conditional field).

For that, I've written this under a hook_nodeapi() function :

        if ($op=='validate'){
        if($node->type == 'profile'){
            // check if the user accepted or not
            if ($node->field_prof_firstflash[0]['value']=="Yes"){
                //check if there's an image loaded
                    if ($node->field_prof_img[0]['filesize'] == 0)
                        form_set_error('field_prof_img', t('Please upload a profile picture.'));
                }
            }
        }
    }

The problem comes when a user wants to edit his settings, later. If the user said "Yes" before but eventually wants to turn it off, my preceding code won't work anymore since $node->field_prof_firstflash[0]['value'] is still set to "Yes". Then, I thought about loading a variable when $op=='prepare' like :

        if ($op =='prepare'){
        $firstflash = $node->field_prof_firstflash[0]['value'];
        $img=$node->field_prof_img[0]['filesize'];

    }
    if ($op=='validate'){
        if($node->type == 'profile'){
            // check if the user accepted or not
            if ($firstflash=="Yes"){
                //check if there's an image loaded
                    if ($img == 0)
                        form_set_error('field_prof_img', t('Please upload a profile picture.'));
                }
            }
        }
    }

But of course (this would have been too simple :-) ), $firstflash is not loaded when $op=='validate', even with that piece of code...

Does anyone has any idea on how to do that ?

Any help is highly appreciated !

ANSWER : Include the following code (and adapt it of course) in your own custom module, it works perfectly!

function MYMODULE_form_alter(&$form, $form_state, $form_id) {
  if ($form_id == 'profile_node_form' || $form_id =='user_register') {
    //dpm($form);
    $form['#after_build'][] = 'MYMODULE_after_build';
    


  }
}

function MYMODULE_after_build($form, &$form_state) {


$form['field_prof_firstflash']['#element_validate'] = array('MYMODULE_validate','optionwidgets_validate','dependent_fields_element_validate');
dpm($form);

return $form;


}

function MYMODULE_validate($form, &$form_state) {
  $fs=$form_state['values']['field_prof_firstflash']['value'];
    $img=$form_state['values']['field_prof_img'][0]['filesize'];
  if ($fs == "Yes" && $img==0) {
    form_set_error('field_prof_firstflash', "Alert message !");
  }
}
jozi
  • 494
  • 1
  • 4
  • 14