Commit 2807af60 authored by Gorodkov Denis's avatar Gorodkov Denis

Ajax Form

parent 733de4d5
......@@ -4,39 +4,42 @@
* Implements hook_schema().
*/
function mymodule_schema() {
$schema['aaaa'] = array( // Название таблицы
'description' => 'Database example', // Описание таблицы
'fields' => array( // Массив с колонками таблицы
'id' => array( // Название колонки
'description' => 'ID', // Описание колонки
'type' => 'serial', // Тип данных
'unsigned' => TRUE, // Unsigned, по умолчанию FALSE
'not null' => TRUE, // Проверка на 0
),
'uid' => array(
'description' => 'UID user',
'type' => 'int',
$schema['AAA_custom_table'] = array(
'description' => 'A table to store simple data',
'fields' => array(
'id' => array(
'description' => 'Holds the id value',
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0, // Значение по умолчанию
),
'text' => array(
'description' => 'Text',
'name' => array(
'description' => 'Holds the name value',
'type' => 'varchar',
'length' => 255,
'length' => '255',
'not null' => TRUE,
),
'email' => array(
'description' => 'Holds the name value',
'type' => 'varchar',
'length' => '255',
'not null' => TRUE,
),
'url' => array(
'description' => 'Holds the name value',
'type' => 'varchar',
'length' => '255',
'not null' => TRUE,
'default' => '',
),
),
'primary key' => array('id'),
);
return $schema;
}
/**
* Implements hook_uninstall().
*/
/*function mymodule_uninstall() {
\Drupal::state()->delete('mymodule.mymodule');
}*/
function mymodule_uninstall() {
\Drupal::state()->delete('mymodule.AAA_custom_table');
}
......@@ -75,12 +75,19 @@ function mymodule_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_
/**
* Implements hook_node_view().
*/
function mymodule_node_view(array &$build, \Drupal\Core\Entity\EntityInterface $entity, \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display, $view_mode) {
/*function mymodule_node_view(array &$build, \Drupal\Core\Entity\EntityInterface $entity, \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display, $view_mode) {
$bundle = $entity->bundle();
if ($bundle == 'people') {
$build['#cache']['contexts'][] = 'url.query_args';
$entity->save();
}
}
}*/
/**
* Implements hook_preprocess().
*/
function mymodule_preprocess(&$variables, $hook) {
if ($hook == 'node') {
$variables["#cache"]["contexts"][] = "url.query_args";
}
}
......@@ -2,6 +2,9 @@
namespace Drupal\mymodule\Form;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\CssCommand;
use Drupal\Core\Ajax\HtmlCommand;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
......@@ -14,29 +17,99 @@ class AjaxForm extends FormBase {
$form['name'] = array(
'#type' => 'textfield',
'#title' => 'Name',
'#required' => TRUE
'#required' => TRUE,
'#prefix' => '',
'#suffix' => '<div class="name-validation-message"></div>',
);
$form['email'] = array(
'#type' => 'email',
'#title' => 'Email',
'#required' => TRUE
'#required' => TRUE,
'#prefix' => '',
'#suffix' => '<div class="email-validation-message"></div>',
'#ajax' => [
'callback' => [$this, 'validateEmailCallbackAjax'],
'event' => 'change',
'wrapper' => 'edit-email',
],
);
$form['url'] = array(
'#type' => 'textfield',
'#title' => 'URL',
'#required' => TRUE
'#required' => TRUE,
'#prefix' => '',
'#suffix' => '<div class="url-validation-message"></div>',
'#ajax' => [
'callback' => [$this, 'validateUrlCallbackAjax'],
'event' => 'change',
'wrapper' => 'edit-email',
],
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Import'
'#value' => 'Click',
);
return $form;
}
public function validateForm(array &$form, FormStateInterface $form_state) {
$url = $form_state->getValue('url');
$regexp = '/^(https|http):\/\//';
$checkHttp = (bool)preg_match($regexp, $url);
if ($checkHttp) {
$url[4] == 's' ? $httpOrHttps = 8 : $httpOrHttps = 7;
$checkHttp ? $url = substr($url, $httpOrHttps) : '';
}
$regexp = '/^www./';
$checkWww = ((bool)preg_match($regexp, $url));
if ($checkWww) {
$url = substr($url, 4);
}
$regexp = '/^([a-z0-9]*.[rр][uф])/';
$checkUrl = (bool)preg_match($regexp, $url);
$url = explode('.',$url);
$url = $url[0] . '.' . $url[1];
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$query = \Drupal::database()->insert('AAA_custom_table');
$query->fields([
'name' => $form_state->getValue('name'),
'email' => $form_state->getValue('email'),
'url' => $form_state->getValue('url'),
]);
$query->execute();
}
public function validateEmailCallbackAjax(array &$form, FormStateInterface $form_state) {
$response = new AjaxResponse();
$email = $form_state->getValue('email');
$email_valid = filter_var($email, FILTER_VALIDATE_EMAIL);
if (!$email_valid and !empty($email)) {
$response->addCommand(new HtmlCommand('.email-validation-message', 'Введен не корректный email'));
$response->addCommand(new CssCommand('.email-validation-message', ['color' => 'red']));
$response->addCommand(new CssCommand('#edit-email', ['border' => '1px solid red']));
} else {
$response->addCommand(new HtmlCommand('.email-validation-message', ''));
$response->addCommand(new CssCommand('#edit-email', ['border' => '']));
}
return $response;
}
public function validateUrlCallbackAjax(array &$form, FormStateInterface $formState) {
$response = new AjaxResponse();
$response->addCommand(new HtmlCommand('.url-validation-message', 'ZXC'));
$response->addCommand(new CssCommand('#edit-url', ['border' => '1px solid red']));
return $response;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment