How to run custom validation on '#type' => 'select' field on custom form using hook_form_Form_ID_alter() in Drupal 6

StackOverflow https://stackoverflow.com/questions/15296492

سؤال

Being a newbie and altering the site-wide form and fields using hook_form_Form_ID_alter() was a big accomplishment but now I'm beginning to run into walls involving validation of some of the custom fields I've added. Following is the code from my custom_form.module file.

<?php

/**
 * Implementation of hook_form_Form_ID_alter(). 
 * @see http://api.drupal.org/api/function/hook_form_Form_ID_alter
 * Targets only request catalog form and adds a few custom fields to the default Drupal contact form and removes some fields.
 */

function states_list() {
  return array(
  '- Select -',
'AL' => 'Alabama',

 //rest of states....,

  );
}

function request_catalog_form_local_contact_page_alter(&$form, $form_state) {
    $form['parts_catalog'] = array(
        '#title' => t('Retail Parts Catalog'),
        '#type' => 'checkbox',
    );
    $form['sprayer_catalog'] = array(
        '#title' => t('Sprayer Parts Catalog'),
        '#type' => 'checkbox',
    );
    $form['address'] = array(
        '#title' => t('Address'),
        '#type' => 'textfield',
        '#required' => true,
    );
    $form['city'] = array(
        '#title' => t('City'),
        '#type' => 'textfield',
        '#size' => 30,
        '#required' => true,
    );
    $form['state'] = array(
        '#title' => t('State'),
        '#type' => 'select',
        '#options' => states_list(),
        '#required' => true,
    );
    $form['country'] = array(
        '#title' => t('Country'),
        '#type' => 'select',
        '#options' => array(
            'US' => t('United States'),
            'Canada' => t('Canada'),
        ),
        '#required' => true,
    );
    $form['zip'] = array(
        '#title' => t('Zip'),
        '#type' => 'textfield',
        '#size' => 6,
        '#maxlength' => 6,
        '#required' => true,
    );
    $form['phone'] = array(
        '#title' => t('Phone (xxx-xxx-xxxx)'),
        '#type' => 'textfield',
        '#size' => 12,
        '#maxlength' => 12,
        '#required' => true,
    );
    //Removes the textfield Message and the dropdown Subject choice.
    $form['message']['#access']=false;
    $form['subject']['#access']=false;

    //Remove the Send Email submit button text by setting it to nothing and changing it.
    $form['submit']['#title'] = '';
    $form['submit']['#value'] = 'Submit Your Request';

    // reorder the elements in the form to include the elements being inserted
    $order = array('parts_catalog', 'sprayer_catalog', 'name', 'address', 'city', 'state', 'country', 'zip', 'phone', 'mail', 'copy', 'submit');
    foreach($order as $key => $field) {
        $form[$field]['#weight'] = $key;
    }
}

// The custom email message sent using the data gathered in the form
function request_catalog_mail_alter(&$message) {
    if ($message['id'] == 'contact_page_mail') {
        $message['body'][1] = $message['params']['name'].' '.'has requested the following catalogs:';
        $message['body'][2] = 'Parts Catalog: ' . $message['params']['parts_catalog']. '     ' . 'Sprayer Parts Catalog: ' .$message['params']['sprayer_catalog'];
        $message['body'][4] = 'Send to:  ' . $message['params']['address'].', '. $message['params']['city'].', ' . $message['params']['state'].' '  . $message['params']['zip']. ' ' . $message['params']['country'];
    }
}

At this point I am most interested in how to show " - Select - " in the US States drop-down field BUT not let the user choose that as a choice. At this point they can leave -Select- in that required field and Drupal allows the form to submit. The data sent via email for that particular field = 0. I want to force the user to actually select a state.

I have tried all of the following methods.

I have tried multiple code trial-an-error variations of these and nothing has worked as advertized...

Your questions for clarification or your suggestions are greatly appreciated. Remember to be specific and simple in your explanations and define terms please - presently for me most of php is like reading shakespeare.
Thanks so much.

هل كانت مفيدة؟

المحلول

I slept on this problem and then retried one of the links I had tried before. Drupal form validation not working for me

I was able to make it work to my satisfaction: I used this within function request_catalog_form_local_contact_page_alter(&$form, $form_state) right after setting the $order value.

    // reorder the elements in the form to include the elements being inserted
    $order = array('parts_catalog', 'sprayer_catalog', 'name', 'address', 'city', 'state', 'country', 'zip', 'phone', 'mail', 'copy', 'submit');
    foreach($order as $key => $field) {
        $form[$field]['#weight'] = $key;
    }
    $form['#validate'][] = 'request_catalog_local_contact_page_validate';

        function request_catalog_local_contact_page_validate(&$form, &$form_state) {
          if ($form_state['values']['state'] == '0') {
            form_set_error('state', t('Please Select a State'));
          };
           if ($form_state['values']['parts_catalog'] == '0' and $form_state['values']['sprayer_catalog'] =='0') {
            form_set_error('parts_catalog' and 'sprayer_catalog', t('Please Select a Catalog'));
          };
        }

// The custom email message sent using the data gathered in the form
        function request_catalog_mail_alter(&$message) {
            if ($message['id'] == 'contact_page_mail') {
                $message['body'][1] = $message['params']['name'].' '.'has requested the following catalogs:';
                $message['body'][2] = 'Parts Catalog: ' . $message['params']['parts_catalog']. '     ' . 'Sprayer Parts Catalog: ' .$message['params']['sprayer_catalog'];
                $message['body'][4] = 'Send to:  ' . $message['params']['address'].', '. $message['params']['city'].', ' . $message['params']['state'].' '  . $message['params']['zip']. ' ' . $message['params']['country'];
            }
        }

}

Works for what I need now.

نصائح أخرى

There is another way you can avoid this.

Just assign NULL value as ket to the first option.

Ex:

 function states_list() {
   return array(
     '' =>'- Select -',
     'AL' => 'Alabama',
   //rest of states....,
   );
 }

This will do.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top