0

每当我单击提交按钮时,表单都会被清除,并且不会执行任何内部代码,尽管我正在设置 drupal 消息或尝试发送电子邮件。我已经用“user@domain.com”替换了我的电子邮件等。

<?php

/**
 *  implementation of help hook
 */
function module_test_help($path, $arg) {
  switch ($path) {    // if the path is equaled to case, return value.
    case 'admin/help#module_test':
      $output .= '<p>' . t('Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam quis dictum turpis.') . '</p>';
      return $output;
     break;
  }
}

/**
 *  implementation of menu hook
 */
/** function module_test_menu() {
*   $items = array();
*   $items['lorem-ipsum'] = array (  
*   'title' => t('Lorem-ipsum'),  
*   'page callback' => '_module_test_page',
*   'access arguments' => array('administer my module'),  
*   'type' => MENU_CALLBACK, 
*   );
*   return $items; 
*   }
**/

function module_test_menu() {
  $items = array();
  $items['validation_form'] = array (  
  'title' => t('Validation Form'),  
  'page callback' => 'drupal_get_form',
  'page arguments' => array('module_test_form'),
  'access arguments' => array('administer my module'),  
  'type' => MENU_NORMAL_ITEM, 
   );
   return $items; 
}

 /**
*   implementing permissions
**/ 
function module_test_perm() {
  return array('Administer my module');
}  

/**
*   implementing forms with validations
**/ 
function module_test_form() {
  $form['name'] = array(
   '#title' => t('Name'),   
   '#type' => 'textfield',
   '#size' => '25',
   '#required' => FALSE,
   );

   $form['email'] = array(
   '#title' => t('E-mail'),
   '#type' => 'textfield',
   '#size' => '25',   
   '#required' => FALSE,
   '#element_validate' => array('module_test_validate'),
   );

   $form['message'] = array(
   '#title' => t('Message'),
   '#type' => 'textarea',
   '#required' => FALSE,
   );

   $form['checkbox'] = array(
   '#title' => t('Send yourself a copy'),
   '#type' => 'checkbox', 
   '#required' => FALSE,
   '#return_value' => 1,
   '#default_value' => 0
   );

   $form['submit'] = array(
   '#type' => 'submit',
   '#value' => t('Submit')
   );
   return $form;//
}

/**
*   Validation of e-mail form
**/
function module_test_validate($form, &$form_state) {
  $valid_email = $form_state['values']['email'];
    if(!valid_email_address($valid_email)) {
     form_set_error('email', 'The email address "' . $valid_email . '" is invalid.');
    }
}

/**
* implementing mail function
*/
function module_test_mail($key, &$message, $params) {

  $headers = array(
    'MIME-Version' => '1.0',
    'Content-Type' => 'text/html; charset=UTF-8; format=flowed',
    'Content-Transfer-Encoding' => '8Bit',
    'X-Mailer' => 'Drupal'
  );

  foreach ($headers as $key => $value) {
    $message['headers'][$key] = $value;
  }

  $message['subject'] = $params['subject'];
  $message['body'] = $params['body'];
}



/**
* Create the form submit function
*/
function module_test_submit($form, &$form_state) {


    //main server details
    ini_set("SMTP", "mail.host");
    //smtp port number
    ini_set("smtp_port", "25");
    //send from address
    ini_set("sendmail_from","user@domain.com");

    $admin_email = 'user@domain.com';
    $valid_name = $form_state['values']['name'];
    $valid_email = $form_state['values']['email'];
    $valid_message = $form_state['values']['message'];
    $valid_check = $form_state['values']['checkbox'];

    $from = 'user@domain.com';
    $body = 'Name: ' . $valid_name;
    $body = 'Email: ' . $valid_email;
    $body = 'Message: ' . $valid_message;

    $params = array(
    'body' => $body,
    'subject' => 'ModuleTest Form Confirmation ',
    );

    if($valid_check == 1){
        if (drupal_mail('module_test_form', 'some_mail_key', $valid_email, language_default(), $params, $from, $send = TRUE))
        {
            form_set_error('checkbox', 'A copy has been sent to ' .$valid_email .'');
        }
    }   
    if (drupal_mail('module_test_form', 'some_mail_key', $valid_email, language_default(), $params, $from, $send = TRUE))
    {
        drupal_set_message('An email has been sent to ' . $admin_email);      
    } else {
        drupal_set_message('There was an error sending your email');
    }

}
/**
* Return the form.
*/
return drupal_get_form('module_test_form');
?>
4

