【问题标题】:Drupal - Make a validate function for cck formDrupal - 为 cck 表单创建验证功能
【发布时间】:2010-08-09 04:33:53
【问题描述】:

通过在我的模板中使用此功能,我为不同的内容类型创建了不同的节点表单:

function mytheme_theme($existing, $type, $theme, $path) {
  return array(
    'type1_form' => array(
        'arguments' => array('form' => NULL),
        'template' => 'type1_form'
    ),
    'type2_form' => array(
        'arguments' => array('form' => NULL),
        'template' => 'type2_form'
    ),
  );
}

现在我想为其中一个表单创建一个验证函数。我尝试在 template.php 中使用这个函数:

function mytheme_form_alter(&$form, $form_state, $form_id) {
   if($form_id == 'type1_form') {
     $form['#validate'][] = 'my_sample_validate_func';
   }
}

function my_sample_validate_func($form, &$form_state) {
   dsm($form_state);
}

但显然 hook_form_alter 不适用于主题层。我是否必须创建一个新模块才能完成此操作?

【问题讨论】:

    标签: php validation drupal forms


    【解决方案1】:

    hook_form_alter 必须放在模块中,而不是 template.php 文件中。对于我的大多数 Drupal 项目,我最终都会创建一个胶水模块来保存诸如 form_alter 函数之类的东西。

    这个Lullabot article 展示了如何使用 hook_form_alter 和在主题层中更改表单,但在我看来,在单独的模块中更容易做到这一点。

    【讨论】:

    • 那么,最简​​单的解决方案往往是最好的。我会为它制作一个胶水模块,希望我也能得到它的其他用途。
    • 哦,你一定会的。拥有一个胶水模块会带来比 Drupal 想象的更多的定制!
    【解决方案2】:

    是的,你可以。但创建 type1_form.tpl.php、type2_form.tpl.php 更简单,通过 print_r 或 devel 模块研究 $form。
    您也可以使用这些主题的预处理器。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多