8000 The initial commit of our humble ajax form example. · drupal-up/ajax_form_submit@71cc4b4 · GitHub
[go: up one dir, main page]

Skip to content

Commit 71cc4b4

Browse files
author
Nikolay
committed
The initial commit of our humble ajax form example.
0 parents  commit 71cc4b4

File tree

4 files changed

+89
-0
lines changed

4 files changed

+89
-0
lines changed

ajax_form_submit.info.yml

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
name: Ajax Form Submit example
2+
type: module
3+
description: Demonstrates the Drupal Form API with ajax submit.
4+
core: 8.x
5+

ajax_form_submit.module

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
3+
/**
4+
* @file
5+
*
6+
*/

ajax_form_submit.routing.yml

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
ajax_form_submit.ajax_submit_demo:
2+
path: 'ajax-form-submit'
3+
defaults:
4+
_form: '\Drupal\ajax_form_submit\Form\AjaxSubmitDemo'
5+
_title: 'Ajax Submit Form'
6+
requirements:
7+
_permission: 'access content'

src/Form/AjaxSubmitDemo.php

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
3+
namespace Drupal\ajax_form_submit\Form;
4+
5+
use Drupal\Core\Form\FormStateInterface;
6+
use Drupal\Core\Form\FormBase;
7+
use Drupal\Core\Ajax\AjaxResponse;
8+
use Drupal\Core\Ajax\HtmlCommand;
9+
/**
10+
*
11+
*/
12+
class AjaxSubmitDemo extends FormBase {
13+
14+
/**
15+
* {@inheritdoc}
16+
*/
17+
public function getFormId() {
18+
return 'ajax_submit_demo';
19+
}
20+
21+
/**
22+
* {@inheritdoc}
23+
*/
24+
public function buildForm(array $form, FormStateInterface $form_state) {
25+
26+
$form['message'] = [
27+
'#type' => 'markup',
28+
'#markup' => '<div class="result_message"></div>'
29+
];
30+
31+
$form['number_1'] = [
32+
'#type' => 'textfield',
33+
'#title' => $this->t('Number 1'),
34+
];
35+
36+
$form['number_2'] = [
37+
'#type' => 'textfield',
38+
'#title' => $this->t('Number 2'),
39+
];
40+
41+
$form['actions'] = [
42+
'#type' => 'button',
43+
'#value' => $this->t('Submit'),
44+
'#ajax' => [
45+
'callback' => '::setMessage',
46+
'wrapper' => 'actions',
47+
],
48+
];
49+
50+
return $form;
51+
}
52+
53+
/**
54+
*
55+
*/
56+
public function setMessage(array $form, FormStateInterface $form_state) {
57+
58+
$response = new AjaxResponse();
59+
$response->addCommand(
60+
new HtmlCommand(
61+
'.result_message',
62+
'<div class="my_top_message">The result is ' . t('The results is ') . ($form_state->getValue('number_1') + $form_state->getValue('number_2')) . '</div>')
63+
);
64+
return $response;
65+
66+
}
67+
68+
public function submitForm(array &$form, FormStateInterface $form_state) {
69+
}
70+
71+
}

0 commit comments

Comments
 (0)
0