How can i set error message in right place drupal 7?

721 views Asked by At

need help with drupal 7. I'm newbie with drupal.

I have code below for validation and validation errors, and it works fine. But errors always appear in wrong place.

I think the problem in $element, but i'm note sure.

some example html

    <form id="webform_client_form_226">
<div class="catalogue__form-input-inner">
    <div class="catalogue__form-input">
        <div class="form-item">
            
            // i need it here
            
            <label for="edit-submitted-vashe-imya--3">Your name</label>
            <input name="submitted[vashe_imya]" value="" size="60" maxlength="128" class="form-text error">
        </div>
    </div>
    
    <div class="catalogue__form-input">
        <div class="form-item webform-component webform-component-textfield webform-component--nomer-telefona webform-container-inline">

            // and here

            <label>Phone number</label>
            <input name="submitted[nomer_telefona]">
        </div>
    </div>
    
    <div class="catalogue__form-button">

    // always appears here right now

        <div class="form-actions">
            <input name="op" value="send"></div>        
        </div>
    </div>
</div>

and template.php

function pkpro_uikit_form_alter(&$form, &$form_state, $form_id) {
    // Check the form id
    if($form_id == 'webform_client_form_226') {
    $form['#validate'][] = 'form_validate';
    }
    }
        
    function form_validate(&$form, &$form_state) {
    $message = "Field required";
    
    if(isset($form_state['values']['submitted']['vashe_imya'])) {
    $name = $form_state['values']['submitted']['vashe_imya'];
    if( condition ) {
    form_error($form['submitted']['vashe_imya'], $message);
    }
    }
    if(isset($form_state['values']['submitted']['nomer_telefona'])) {
    $name = $form_state['values']['submitted']['nomer_telefona'];
    if( condition ) {
    form_error($form['submitted']["nomer_telefona"], $message);
    }
    }
      // get all validation errors. 
      $form_errors = form_get_errors();
      
      if (!empty($form_errors)) {
        foreach ($form_errors as $element => $error) {
          $form[$element]['#suffix'] = '<div>'.$error.'</div>';
        }
      }
      // clear default error messages.
      drupal_get_messages('error');    
    }

Help, pls)

1

There are 1 answers

0
rovr138 On

You need to modify your template. Look for page.tpl.php. It should have a $message variable on it. Move it to the correct location. If you only want it for one content type or page, copy the page.tpl.php so it only targets what you want.