Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
F
field_validation
Manage
Activity
Members
Labels
Plan
Wiki
Custom issue tracker
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Model registry
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
project
field_validation
Commits
c90d9cf4
Commit
c90d9cf4
authored
Jul 30, 2023
by
howard ge
Browse files
Options
Downloads
Patches
Plain Diff
Issue
#3377623
by g089h515r806: add support for Drupal core's Constraints, add range
parent
d6f8e9f5
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
config/schema/field_validation.schema.yml
+27
-1
27 additions, 1 deletion
config/schema/field_validation.schema.yml
src/Plugin/FieldValidationRule/RangeConstraintFieldValidationRule.php
+113
-0
113 additions, 0 deletions
...ieldValidationRule/RangeConstraintFieldValidationRule.php
with
140 additions
and
1 deletion
config/schema/field_validation.schema.yml
+
27
−
1
View file @
c90d9cf4
...
@@ -144,3 +144,29 @@ field_validation.rule.unique_field_constraint_rule:
...
@@ -144,3 +144,29 @@ field_validation.rule.unique_field_constraint_rule:
message
:
message
:
type
:
string
type
:
string
label
:
'
Message'
label
:
'
Message'
field_validation.rule.range_constraint_rule
:
type
:
mapping
label
:
'
Range
constraint
validation
rule'
mapping
:
validate_mode
:
type
:
string
label
:
'
Validate
mode'
min
:
type
:
string
label
:
'
Min'
max
:
type
:
string
label
:
'
Max'
maxMessage
:
type
:
string
label
:
'
Max
message'
minMessage
:
type
:
string
label
:
'
Min
message'
invalidMessage
:
type
:
string
label
:
'
Invalid
message'
notInRangeMessage
:
type
:
string
label
:
'
Not
in
range
message'
\ No newline at end of file
This diff is collapsed.
Click to expand it.
src/Plugin/FieldValidationRule/RangeConstraintFieldValidationRule.php
0 → 100644
+
113
−
0
View file @
c90d9cf4
<?php
namespace
Drupal\field_validation\Plugin\FieldValidationRule
;
use
Drupal\Core\Form\FormStateInterface
;
use
Drupal\field_validation
\ConstraintFieldValidationRuleBase
;
use
Drupal\field_validation
\FieldValidationRuleSetInterface
;
/**
* Provides funcationality for EmailFieldValidationRule.
*
* @FieldValidationRule(
* id = "range_constraint_rule",
* label = @Translation("Range constraint"),
* description = @Translation("Range constraint.")
* )
*/
class
RangeConstraintFieldValidationRule
extends
ConstraintFieldValidationRuleBase
{
/**
* {@inheritdoc}
*/
public
function
getConstraintName
():
string
{
return
"Range"
;
}
/**
* {@inheritdoc}
*/
public
function
isPropertyConstraint
():
bool
{
return
TRUE
;
}
/**
* {@inheritdoc}
*/
public
function
defaultConfiguration
()
{
return
[
'min'
=>
NULL
,
'max'
=>
NULL
,
'maxMessage'
=>
NULL
,
'minMessage'
=>
NULL
,
'invalidMessage'
=>
NULL
,
'notInRangeMessage'
=>
NULL
,
]
+
parent
::
defaultConfiguration
();
}
/**
* {@inheritdoc}
*/
public
function
buildConfigurationForm
(
array
$form
,
FormStateInterface
$form_state
)
{
$form
=
parent
::
buildConfigurationForm
(
$form
,
$form_state
);
//copied from core.
$maxMessage
=
'This value should be %limit or more.'
;
$minMessage
=
'This value should be %limit or less.'
;
$invalidMessage
=
'This value should be a valid number.'
;
$notInRangeMessage
=
'This value should be between %min and %max.'
;
$form
[
'min'
]
=
[
'#type'
=>
'textfield'
,
'#title'
=>
$this
->
t
(
'Min'
),
'#default_value'
=>
$this
->
configuration
[
'min'
],
'#required'
=>
TRUE
,
];
$form
[
'max'
]
=
[
'#type'
=>
'textfield'
,
'#title'
=>
$this
->
t
(
'Max'
),
'#default_value'
=>
$this
->
configuration
[
'max'
],
'#required'
=>
TRUE
,
];
$form
[
'maxMessage'
]
=
[
'#type'
=>
'textfield'
,
'#title'
=>
$this
->
t
(
'Max Message'
),
'#default_value'
=>
$this
->
configuration
[
'maxMessage'
]
??
$maxMessage
,
'#maxlength'
=>
255
,
];
$form
[
'minMessage'
]
=
[
'#type'
=>
'textfield'
,
'#title'
=>
$this
->
t
(
'Min Message'
),
'#default_value'
=>
$this
->
configuration
[
'minMessage'
]
??
$minMessage
,
'#maxlength'
=>
255
,
];
$form
[
'invalidMessage'
]
=
[
'#type'
=>
'textfield'
,
'#title'
=>
$this
->
t
(
'Invalid Message'
),
'#default_value'
=>
$this
->
configuration
[
'invalidMessage'
]
??
$invalidMessage
,
'#maxlength'
=>
255
,
];
$form
[
'notInRangeMessage'
]
=
[
'#type'
=>
'textfield'
,
'#title'
=>
$this
->
t
(
'Not In Range Message'
),
'#default_value'
=>
$this
->
configuration
[
'notInRangeMessage'
]
??
$notInRangeMessage
,
'#maxlength'
=>
255
,
];
return
$form
;
}
/**
* {@inheritdoc}
*/
public
function
submitConfigurationForm
(
array
&
$form
,
FormStateInterface
$form_state
)
{
parent
::
submitConfigurationForm
(
$form
,
$form_state
);
$this
->
configuration
[
'min'
]
=
$form_state
->
getValue
(
'min'
);
$this
->
configuration
[
'max'
]
=
$form_state
->
getValue
(
'max'
);
$this
->
configuration
[
'maxMessage'
]
=
$form_state
->
getValue
(
'maxMessage'
);
$this
->
configuration
[
'minMessage'
]
=
$form_state
->
getValue
(
'minMessage'
);
$this
->
configuration
[
'invalidMessage'
]
=
$form_state
->
getValue
(
'invalidMessage'
);
$this
->
configuration
[
'notInRangeMessage'
]
=
$form_state
->
getValue
(
'notInRangeMessage'
);
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
sign in
to comment