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

Ajax Form

parent 733de4d5
...@@ -4,39 +4,42 @@ ...@@ -4,39 +4,42 @@
* Implements hook_schema(). * Implements hook_schema().
*/ */
function mymodule_schema() { function mymodule_schema() {
$schema['aaaa'] = array( // Название таблицы $schema['AAA_custom_table'] = array(
'description' => 'Database example', // Описание таблицы 'description' => 'A table to store simple data',
'fields' => array( // Массив с колонками таблицы 'fields' => array(
'id' => array( // Название колонки 'id' => array(
'description' => 'ID', // Описание колонки 'description' => 'Holds the id value',
'type' => 'serial', // Тип данных 'type' => 'serial',
'unsigned' => TRUE, // Unsigned, по умолчанию FALSE
'not null' => TRUE, // Проверка на 0
),
'uid' => array(
'description' => 'UID user',
'type' => 'int',
'unsigned' => TRUE, 'unsigned' => TRUE,
'not null' => TRUE, 'not null' => TRUE,
'default' => 0, // Значение по умолчанию
), ),
'text' => array( 'name' => array(
'description' => 'Text', 'description' => 'Holds the name value',
'type' => 'varchar', '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, 'not null' => TRUE,
'default' => '',
), ),
), ),
'primary key' => array('id'), 'primary key' => array('id'),
); );
return $schema; return $schema;
} }
/** /**
* Implements hook_uninstall(). * Implements hook_uninstall().
*/ */
/*function mymodule_uninstall() { function mymodule_uninstall() {
\Drupal::state()->delete('mymodule.mymodule'); \Drupal::state()->delete('mymodule.AAA_custom_table');
}*/ }
...@@ -75,12 +75,19 @@ function mymodule_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_ ...@@ -75,12 +75,19 @@ function mymodule_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_
/** /**
* Implements hook_node_view(). * 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(); $bundle = $entity->bundle();
if ($bundle == 'people') { if ($bundle == 'people') {
$build['#cache']['contexts'][] = 'url.query_args'; $build['#cache']['contexts'][] = 'url.query_args';
$entity->save(); $entity->save();
} }
} }*/
/**
* Implements hook_preprocess().
*/
function mymodule_preprocess(&$variables, $hook) {
if ($hook == 'node') {
$variables["#cache"]["contexts"][] = "url.query_args";
}
}
...@@ -2,6 +2,9 @@ ...@@ -2,6 +2,9 @@
namespace Drupal\mymodule\Form; 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\FormBase;
use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Form\FormStateInterface;
...@@ -14,29 +17,99 @@ class AjaxForm extends FormBase { ...@@ -14,29 +17,99 @@ class AjaxForm extends FormBase {
$form['name'] = array( $form['name'] = array(
'#type' => 'textfield', '#type' => 'textfield',
'#title' => 'Name', '#title' => 'Name',
'#required' => TRUE '#required' => TRUE,
'#prefix' => '',
'#suffix' => '<div class="name-validation-message"></div>',
); );
$form['email'] = array( $form['email'] = array(
'#type' => 'email', '#type' => 'email',
'#title' => '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( $form['url'] = array(
'#type' => 'textfield', '#type' => 'textfield',
'#title' => 'URL', '#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( $form['submit'] = array(
'#type' => 'submit', '#type' => 'submit',
'#value' => 'Import' '#value' => 'Click',
); );
return $form; 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) { 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