Drupal 7 FAPI-在验证或提交处理程序时添加表单元素 [英] Drupal 7 FAPI- Adding form elements in validate or submit handlers

查看:390
本文介绍了Drupal 7 FAPI-在验证或提交处理程序时添加表单元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在drupal 7模块中验证或提交函数中添加其他表单元素?以下代码工作,图像显示在表单上:

Is it possible to add additional form elements in the validate or submit functions in drupal 7 module? The following code works and image is displayed on the form:

function test1_form($form, &$form_state)
{
     $form['image']=array(
           '#markup'=>'<img src="sites/all/modules/create_ad/th.jpeg"><br/>', //replace with your own image path
     );

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

但是当我提交后,如果在提交后尝试显示图像,如下所示不工作:

but when I try to display image after submission in submit function like following, it doesn't work:

function test1_form($form, &$form_state)
{
     $form['submit'] = array(
           '#type' => 'submit',
           '#value' => 'Submit',
     );
}

function test1_form_submit($form,&$form_state)
{
     $form['image']=array(
           '#markup'=>'<img src="sites/all/modules/create_ad/th.jpeg"><br/>', //replace with your own image path
     );
}

欢迎任何积极的帮助。谢谢。

Any positive help is welcome. Thanks.

推荐答案

您可以按照多步骤表单方法在提交后向表单添加其他字段。

You could follow the multi-step form methodology to add additional fields to your form after submission.

这是一篇博文一个>谈论一个多步骤的方法,可以给你一些洞察力。

Here is a blog post that talks about one approach for multi-step and can give you some insight.

基本上,在你的提交功能上,你存储你的值并设置要重建的表单。然后在您的表单函数中,检查这些存储值,并添加新字段(如果存在)。

Basically, on your submit function you store your values and set the form to be rebuilt. Then in your form function you check for those stored values and add the new fields if present.

示例:

<?php
function test1_form($form, &$form_state)
{
        if (isset($form_state['storage']['show-image'])){
            $form['image']=array(
                '#markup'=>'<img src="sites/all/modules/create_ad/th.jpeg"><br/>', //replace with your own image path
            );
        }

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

function test1_form_submit($form,&$form_state)
{
    $form_state['rebuild'] = TRUE;
    $form_state['storage']['show-image'] = true;
}

这篇关于Drupal 7 FAPI-在验证或提交处理程序时添加表单元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