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

Update AJAX FORM

parent aeec46f0
......@@ -33,11 +33,6 @@ class AjaxForm extends FormBase {
'#required' => TRUE,
'#prefix' => '',
'#suffix' => '<div class="email-validation-message"></div>',
'#ajax' => [
'callback' => [$this, 'validateEmailCallbackAjax'],
'event' => 'change',
'wrapper' => 'edit-email',
],
);
$form['url'] = array(
......@@ -45,11 +40,6 @@ class AjaxForm extends FormBase {
'#title' => 'URL',
'#prefix' => '',
'#suffix' => '<div class="url-validation-message"></div>',
'#ajax' => [
'callback' => [$this, 'validateUrlCallbackAjax'],
'event' => 'change',
'wrapper' => 'edit-email',
],
'#states' => array(
'visible' => array(
':input[name="site"]' => ['value' => 'hasSite'],
......@@ -86,6 +76,10 @@ class AjaxForm extends FormBase {
$url = $form_state->getValue('url');
$email = $form_state->getValue('email');
$site = $form_state->getValue('site');
$name = $form_state->getValue('name');
if (empty($name)) {
$form_state->setErrorByName('name', 'Заполните name');
}
if ($site == 'hasSite') {
if (!$this->checkUrl($url) or empty($url)) {
$form_state->setErrorByName('url', 'Неверно введен url');
......@@ -97,10 +91,6 @@ class AjaxForm extends FormBase {
}
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->fields([
'name' => $form_state->getValue('name'),
......@@ -108,35 +98,6 @@ class AjaxForm extends FormBase {
'url' => $form_state->getValue('url'),
]);
$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) {
......@@ -175,22 +136,47 @@ class AjaxForm extends FormBase {
}
public function alert(array &$form, FormStateInterface $form_state) {
$response = new AjaxResponse();
$url = $form_state->getValue('url');
$email = $form_state->getValue('email');
$site = $form_state->getValue('site');
$commandRedirect = new RedirectCommand('/home');
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');
$url = $form_state->getValue('url');
$site = $form_state->getValue('site');
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 ($site == 'hasSite') {
if (!$this->checkUrl($url) or empty($url)) {
$form_state->setErrorByName('url', 'Неверное введен url');
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 (!$this->checkEmail($email)) {
$form_state->setErrorByName('email', 'Неверное введен email');
if ($site == 'hasSite') {
if (!$this->checkUrl($url) or empty($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' => '']));
}
}
}
}
$response->addCommand(new AlertCommand('Спасибо за заполнение'));
$response->addCommand($commandRedirect);
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