2 回答 2

0

您的代码有几个问题,但是您的提交处理程序不起作用的原因仅仅是因为您有错误的命名约定。

让我们澄清所有这些:

您已将电子邮件字段设置为不需要,然后假设电子邮件地址字段在验证功能中可用。将其更正为:

$form['email'] = array(
   '#title' => t('E-mail'),
   '#type' => 'textfield',
   '#size' => '25',   
   '#required' => TRUE,
   );

我们现在允许标准验证功能来验证电子邮件地址。

您的验证功能在清理用户输入方面存在问题。将其更正为:

function module_test_validate($form, &$form_state) {
  $valid_email = $form_state['values']['email'];
    if(!valid_email_address($valid_email)) {
     form_set_error('email', t('The email address "@email" is invalid.', array('@email' => $valid_email)));
    }
}

您不能在打开的 .module 文件中返回任何内容。删除这个:

/**
* Return the form.
*/
return drupal_get_form('module_test_form');

像这样重命名验证函数:

function module_test_form_validate($form, &$form_state) {

并且您的提交功能有几个问题。主要是它的名字。

/**
* Create the form submit function
*/
function module_test_form_submit($form, &$form_state) {


    //main server details
    ini_set("SMTP", "mail.host");
    //smtp port number
    ini_set("smtp_port", "25");
    //send from address
    ini_set("sendmail_from","user@domain.com");

    $admin_email = 'user@domain.com';
    $valid_name = $form_state['values']['name'];
    $valid_email = $form_state['values']['email'];
    $valid_message = $form_state['values']['message'];
    $valid_check = $form_state['values']['checkbox'];

    $from = 'user@domain.com';
    $body[] = 'Name: ' . check_plain($valid_name);
    $body[] = 'Email: ' . $valid_email;
    $body[] = 'Message: ' . nl2br(check_plain($valid_message));
    $body = implode('<br />', $body);
    $params = array(
    'body' => $body,
    'subject' => 'ModuleTest Form Confirmation ',
    );

    if($valid_check == 1){
        if (drupal_mail('module_test', 'some_mail_key', $valid_email, language_default(), $params, $from))
        {
            drupal_set_message(t('A copy has been sent to @email', array('@email' => $valid_email)));
        }
    }   
    if (drupal_mail('module_test', 'some_mail_key', $valid_email, language_default(), $params, $from))
    {
        drupal_set_message(t('An email has been sent to @email', array('@email' => $admin_email)));      
    } else {
        drupal_set_message('There was an error sending your email', 'error');
    }

}

好的,下面是您可以复制粘贴的模块文件的完整代码。它有效(我测试过)。尝试找出必要的更改。尽可能/合适地使用 t() 函数。

<?php

/**
 *  implementation of help hook
 */
function module_test_help($path, $arg) {
  switch ($path) {    // if the path is equaled to case, return value.
    case 'admin/help#module_test':
      $output .= '<p>' . t('Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam quis dictum turpis.') . '</p>';
      return $output;
     break;
  }
}

/**
 *  implementation of menu hook
 */
/** function module_test_menu() {
*   $items = array();
*   $items['lorem-ipsum'] = array (  
*   'title' => t('Lorem-ipsum'),  
*   'page callback' => '_module_test_page',
*   'access arguments' => array('administer my module'),  
*   'type' => MENU_CALLBACK, 
*   );
*   return $items; 
*   }
**/

function module_test_menu() {
  $items = array();
  $items['validation_form'] = array (  
  'title' => t('Validation Form'),  
  'page callback' => 'drupal_get_form',
  'page arguments' => array('module_test_form'),
  'access arguments' => array('administer my module'),  
  'type' => MENU_NORMAL_ITEM, 
   );
   return $items; 
}

 /**
*   implementing permissions
**/ 
function module_test_perm() {
  return array('Administer my module');
}  

/**
*   implementing forms with validations
**/ 
function module_test_form() {
  $form['name'] = array(
   '#title' => t('Name'),   
   '#type' => 'textfield',
   '#size' => '25',
   '#required' => FALSE,
   );

   $form['email'] = array(
   '#title' => t('E-mail'),
   '#type' => 'textfield',
   '#size' => '25',   
   '#required' => TRUE,
   );

   $form['message'] = array(
   '#title' => t('Message'),
   '#type' => 'textarea',
   '#required' => FALSE,
   );

   $form['checkbox'] = array(
   '#title' => t('Send yourself a copy'),
   '#type' => 'checkbox', 
   '#required' => FALSE,
   '#return_value' => 1,
   '#default_value' => 0
   );

   $form['submit'] = array(
   '#type' => 'submit',
   '#value' => t('Submit')
   );
   return $form;//
}

/**
*   Validation of e-mail form
**/
function module_test_form_validate($form, &$form_state) {
  $valid_email = $form_state['values']['email'];
    if(!valid_email_address($valid_email)) {
     form_set_error('email', 'The email address "' . $valid_email . '" is invalid.');
    }
}

/**
* implementing mail function
*/
function module_test_mail($key, &$message, $params) {
  // these do not work in Drupal 7 //
  $headers = array(
    'MIME-Version' => '1.0',
    'Content-Type' => 'text/html; charset=UTF-8; format=flowed',
    'Content-Transfer-Encoding' => '8Bit',
    'X-Mailer' => 'Drupal'
  );

  foreach ($headers as $key => $value) {
    $message['headers'][$key] = $value;
  }

  $message['subject'] = $params['subject'];
  $message['body'] = $params['body'];
}



/**
* Create the form submit function
*/
function module_test_form_submit($form, &$form_state) {


    //main server details
    ini_set("SMTP", "mail.host");
    //smtp port number
    ini_set("smtp_port", "25");
    //send from address
    ini_set("sendmail_from","user@domain.com");

    $admin_email = 'user@domain.com';
    $valid_name = $form_state['values']['name'];
    $valid_email = $form_state['values']['email'];
    $valid_message = $form_state['values']['message'];
    $valid_check = $form_state['values']['checkbox'];

    $from = 'user@domain.com';
    $body[] = 'Name: ' . $valid_name;
    $body[] = 'Email: ' . $valid_email;
    $body[] = 'Message: ' . nl2br($valid_message);
    $body = implode('<br />', $body);
    $params = array(
    'body' => $body,
    'subject' => 'ModuleTest Form Confirmation ',
    );

    if($valid_check == 1){
        if (drupal_mail('module_test', 'some_mail_key', $valid_email, language_default(), $params, $from))
        {
            drupal_set_message(t('A copy has been sent to @email', array('@email' => $valid_email)));
        }
    }   
    if (drupal_mail('module_test', 'some_mail_key', $valid_email, language_default(), $params, $from))
    {
        drupal_set_message(t('An email has been sent to @email', array('@email' => $admin_email)));      
    } else {
        drupal_set_message('There was an error sending your email', 'error');
    }

}
于 2012-11-21T01:42:15.980 回答
0

缺少一个 } 来关闭你的函数 module_test_submit($form, &$form_state) {

希望能帮助到你

公关

于 2012-11-20T16:14:50.293 回答