初心者であり、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-」を表示する方法に最も興味がありますが、ユーザーにそれを選択肢として選択させないでください。この時点で、必須フィールドに-Select-を残すことができ、Drupalはフォームの送信を許可します。その特定のフィールドについて電子メールで送信されたデータ=0。ユーザーに実際に状態を選択させたい。
私は以下のすべての方法を試しました。
- http://www.chaossites.com/blog/drupal-tutorial-form-overrides-and-element-specific-validations/
- Drupalフォームの検証が機能しない
- http://befused.com/drupal/additional-validation-function
私はこれらの複数のコードの試行錯誤のバリエーションを試しましたが、宣伝されているように機能するものはありません...
明確化のためのあなたの質問またはあなたの提案は大歓迎です。説明は具体的かつ単純にすることを忘れないでください。用語を定義してください。現在、私にとってphpのほとんどはシェイクスピアを読むようなものです。
本当にありがとう。