简体   繁体   English

Drupal 7 FAPI-在验证或提交处理程序中添加表单元素

[英]Drupal 7 FAPI- Adding form elements in validate or submit handlers

Is it possible to add additional form elements in the validate or submit functions in drupal 7 module? 是否可以在drupal 7模块的验证或提交函数中添加其他表单元素? 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. 然后在表单函数中检查这些存储的值并添加新字段(如果存在)。

Example: 例:

<?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;
}

Here is another way of doing this with Ajax. 这是使用Ajax执行此操作的另一种方法。 This way you will not get any page reload since only the image will be loaded in to the div with wrapper id. 这样您就不会重新加载任何页面,因为只有图像将被加载到带有包装ID的div中。

Here is the code: 这是代码:

function test_menu()
{
    $items = array();

    $items['test'] = array(
        'title'           => 'test',
        'page callback'   => 'drupal_get_form',
        'page arguments'  => array('test1_form'),
        'access callback' => array(TRUE),
        'type'            => MENU_CALLBACK,
    );

    return $items;
}

function test1_form($form, &$form_state)
{
    $form['submit'] = array(
        '#type'  => 'button',
        '#value' => t('Submit'),
        '#name'  => 'add',
        '#ajax'  => array(
            'callback' => 'ajax_load_picture_callback',
            'wrapper'  => 'wrapper',
            'method'   => 'replace',
            'effect'   => 'fade',
        ),
    );

    $form['image'] = array(
        '#markup' => '',
        '#prefix' => '<div id="wrapper">',
        '#suffix' => '</div>',
    );
    if (array_key_exists('triggering_element', $form_state) &&
        array_key_exists('#name', $form_state['triggering_element']) &&
        $form_state['triggering_element']['#name'] == 'add')
    {
        $form['image']['#markup'] ='<img src="themes/bartik/logo.png"><br/>'; //replace with your own image path
    }

    return $form;
}

function ajax_load_picture_callback($form, $form_state)
{
    return $form['image'];
}

function test1_form_submit($form, &$form_state)
{

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM