作为一个新手并使用 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。我想强制用户实际选择一个状态。
我已经尝试了以下所有方法。
- http://www.chromaticsites.com/blog/drupal-tutorial-form-overrides-and-element-specific-validations/
- Drupal 表单验证对我不起作用
- http://befused.com/drupal/additional-validation-function
我已经尝试了这些的多种代码试错变体,但没有任何效果像广告宣传的那样......
非常感谢您的澄清问题或建议。请记住在您的解释中要具体和简单,并请定义术语 - 目前对我来说,大多数 php 就像阅读莎士比亚。
非常感谢。