Commit d71b7500 authored by Gorodkov Denis's avatar Gorodkov Denis

Update AJAX FORM

parent 865fbdc9
......@@ -14,6 +14,9 @@ use Drupal\Core\Url;
use Drupal\redirect\Entity\Redirect;
class AjaxForm extends FormBase {
public $response;
public function getFormId() {
return 'mymodule_ajax_form';
}
......@@ -79,14 +82,20 @@ class AjaxForm extends FormBase {
$name = $form_state->getValue('name');
if (empty($name)) {
$form_state->setErrorByName('name', 'Заполните name');
} else {
$this->clearRedField('name');
}
if ($site == 'hasSite') {
if (!$this->checkUrl($url) or empty($url)) {
$form_state->setErrorByName('url', 'Неверно введен url');
} else {
$this->clearRedField('url');
}
}
if (!$this->checkEmail($email)) {
$form_state->setErrorByName('email', 'Неверно введен email');
} else {
$this->clearRedField('email');
}
}
......@@ -135,7 +144,9 @@ class AjaxForm extends FormBase {
}
}
public function alert(array &$form, FormStateInterface $form_state) {
$response = new AjaxResponse();
$this->response = new AjaxResponse();
$response = $this->response;
if (empty($form_state->getErrors())) {
$commandRedirect = new RedirectCommand('/home');
$response->addCommand(new AlertCommand('Спасибо за заполнение'));
......@@ -145,38 +156,25 @@ class AjaxForm extends FormBase {
$email = $form_state->getValue('email');
$url = $form_state->getValue('url');
$site = $form_state->getValue('site');
$errors = $form_state->getErrors();
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 (!$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' => '']));
}
}
foreach ($errors as $key => $value) {
$this->redFiled($key);
}
}
return $response;
}
public function redFiled($key) {
$response = $this->response;
$response->addCommand(new HtmlCommand(".$key-validation-message", "Введен не корректный $key"));
$response->addCommand(new CssCommand(".$key-validation-message", ['color' => 'red']));
$response->addCommand(new CssCommand("#edit-$key", ['border' => '1px solid red']));
}
public function clearRedField($key) {
$response = $this->response;
$response->addCommand(new HtmlCommand(".$key-validation-message", ''));
$response->addCommand(new CssCommand("#edit-$key", ['border' => '']));
}
}
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