I am creating a module that can store user input in db and populate in a listing page with validation. I am collecting all values before submit i.e. any change in any field I store the form state in database, next I need to validate it on a listing page which will validate those input and give the error.

I have tried node_validate_form & drupal_validate_form but it's not working.

I think drupal_validate_form will do what I want but it omit default field validation like required validation or email validation are not validating properly and also in listing page it only validate the 1st one after that it is not checking any validation.

$header = array(
    array('data' => t('Field')),
    array('data' => t('Error')),
  $rows = array();
  $owner = '';
  $result = db_select('MY_MODULE', 'lt')
    ->fields('lt', array('owner', 'form_state', 'form_state_input'))
    ->condition('lid', $lid)

  if (!$result) {
    $rows[] = array(array('data' => t('No Result found.'), 'colspan' => 10));
  else {
    $old_msg = drupal_get_messages();
    $_SESSION['messages'] = array();
    $node_form = (object) array(
      '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'));
    foreach($result as $r){
        $owner = l(t(user_load($r->owner)->name), 'user/'.$r->owner);
        $old_fs = @unserialize($r->form_state);
        $form_state_input = @unserialize($r->form_state_input);
        $old_fs['values']['uid'] = $user->uid;
        $form_state = array();
        $form_state['values'] = array_merge($old_fs['values'],$form_state_input);
        $form_state['values']['op'] = t('Save');
        $form_state['submitted'] = 1;
        $form_state['complete form'] = array();
        $form_state['rebuild'] = TRUE;
        $form_state['triggering_element'] = array('#parents'=>array('next'),'#button_type'=>'submit');
        drupal_validate_form('MY_CONTENT_TYPE_node_form', $form, $form_state);
        $errors = form_get_errors();
        $noOfError = 'empty';
        if (!empty($errors)) {
          $noOfError = count($errors);
          foreach($errors as $field => $error){
            $field_name = explode(']',$field,2);
            $info = field_info_instance('node',$field_name[0],'MY_CONTENT_TYPE');
            $rows[] = array(
                    array('data' => $info['label']),
                    array('data' => $error),
          $rows[] = array(array('data' => t('No Error found.'), 'colspan' => 10));
    $_SESSION['messages'] = $old_msg;
  $build = array(
    '#theme' => 'table',
    '#header' => $header,
    '#rows' => $rows,
    '#attributes' => array('id' => 'MY_MODULE'),

Thank You In Advance....:)



drupal submit form hook does the magic

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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