如何在' #type'上运行自定义验证=> '选择'自定义表单上的字段使用Drupal 6中的hook_form_Form_ID_alter()

时间:2013-03-08 14:39:11

标签: drupal-6 validation hook-form-alter

成为新手并使用hook_form_Form_ID_alter()更改站点范围的表单和字段是一项重大成就,但现在我开始遇到涉及验证我添加的一些自定义字段的问题。以下是我的 custom_form.module 文件中的代码。

<?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'];
    }
}

此时我最感兴趣的是如何在美国国家/地区下拉字段中显示“ - 选择 - ”但不要让用户选择它作为选择。此时他们可以保留-Select-在该必填字段中,Drupal允许表单提交。通过电子邮件发送的特定字段的数据= 0.我想强制用户实际选择一个州。

我尝试了以下所有方法。

我已经尝试了多个代码试验 - 这些错误的变化,并没有任何工作广告...

非常感谢您提出的澄清问题或建议。请记住在解释中要具体而简单并定义术语 - 目前对我来说,大多数PHP就像读莎士比亚一样。
非常感谢。

2 个答案:

答案 0 :(得分:0)

我睡了这个问题,然后重试了我之前尝试过的一个链接。 Drupal form validation not working for me

我能够让它让我满意:我在里面使用过它 在设置$ order值后立即调用request_catalog_form_local_contact_page_alter(&amp; $ form,$ form_state)。

    // 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'];
            }
        }

}

适用于我现在需要的东西。

答案 1 :(得分:0)

还有另一种方法可以避免这种情况。

只需将NULL值指定为第一个选项。

前:

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

这样做。