Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
N
new_project
Project
Project
Details
Activity
Releases
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
Telenkov Ruslan
new_project
Commits
3511cc59
Commit
3511cc59
authored
Jun 03, 2022
by
Telenkov Ruslan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
this is form api task
parent
612b62b3
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
249 additions
and
0 deletions
+249
-0
formApi.info.yml
app/docroot/modules/custom/formApi/formApi.info.yml
+5
-0
formApi.install
app/docroot/modules/custom/formApi/formApi.install
+43
-0
formApi.routing.yml
app/docroot/modules/custom/formApi/formApi.routing.yml
+7
-0
FormApiTask.php
app/docroot/modules/custom/formApi/src/Form/FormApiTask.php
+194
-0
No files found.
app/docroot/modules/custom/formApi/formApi.info.yml
View file @
3511cc59
name
:
Form api
type
:
module
description
:
Module for implementing custom form.
core_version_requirement
:
^8.8 || ^9
package
:
Custom
app/docroot/modules/custom/formApi/formApi.install
0 → 100644
View file @
3511cc59
<?php
use
Drupal\Core\Database\Database
;
/**
* Implements hook_schema().
*/
function
formApi_schema
(){
$schema
[
'students'
]
=
array
(
'description'
=>
'The table for storing the students data.'
,
'fields'
=>
array
(
'id'
=>
array
(
'description'
=>
'The primary identifier for row'
,
'type'
=>
'serial'
,
'not null'
=>
TRUE
,
'unsigned'
=>
TRUE
,
),
'name'
=>
array
(
'description'
=>
'Name'
,
'type'
=>
'varchar'
,
'length'
=>
255
,
'not null'
=>
TRUE
,
'default'
=>
''
,
),
'email'
=>
array
(
'description'
=>
'Email'
,
'type'
=>
'varchar'
,
'length'
=>
255
,
'not null'
=>
TRUE
,
'default'
=>
''
,
),
'site_address'
=>
array
(
'description'
=>
'Site address'
,
'type'
=>
'varchar'
,
'length'
=>
255
,
'not null'
=>
TRUE
,
'default'
=>
''
,
),
),
'primary key'
=>
array
(
'id'
),
);
return
$schema
;
}
app/docroot/modules/custom/formApi/formApi.routing.yml
0 → 100644
View file @
3511cc59
formApi.form
:
path
:
'
/formApi'
defaults
:
_form
:
'
\Drupal\formApi\Form\FormApiTask'
_title
:
'
Form
API'
requirements
:
_permission
:
'
access
content'
app/docroot/modules/custom/formApi/src/Form/FormApiTask.php
0 → 100644
View file @
3511cc59
<?php
/**
* @file
* Contains \Drupal\formApi\Form\FormApiTask.
*/
namespace
Drupal\formApi\Form
;
use
Drupal\Core\Ajax\AddCssCommand
;
use
Drupal\Core\Ajax\AfterCommand
;
use
Drupal\Core\Ajax\AjaxResponse
;
use
Drupal\Core\Ajax\AlertCommand
;
use
Drupal\Core\Ajax\HtmlCommand
;
use
Drupal\Core\Ajax\RedirectCommand
;
use
Drupal\Core\Ajax\RemoveCommand
;
use
Drupal\Core\Form\FormBase
;
use
Drupal\Core\Form\FormStateInterface
;
class
FormApiTask
extends
FormBase
{
/**
* {@inheritdoc}
*/
public
function
getFormId
()
{
return
'student_registration_form'
;
}
public
function
buildForm
(
array
$form
,
FormStateInterface
$form_state
)
{
$form
[
'name'
]
=
array
(
'#type'
=>
'textfield'
,
'#title'
=>
t
(
'Name:'
),
);
$form
[
'site_address'
]
=
array
(
'#type'
=>
'textfield'
,
'#title'
=>
(
'Site address:'
),
);
$form
[
'system_messages'
]
=
[
'#markup'
=>
'<div id="form-system-messages"></div>'
,
'#weight'
=>
-
100
,
];
$form
[
'actions'
][
'#type'
]
=
'actions'
;
$form
[
'actions'
][
'submit'
]
=
[
'#type'
=>
'submit'
,
'#name'
=>
'submit'
,
'#value'
=>
'Submit this form'
,
'#ajax'
=>
[
'callback'
=>
'::ajaxSubmitCallback'
,
'event'
=>
'click'
,
'progress'
=>
[
'type'
=>
'throbber'
,
],
],
];
$form
[
'email'
]
=
[
'#title'
=>
'Email'
,
'#type'
=>
'email'
,
'#required'
=>
TRUE
,
];
return
$form
;
}
public
function
validateForm
(
array
&
$form
,
FormStateInterface
$form_state
)
{
}
public
function
ajaxSubmitCallback
(
array
&
$form
,
FormStateInterface
$form_state
){
$response
=
new
AjaxResponse
();
//обнуление стилей при повторном сабмите - необходимо удалить стиль надписи под полем, чтобы их не было 1000
$selector
=
'.domen-error'
;
$response
->
addCommand
(
new
RemoveCommand
(
$selector
));
$selector
=
'.email-error'
;
$response
->
addCommand
(
new
RemoveCommand
(
$selector
));
$selector
=
'.name-error'
;
$response
->
addCommand
(
new
RemoveCommand
(
$selector
));
//ВАЛИДАЦИЯ адреса сайта
$reg
=
'/(https?:\/\/)?(www.)?([\da-z\.-]+)\.([a-z\.]{2,6})/ui'
;
if
(
preg_match
(
$reg
,
$form_state
->
getValue
(
'site_address'
),
$array_of_site_address
))
{
// Если пользователь повторно ввел все нормально в это поле, то при сабмите сбросим красную рамку
if
(
end
(
$array_of_site_address
)
===
'ru'
||
end
(
$array_of_site_address
)
===
'рф'
){
$style
=
'<style>#edit-site-address{border:1px solid grey !important;}</style>'
;
$response
->
addCommand
(
new
AddCssCommand
(
$style
));
// при успешной валидации убираем из результата http или https или www
$result
=
preg_replace
(
'/((https?:\/\/)||(www\.))/ui'
,
''
,
$form_state
->
getValue
(
'site_address'
));
$valid_site_address
=
true
;
}
else
{
//ошибка домена
$style
=
'<style>#edit-site-address{border:3px solid red}</style>'
;
$response
->
addCommand
(
new
AddCssCommand
(
$style
));
$selector
=
'#edit-site-address'
;
$content
=
'<p class = "domen-error">Вы ввели неправильную доменную зону, необходимая зона: "рф" или "ru"</p>'
;
$response
->
addCommand
(
new
AfterCommand
(
$selector
,
$content
));
$style
=
'<style>.domen-error{color: red !important;}</style>'
;
$response
->
addCommand
(
new
AddCssCommand
(
$style
));
}
}
else
{
//ошибка ввода самого адреса
$style
=
'<style>#edit-site-address{border:3px solid red}</style>'
;
$response
->
addCommand
(
new
AddCssCommand
(
$style
));
$selector
=
'#edit-site-address'
;
if
(
$form_state
->
getValue
(
'site_address'
)
==
NULL
){
$content
=
'<p class = "domen-error">Необходимо ввести адрес сайта</p>'
;
}
else
{
$content
=
'<p class = "domen-error">Вы ввели адрес неправильно, пример адреса "www.example.ru"</p>'
;
}
$response
->
addCommand
(
new
AfterCommand
(
$selector
,
$content
));
$style
=
'<style>.domen-error{color: red !important;}</style>'
;
$response
->
addCommand
(
new
AddCssCommand
(
$style
));
}
// ВАЛИДАЦИЯ email
if
(
filter_var
(
$form_state
->
getValue
(
'email'
),
FILTER_VALIDATE_EMAIL
))
{
$style
=
'<style>#edit-email{border:1px solid grey !important;}</style>'
;
$response
->
addCommand
(
new
AddCssCommand
(
$style
));
$valid_email
=
true
;
}
else
{
$style
=
'<style>#edit-email{border:3px solid red}</style>'
;
$response
->
addCommand
(
new
AddCssCommand
(
$style
));
$selector
=
'#edit-email'
;
if
(
$form_state
->
getValue
(
'email'
)
==
NULL
){
$content
=
'<p class = "email-error">Необходимо ввести email</p>'
;
}
else
{
$content
=
'<p class = "email-error">Вы ввели email неправильно, пример адреса "example@mail.ru"</p>'
;
}
$response
->
addCommand
(
new
AfterCommand
(
$selector
,
$content
));
$style
=
'<style>.email-error{color: red !important;}</style>'
;
$response
->
addCommand
(
new
AddCssCommand
(
$style
));
}
//Валидация name
if
(
$form_state
->
getValue
(
'name'
)
==
NULL
){
$style
=
'<style>#edit-name{border:3px solid red}</style>'
;
$response
->
addCommand
(
new
AddCssCommand
(
$style
));
$selector
=
'#edit-name'
;
$content
=
'<p class = "name-error">Необходимо ввести имя</p>'
;
$response
->
addCommand
(
new
AfterCommand
(
$selector
,
$content
));
$style
=
'<style>.name-error{color: red !important;}</style>'
;
$response
->
addCommand
(
new
AddCssCommand
(
$style
));
}
else
{
$valid_name
=
true
;
$style
=
'<style>#edit-name{border:1px solid grey !important;}</style>'
;
$response
->
addCommand
(
new
AddCssCommand
(
$style
));
}
//после валидации обращаюсь к БД и заполняю поля у таблицы
if
(
$valid_email
&&
$valid_name
&&
$valid_site_address
){
$connection
=
\Drupal
::
service
(
'database'
);
$result
=
$connection
->
insert
(
'students'
)
->
fields
([
'name'
=>
$form_state
->
getValue
(
'name'
),
'email'
=>
$form_state
->
getValue
(
'email'
),
'site_address'
=>
$result
,
])
->
execute
();
$response
->
addCommand
(
new
AlertCommand
(
'Спасибо за заполнение, нажмите "OK" для продолжения'
));
$url
=
'/'
;
$response
->
addCommand
(
new
RedirectCommand
(
$url
));
}
return
$response
;
}
public
function
submitForm
(
array
&
$form
,
FormStateInterface
$form_state
)
{
}
}
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