Skip to content

Commit b4f9c84

Browse files
authored
Adding validation for email-id field (#1050)
Adding Field Constraints for user register form
1 parent c797843 commit b4f9c84

File tree

3 files changed

+169
-0
lines changed

3 files changed

+169
-0
lines changed

apigee_edge.module

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,7 @@ function apigee_edge_entity_base_field_info_alter(&$fields, EntityTypeInterface
326326
$mail = $fields['mail'];
327327
$mail->setRequired(TRUE);
328328
$mail->addConstraint('DeveloperMailUnique');
329+
$mail->addConstraint('DeveloperLowercaseEmail');
329330

330331
// Add a bundle to these fields to allow other modules to display them
331332
// as configurable (fields added through the UI or configuration do have a
@@ -336,6 +337,28 @@ function apigee_edge_entity_base_field_info_alter(&$fields, EntityTypeInterface
336337
}
337338
}
338339

340+
/**
341+
* Implements hook_form_FORM_ID_alter() for install_configure_form().
342+
*
343+
* Allows the profile to alter the site configuration form.
344+
*/
345+
function apigee_edge_form_install_configure_form_alter(&$form, FormStateInterface $form_state) {
346+
$form['#validate'][] = '_apigee_edge_site_install_form_check_email';
347+
}
348+
349+
/**
350+
* @internal
351+
*/
352+
function _apigee_edge_site_install_form_check_email(&$form, FormStateInterface $form_state) {
353+
$org_controller = \Drupal::service('apigee_edge.controller.organization');
354+
// Check if org is ApigeeX.
355+
if ($org_controller->isOrganizationApigeeX()) {
356+
if (preg_match('/[A-Z]/', $form_state->getValue(['account', 'mail']))) {
357+
$form_state->setErrorByName('account][mail', 'This email address accepts only lowercase characters.');
358+
}
359+
}
360+
}
361+
339362
/**
340363
* Implements hook_entity_extra_field_info().
341364
*/
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
/**
4+
* Copyright 2024 Google Inc.
5+
*
6+
* This program is free software; you can redistribute it and/or
7+
* modify it under the terms of the GNU General Public License
8+
* version 2 as published by the Free Software Foundation.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program; if not, write to the Free Software
17+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
18+
* USA.
19+
*/
20+
21+
namespace Drupal\apigee_edge\Plugin\Validation\Constraint;
22+
23+
use Symfony\Component\Validator\Constraint;
24+
25+
/**
26+
* Checks that the submitted value is a unique integer.
27+
*
28+
* @Constraint(
29+
* id = "DeveloperLowercaseEmail",
30+
* label = @Translation("Developer Lowercase Email", context = "Validation"),
31+
* type = "email"
32+
* )
33+
*/
34+
class LowercaseEmailConstraint extends Constraint {
35+
36+
/**
37+
* The message that will be shown if the value contains any uppercase characters.
38+
*
39+
* @var string
40+
*/
41+
public $notLowercase = 'This email address accepts only lowercase characters.';
42+
43+
}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
<?php
2+
3+
/**
4+
* Copyright 2024 Google Inc.
5+
*
6+
* This program is free software; you can redistribute it and/or
7+
* modify it under the terms of the GNU General Public License
8+
* version 2 as published by the Free Software Foundation.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program; if not, write to the Free Software
17+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
18+
* USA.
19+
*/
20+
21+
namespace Drupal\apigee_edge\Plugin\Validation\Constraint;
22+
23+
use Drupal\apigee_edge\Entity\Controller\OrganizationController;
24+
use Drupal\apigee_edge\SDKConnectorInterface;
25+
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
26+
use Symfony\Component\DependencyInjection\ContainerInterface;
27+
use Symfony\Component\Validator\Constraint;
28+
use Symfony\Component\Validator\ConstraintValidator;
29+
30+
/**
31+
* Validates the LowercaseEmail constraint.
32+
*/
33+
class LowercaseEmailConstraintValidator extends ConstraintValidator implements ContainerInjectionInterface {
34+
35+
/**
36+
* The internal entity cache.
37+
*
38+
* @var \Apigee\Edge\Api\Management\Entity\OrganizationInterface[]
39+
*/
40+
private $cache = [];
41+
42+
/**
43+
* The organization controller service.
44+
*
45+
* @var \Drupal\apigee_edge\Entity\Controller\OrganizationController
46+
*/
47+
private $orgController;
48+
49+
/**
50+
* The sdk comnector service.
51+
*
52+
* @var \Drupal\apigee_edge\SDKConnectorInterface
53+
*/
54+
private $sdkConnector;
55+
56+
/**
57+
* Constructs a ValidReferenceConstraintValidator object.
58+
*
59+
* @param \Drupal\apigee_edge\SDKConnectorInterface $sdk_connector
60+
* The SDK connector service.
61+
* @param \Drupal\apigee_edge\Entity\Controller\OrganizationController $org_controller
62+
* The organization controller service.
63+
*/
64+
public function __construct(SDKConnectorInterface $sdk_connector, OrganizationController $org_controller) {
65+
$this->sdkConnector = $sdk_connector;
66+
$this->orgController = $org_controller;
67+
}
68+
69+
/**
70+
* {@inheritdoc}
71+
*/
72+
public static function create(ContainerInterface $container) {
73+
return new static(
74+
$container->get('apigee_edge.sdk_connector'),
75+
$container->get('apigee_edge.controller.organization')
76+
);
77+
}
78+
79+
/**
80+
* {@inheritdoc}
81+
*/
82+
public function validate($value, Constraint $constraint) {
83+
try {
84+
if (!isset($this->cache[$this->sdkConnector->getOrganization()])) {
85+
$this->cache[$this->sdkConnector->getOrganization()] = $this->orgController;
86+
}
87+
// Check if organization is ApigeeX.
88+
if ($this->cache[$this->sdkConnector->getOrganization()]->isOrganizationApigeeX()) {
89+
foreach ($value as $item) {
90+
if (preg_match('/[A-Z]/', $item->value)) {
91+
// The value contains uppercase character, the error, is applied.
92+
$this->context->addViolation($constraint->notLowercase, ['%value' => $item->value]);
93+
}
94+
}
95+
}
96+
}
97+
catch (\Exception $e) {
98+
// If not able to connect to Apigee Edge.
99+
\Drupal::logger('apigee_edge')->error($e->getMessage());
100+
}
101+
}
102+
103+
}

0 commit comments

Comments
 (0)