0

I have a form wich allow to create content, it's build like :

  1. get the owner part in a form
    • if the user isn't connected show him a login form and registration form
    • if the user is connected show him the user profile form to edit his informations
  2. get the content form
    • include 1 by drupal get form
    • add an ajax system to add/remove lines of content creation

But when i clic on add or remove button (before i commented the user form and it works perfectly) i get this ajax error : Error : Call to undefined function user_profile_form_validate() dans form_execute_handlers() (ligne 1519 dans /Applications/MAMP/htdocs/fritr/includes/form.inc).

I've seen this post wich may contain the solution : https://www.drupal.org/node/1229882 Maybe it's because i don't use a form_load_include() but i don't know what include...

Here is my code :

/*

THE USER FORM

*/

function fritr_register_friterie_owner_form($form, &$form_state) { 
    // GET THE USER ENTITY IN FORM
    global $user;
    global $language;
    if(user_is_logged_in())
        $user = user_load($user->uid);
    form_load_include($form_state, 'inc', 'user', 'user.pages');

    // MAIN
    $form = array(
        '#type' => 'fieldset',            
        '#tree' => TRUE,
        '#collapsible' => FALSE,
        '#collapsed' => FALSE,            
        '#attributes' => array(
            'class' => array('clean-panel'),
        ),
    );

    // SHOW LOGIN FORM IF USER ISN'T CONNECTED
    $form['connexion'] = array(
        '#type' => 'fieldset',
        '#title' => t('Se connecter'),
        '#tree' => TRUE,
        '#collapsible' => FALSE,
        '#collapsed' => FALSE,
        '#weight' => 1,
        '#attributes' => array(
            'class' => array('clean-panel titled'),
        ),
    );    

    // USER FORM
    $form['user'] = array(
        '#type' => 'fieldset',
        '#title' => t('Editer ses informations'),
        '#tree' => TRUE,
        '#collapsible' => FALSE,
        '#collapsed' => FALSE,
        '#weight' => 2,
        '#attributes' => array(
            'class' => array('clean-panel titled'),
        ),
    );

    if (user_is_logged_in()) {        
        unset($form['connexion']);
        $form['user']['#title'] = t('Editer ses informations');
        $form['user']['content'] = drupal_get_form('user_profile_form', $user);            
        $form['user']['content']['#validate'] = array();
    } else {
        $form['connexion']['content'] = drupal_get_form('user_login');
        $form['user']['#title'] = t('Créer un compte');
        $form['user']['content'] = drupal_get_form('user_register_form');           
    }                      

    return $form;
}

function fritr_register_friterie_connexion_form_submit($form, &$form_state) {    
    $form_state['redirect'] = current_path();
}

/*

THE FRITERIE FORM WICH CONTAIN USER FORM

*/

/*
 * Implements hook_form().
 */
function fritr_register_friterie_form($form, &$form_state) {
    module_load_include('inc', 'fritr_register_friterie');
    form_load_include($form_state, 'inc', 'user', 'user.pages');    

    // USER REGISTER FORM       
    $form['owner'] = drupal_get_form('fritr_register_friterie_owner_form');
    $form['owner']['#weight'] = 2;

    $form['friteries'] = array(
        '#type' => 'fieldset',
        '#title' => t('Créer des friteries'),
        '#tree' => TRUE,
        '#collapsible' => FALSE,
        '#collapsed' => FALSE,
        '#weight' => 3,
        '#attributes' => array(
            'class' => array('clean-panel titled'),
        ),
    );    
    $form['friteries']['content'] = array(
        '#type' => 'container',
        '#weight' => 80,
        '#tree' => TRUE,        
        '#prefix' => '<div id="wrapper-friterie">',
        '#suffix' => '</div>',
    );

    $form_state['friterie_deltas'] = isset($form_state['friterie_deltas']) ? $form_state['friterie_deltas'] : range(0, 0);
    $field_count = $form_state['friterie_deltas'];

    foreach ($field_count as $delta) {
        $form['friteries']['content'][$delta] = array(
            '#type' => 'fieldset',            
            '#tree' => TRUE,
            '#collapsible' => FALSE,
            '#collapsed' => FALSE,
            '#weight' => 1,
            '#attributes' => array(
                'class' => array('clean-panel'),
            ),
        );

        // FRITERIE FORM
        $form['friteries']['content'][$delta]['remove_friterie'] = array(
            '#type' => 'submit',
            '#value' => t('-'),
            '#submit' => ['fritr_register_friterie_remove'],
            '#limit_validation_errors' => array(),            
            '#weight' => 1,
            '#ajax' => array(
                'callback' => 'fritr_register_friterie_remove_callback',
                'wrapper' => 'wrapper-friterie',
            ),            
            '#attributes' => array(
                'class' => ['button-small'],
            ),
            '#name' => 'remove_name_' . $delta,
        );
        $form['friteries']['content'][$delta]['friterie_fields'] = array(
            '#type' => 'fieldset',
            '#title' => t('Informations friterie (cliquez pour basculer l\'affichage)'),
            '#tree' => TRUE,
            '#collapsible' => TRUE,
            '#collapsed' => FALSE,
            '#weight' => 2,
            '#attributes' => array(
                'class' => array('clean-panel titled underline collapsible'),
            ),
        );                

        if (!isset($form['friteries']['content'][$delta]['#node'])) {
            global $language;
            $form['friteries']['content'][$delta]['#node'] = entity_create('node', array('type' => 'friterie', 'language' => $language->language));
        }
        field_attach_form('node', $form['friteries']['content'][$delta]['#node'], $form['friteries']['content'][$delta]['friterie_fields'], $form_state);        
    }

    $form['friteries']['content']['add_friterie'] = array(
        '#type' => 'submit',
        '#value' => t('Ajouter une friterie'),
        '#submit' => ['fritr_register_friterie_add'],
        '#limit_validation_errors' => array(),
        '#ajax' => array(
            'callback' => 'fritr_register_friterie_add_callback',
            'wrapper' => 'wrapper-friterie',
        ),
        '#weight' => 100,
    );       

    $form['#submit'][] = 'fritr_register_friterie_form_submit';
    $form['submit'] = array(
        '#type' => 'submit',
        '#value' => t('Enregistrer la friterie'),
        '#weight' => 100,
    );         

    dsm($form);
    dsm($form_state);

    return $form;
}

/*

THE AJAX AND SUBMIT CALLBACK OF BUTTONS WICH ADD OR REMOVE A FRITERIE NODE FORM CREATION

*/

function fritr_register_friterie_remove($form, &$form_state) {
    $delta_remove = $form_state['triggering_element']['#parents'][1];
    $k = array_search($delta_remove, $form_state['friterie_deltas']);
    unset($form_state['friterie_deltas'][$k]);

    $form_state['rebuild'] = TRUE;
    drupal_get_messages();
}

function fritr_register_friterie_remove_callback($form, &$form_state) {
    return $form['friteries']['content'];
}

function fritr_register_friterie_add($form, &$form_state) {
    $form_state['friterie_deltas'][] = count($form_state['friterie_deltas']) > 0 ? max($form_state['friterie_deltas']) + 1 : 0;

    $form_state['rebuild'] = TRUE;
    drupal_get_messages();
}

function fritr_register_friterie_add_callback($form, $form_state) {
    return $form['friteries']['content'];
}

2 Answers 2

1

You need to use form_load_include instead of module_load_include in forms:

Use this function instead of module_load_include() from inside a form constructor or any form processing logic as it ensures that the include file is loaded whenever the form is processed. In contrast to using module_load_include() directly, form_load_include() makes sure the include file is correctly loaded also if the form is cached.

e.g.

form_load_include($form_state, 'inc', 'user', 'user.pages');
7
  • This not work, i edit my code in my post but i have the same error, no changes.
    – Kpone
    Commented Aug 12, 2016 at 12:39
  • You'll have to be more specific, "not work" isn't useful feedback. You should cut the code down in your question to only the pertinent parts as well, people (including me) aren't going to read through 100+ lines scanning for possible errors. Get your code down to the smallest you can to reproduce the problem
    – Clive
    Commented Aug 12, 2016 at 12:45
  • I can't be more precise ^^ I try the solution you give to me and this do nothing (i've the same error message and no new comportement). In the function fritr_register_friterie_owner_form, i change the module_load_include by your form_load_include and in function fritr_register_friterie_form i do the same thing (theses lines are at the start of theses functions you can tchek it yourself ;) )
    – Kpone
    Commented Aug 12, 2016 at 12:59
  • Your code contains definitions for two forms, yet you can only be receiving the error in one at a time. So saying that you can't be more precise isn't really true. As I mentioned, people aren't likely to wade through hundreds of lines of code to find where you've made a mistake. If you want to know what's wrong with your code, you need to isolate the parts that are causing the problem, and only present those. Remove all of the unrelated stuff. This means creating a SSCCE. That's the trade-off - you give us a well-formed question, we'll give you an answer to it :)
    – Clive
    Commented Aug 12, 2016 at 13:04
  • Agree @Clive, no supplement
    – Jimmy Ko
    Commented Aug 12, 2016 at 13:11
0

Please include the required handler with form_load_include:

form_load_include($form_state, 'inc', 'user', 'user.pages');

You may have below question.

How to find the correct file to include?

You can use the function name as key word (e.g. function user_profile_form_validate) and search in your Drupal project, then you can find the file contains its definition. It is easy be done in Sublime or other editors/IDEs.

1
  • This not work, i edit my code in my post but i have the same error, no changes.
    – Kpone
    Commented Aug 12, 2016 at 12:40

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.