0

作为一个新手并使用 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 就像阅读莎士比亚。
非常感谢。

4

2 回答 2

0

我在这个问题上睡了一觉,然后重试了我之前尝试过的链接之一。Drupal 表单验证对我不起作用

我能够让它令我满意:我在设置 $order 值后立即在函数 request_catalog_form_local_contact_page_alter(&$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'];
            }
        }

}

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

于 2013-03-22T13:46:12.353 回答
0

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

只需将 NULL 值作为 ket 分配给第一个选项。

前任:

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

这会做。

于 2014-04-28T07:27:00.870 回答