validationdrupal-7drupal-form-validation

drupal_validate_form not validating more than one time on listing page


i am using drupal_validate_form on a node listing page . it is validating it correctly only for 1st item after that it is not checking validation.

here is my code

     foreach($result as $r){
        $node_form = (object) array(
          'uid' => $user->uid,
          'type' => 'MY_CONTENT_TYPE',
          'language' => LANGUAGE_NONE,
        );
        $form = drupal_get_form('MY_CONTENT_TYPE_node_form',$node_form);
        $form['#submit'] = array('#type' => 'submit', '#value' => t('Next'));

        $old_fs = @unserialize($r->form_state);
        $old_fs['values']['uid'] = $user->uid;
        $node = (object) array(
          'uid' => $user->uid,
          'type' => 'MY_CONTENT_TYPE',
          'language' => LANGUAGE_NONE,
        );
        node_object_prepare($node);
        $form_state = array();
        $form_state['build_info']['args'] = array($node);
        $form_state['values'] = $old_fs['values'];
        $form_state['values']['op'] = t('Save');
        $form_state['submitted'] = 1;
        $form_state['complete form'] = array();
        $form_state['triggering_element'] = array('#parents'=>array('next'),'#button_type'=>'submit');
        unset($form['#token']);
        drupal_validate_form('MY_CONTENT_TYPE_node_form', $form, $form_state);
        $errors = form_get_errors();
        $noOfError = 'empty';
        if (!empty($errors)) {
          $noOfError = count($errors);
        } 
        form_clear_error();
    }

thank You in advance


Solution

  • Finally i have to reset need_validation in $form . Because every time form get for validation it will chack $form['#needs_validation'] in _form_validate(); so i added another line after

    $form = drupal_get_form('MY_CONTENT_TYPE_node_form',$node_form);
    $form['#needs_validation'] = TRUE;
    

    and it will validate every form in that loop