Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
P
Project
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Jobs
Commits
Open sidebar
Gorodkov Denis
Project
Commits
d71b7500
Commit
d71b7500
authored
May 24, 2022
by
Gorodkov Denis
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Update AJAX FORM
parent
865fbdc9
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
28 additions
and
30 deletions
+28
-30
AjaxForm.php
app/docroot/modules/custom/mymodule/src/Form/AjaxForm.php
+28
-30
No files found.
app/docroot/modules/custom/mymodule/src/Form/AjaxForm.php
View file @
d71b7500
...
@@ -14,6 +14,9 @@ use Drupal\Core\Url;
...
@@ -14,6 +14,9 @@ use Drupal\Core\Url;
use
Drupal\redirect\Entity\Redirect
;
use
Drupal\redirect\Entity\Redirect
;
class
AjaxForm
extends
FormBase
{
class
AjaxForm
extends
FormBase
{
public
$response
;
public
function
getFormId
()
{
public
function
getFormId
()
{
return
'mymodule_ajax_form'
;
return
'mymodule_ajax_form'
;
}
}
...
@@ -79,14 +82,20 @@ class AjaxForm extends FormBase {
...
@@ -79,14 +82,20 @@ class AjaxForm extends FormBase {
$name
=
$form_state
->
getValue
(
'name'
);
$name
=
$form_state
->
getValue
(
'name'
);
if
(
empty
(
$name
))
{
if
(
empty
(
$name
))
{
$form_state
->
setErrorByName
(
'name'
,
'Заполните name'
);
$form_state
->
setErrorByName
(
'name'
,
'Заполните name'
);
}
else
{
$this
->
clearRedField
(
'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'
);
}
else
{
$this
->
clearRedField
(
'url'
);
}
}
}
}
if
(
!
$this
->
checkEmail
(
$email
))
{
if
(
!
$this
->
checkEmail
(
$email
))
{
$form_state
->
setErrorByName
(
'email'
,
'Неверно введен email'
);
$form_state
->
setErrorByName
(
'email'
,
'Неверно введен email'
);
}
else
{
$this
->
clearRedField
(
'email'
);
}
}
}
}
...
@@ -135,7 +144,9 @@ class AjaxForm extends FormBase {
...
@@ -135,7 +144,9 @@ class AjaxForm extends FormBase {
}
}
}
}
public
function
alert
(
array
&
$form
,
FormStateInterface
$form_state
)
{
public
function
alert
(
array
&
$form
,
FormStateInterface
$form_state
)
{
$response
=
new
AjaxResponse
();
$this
->
response
=
new
AjaxResponse
();
$response
=
$this
->
response
;
if
(
empty
(
$form_state
->
getErrors
()))
{
if
(
empty
(
$form_state
->
getErrors
()))
{
$commandRedirect
=
new
RedirectCommand
(
'/home'
);
$commandRedirect
=
new
RedirectCommand
(
'/home'
);
$response
->
addCommand
(
new
AlertCommand
(
'Спасибо за заполнение'
));
$response
->
addCommand
(
new
AlertCommand
(
'Спасибо за заполнение'
));
...
@@ -145,38 +156,25 @@ class AjaxForm extends FormBase {
...
@@ -145,38 +156,25 @@ class AjaxForm extends FormBase {
$email
=
$form_state
->
getValue
(
'email'
);
$email
=
$form_state
->
getValue
(
'email'
);
$url
=
$form_state
->
getValue
(
'url'
);
$url
=
$form_state
->
getValue
(
'url'
);
$site
=
$form_state
->
getValue
(
'site'
);
$site
=
$form_state
->
getValue
(
'site'
);
$errors
=
$form_state
->
getErrors
();
if
(
empty
(
$name
))
{
foreach
(
$errors
as
$key
=>
$value
)
{
$response
->
addCommand
(
new
HtmlCommand
(
'.name-validation-message'
,
"Введен не корректный name"
));
$this
->
redFiled
(
$key
);
$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'
=>
''
]));
}
}
}
}
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'
]));
}
}
return
$response
;
public
function
clearRedField
(
$key
)
{
$response
=
$this
->
response
;
$response
->
addCommand
(
new
HtmlCommand
(
".
$key
-validation-message"
,
''
));
$response
->
addCommand
(
new
CssCommand
(
"#edit-
$key
"
,
[
'border'
=>
''
]));
}
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment