2

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?

2 Answers 2

0

At first sight, it looks like you would be better off implementing hook_form_alter, adding a custom validation handler. A form validation handler will give you access to the submitted values in $form_state as well as the previously stored values.

2
  • Thanks, I followed your comment ! For people interested, I display the code in the edited part of my question :-)
    – jozi
    Nov 25, 2011 at 22:37
  • That's great to hear. Thanks for sharing your code when it's finished. Nov 25, 2011 at 22:43
0

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 !");
  }
}

Source: original OP edit.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.