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-」を表示する方法に最も興味がありますが、ユーザーにそれを選択肢として選択させないでください。この時点で、必須フィールドに-Select-を残すことができ、Drupalはフォームの送信を許可します。その特定のフィールドについて電子メールで送信されたデータ=0。ユーザーに実際に状態を選択させたい。

私は以下のすべての方法を試しました。

私はこれらの複数のコードの試行錯誤のバリエーションを試しましたが、宣伝されているように機能するものはありません...

明確化のためのあなたの質問またはあなたの提案は大歓迎です。説明は具体的かつ単純にすることを忘れないでください。用語を定義してください。現在、私にとってphpのほとんどはシェイクスピアを読むようなものです。
本当にありがとう。

4

2 に答える 2

0

私はこの問題について眠り、それから以前に試したリンクの1つを再試行しました。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

これを回避する別の方法があります。

最初のオプションにketとしてNULL値を割り当てるだけです。

元:

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

これで十分です。

于 2014-04-28T07:27:00.870 に答える