Commit 865fbdc9 authored by Gorodkov Denis's avatar Gorodkov Denis

Update AJAX FORM

parent aeec46f0
...@@ -33,11 +33,6 @@ class AjaxForm extends FormBase { ...@@ -33,11 +33,6 @@ class AjaxForm extends FormBase {
'#required' => TRUE, '#required' => TRUE,
'#prefix' => '', '#prefix' => '',
'#suffix' => '<div class="email-validation-message"></div>', '#suffix' => '<div class="email-validation-message"></div>',
'#ajax' => [
'callback' => [$this, 'validateEmailCallbackAjax'],
'event' => 'change',
'wrapper' => 'edit-email',
],
); );
$form['url'] = array( $form['url'] = array(
...@@ -45,11 +40,6 @@ class AjaxForm extends FormBase { ...@@ -45,11 +40,6 @@ class AjaxForm extends FormBase {
'#title' => 'URL', '#title' => 'URL',
'#prefix' => '', '#prefix' => '',
'#suffix' => '<div class="url-validation-message"></div>', '#suffix' => '<div class="url-validation-message"></div>',
'#ajax' => [
'callback' => [$this, 'validateUrlCallbackAjax'],
'event' => 'change',
'wrapper' => 'edit-email',
],
'#states' => array( '#states' => array(
'visible' => array( 'visible' => array(
':input[name="site"]' => ['value' => 'hasSite'], ':input[name="site"]' => ['value' => 'hasSite'],
...@@ -86,6 +76,10 @@ class AjaxForm extends FormBase { ...@@ -86,6 +76,10 @@ class AjaxForm extends FormBase {
$url = $form_state->getValue('url'); $url = $form_state->getValue('url');
$email = $form_state->getValue('email'); $email = $form_state->getValue('email');
$site = $form_state->getValue('site'); $site = $form_state->getValue('site');
$name = $form_state->getValue('name');
if (empty($name)) {
$form_state->setErrorByName('name', 'Заполните name');
}
if ($site == 'hasSite') { if ($site == 'hasSite') {
if (!$this->checkUrl($url) or empty($url)) { if (!$this->checkUrl($url) or empty($url)) {
$form_state->setErrorByName('url', 'Неверно введен url'); $form_state->setErrorByName('url', 'Неверно введен url');
...@@ -97,10 +91,6 @@ class AjaxForm extends FormBase { ...@@ -97,10 +91,6 @@ class AjaxForm extends FormBase {
} }
public function submitForm(array &$form, FormStateInterface $form_state) { public function submitForm(array &$form, FormStateInterface $form_state) {
#$url = Url::fromRoute('mymodule.batch_form');
#$form_state->setRedirectUrl();
#$form_state->setRedirect();
$query = \Drupal::database()->insert('AAA_custom_table'); $query = \Drupal::database()->insert('AAA_custom_table');
$query->fields([ $query->fields([
'name' => $form_state->getValue('name'), 'name' => $form_state->getValue('name'),
...@@ -108,35 +98,6 @@ class AjaxForm extends FormBase { ...@@ -108,35 +98,6 @@ class AjaxForm extends FormBase {
'url' => $form_state->getValue('url'), 'url' => $form_state->getValue('url'),
]); ]);
$query->execute(); $query->execute();
$form_state->setRedirect('<front>');
}
public function getResponse(array &$form, FormStateInterface $form_state, $field_name) {
$response = new AjaxResponse();
$val = $field_name == 'email' ? $val = $form_state->getValue('email') : $val = $form_state->getValue('url');
$check = $field_name == 'email' ? $check = 'checkEmail' : $check = 'checkUrl';
if (!$this->$check($val)) {
$response->addCommand(new HtmlCommand('.email-validation-message', "Введен не корректный $field_name"));
$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 validateEmailCallbackAjax(array &$form, FormStateInterface $form_state) {
$field_name = 'email';
$response = $this->getResponse($form, $form_state, $field_name);
return $response;
}
public function validateUrlCallbackAjax(array &$form, FormStateInterface $form_state) {
$field_name = 'url';
$response = $this->getResponse($form, $form_state, $field_name);
return $response;
} }
public function checkUrl($url) { public function checkUrl($url) {
...@@ -175,22 +136,47 @@ class AjaxForm extends FormBase { ...@@ -175,22 +136,47 @@ class AjaxForm extends FormBase {
} }
public function alert(array &$form, FormStateInterface $form_state) { public function alert(array &$form, FormStateInterface $form_state) {
$response = new AjaxResponse(); $response = new AjaxResponse();
$url = $form_state->getValue('url'); if (empty($form_state->getErrors())) {
$commandRedirect = new RedirectCommand('/home');
$response->addCommand(new AlertCommand('Спасибо за заполнение'));
$response->addCommand($commandRedirect);
} else {
$name = $form_state->getValue('name');
$email = $form_state->getValue('email'); $email = $form_state->getValue('email');
$url = $form_state->getValue('url');
$site = $form_state->getValue('site'); $site = $form_state->getValue('site');
$commandRedirect = new RedirectCommand('/home');
if (empty($name)) {
$response->addCommand(new HtmlCommand('.name-validation-message', "Введен не корректный name"));
$response->addCommand(new CssCommand('.name-validation-message', ['color' => 'red']));
$response->addCommand(new CssCommand('#edit-name', ['border' => '1px solid red']));
} else {
$response->addCommand(new HtmlCommand('.name-validation-message', ''));
$response->addCommand(new CssCommand('#edit-name', ['border' => '']));
}
if (!$this->checkEmail($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' => '']));
}
if ($site == 'hasSite') { if ($site == 'hasSite') {
if (!$this->checkUrl($url) or empty($url)) { if (!$this->checkUrl($url) or empty($url)) {
$form_state->setErrorByName('url', 'Неверное введен url'); if (!$this->checkUrl($url)) {
$response->addCommand(new HtmlCommand('.url-validation-message', "Введен не корректный url"));
$response->addCommand(new CssCommand('.url-validation-message', ['color' => 'red']));
$response->addCommand(new CssCommand('#edit-url', ['border' => '1px solid red']));
} else {
$response->addCommand(new HtmlCommand('.url-validation-message', ''));
$response->addCommand(new CssCommand('#edit-url', ['border' => '']));
}
} }
} }
if (!$this->checkEmail($email)) {
$form_state->setErrorByName('email', 'Неверное введен email');
} }
$response->addCommand(new AlertCommand('Спасибо за заполнение'));
$response->addCommand($commandRedirect);
return $response; 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