0

我在节点列表页面上使用 drupal_validate_form。它仅对第一个项目进行正确验证,之后它不检查验证。

这是我的代码

     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();
    }

先感谢您

4

1 回答 1

0

最后我必须在 $form 中重置 need_validation 。因为每次表单获取验证时它都会$form['#needs_validation']进入_form_validate();所以我在之后添加了另一行

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

它将验证该循环中的每个表单

于 2014-09-19T08:13:03.137 回答