Skip to content

Commit

Permalink
Changes added pattern message and delimiters
Browse files Browse the repository at this point in the history
  • Loading branch information
kedarkhaire committed Sep 7, 2023
1 parent 6f7df5b commit 515f5c9
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 6 deletions.
2 changes: 1 addition & 1 deletion config/install/apigee_edge.common_app_settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ default_products: []
analytics_environment: prod
analytics_available_environments:
- prod
callback_url_pattern: '^https?:\/\/.*$'
callback_url_pattern: '/^https?:\/\/.*$/'
callback_url_pattern_error_message: 'The Callback URL must start with http:// or https://'
callback_url_description: 'External site to which a consumer of this app is redirected to log in when using three-legged OAuth.'
callback_url_placeholder: ''
7 changes: 3 additions & 4 deletions src/Form/AppCallbackUrlSettingsForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function buildForm(array $form, FormStateInterface $form_state) {
'#type' => 'textfield',
'#title' => $this->t('Pattern'),
'#default_value' => $app_settings->get('callback_url_pattern'),
'#description' => $this->t('Regular expression that a Callback URL should match. Default is "^https?:\/\/.*$" that ensures callback url starts with either <em>http://</em> or <em>https://</em>.'),
'#description' => $this->t('<a href="https://www.php.net/manual/en/function.preg-match.php" target="_blank">Regular expression </a> that a Callback URL should match. Default is "/^https?:\/\/.*$/" that ensures callback url starts with either <em>http://</em> or <em>https://</em>.'),
'#required' => TRUE,
];
$form['callback_url']['pattern_error_message'] = [
Expand Down Expand Up @@ -94,9 +94,8 @@ public function validateForm(array &$form, FormStateInterface $form_state) {
parent::validateForm($form, $form_state);

$isRegExValid = FALSE;
$pattern = '/' . $form_state->getValue(['callback_url', 'pattern']) . '/';
try {
if (@preg_match($pattern, '') !== FALSE) {
if (@preg_match($form_state->getValue(['callback_url', 'pattern']), '') !== FALSE) {
$isRegExValid = TRUE;
}
}
Expand All @@ -105,7 +104,7 @@ public function validateForm(array &$form, FormStateInterface $form_state) {
}

if (!$isRegExValid) {
$form_state->setError($form['callback_url']['pattern'], $this->t('The pattern should be a valid regular expression. Please remove the delimiters if added.'));
$form_state->setError($form['callback_url']['pattern'], $this->t('The pattern should be a valid regular expression. It should /start and end/ with proper delimiters.'));
}
}

Expand Down
6 changes: 5 additions & 1 deletion src/Plugin/Field/FieldWidget/AppCallbackUrlWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,11 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen
// Fallback to the default configuration if field widget does
// not have an override.
$app_settings = \Drupal::config("apigee_edge.common_app_settings");
$element['value']['#pattern'] = $this->getSetting('callback_url_pattern') ?? $app_settings->get('callback_url_pattern');
$callback_url_pattern = $this->getSetting('callback_url_pattern') ?? $app_settings->get('callback_url_pattern');
if (substr($callback_url_pattern, 0, 1) === '/' && substr($callback_url_pattern, -1) === '/') {
$callback_url_pattern = substr($callback_url_pattern, 1, -1);
}
$element['value']['#pattern'] = $callback_url_pattern;
$element['value']['#attributes']['title'] = $this->getSetting('callback_url_pattern_error_message') ?? $app_settings->get('callback_url_pattern_error_message');
$element['value']['#placeholder'] = $this->getSetting('placeholder') ?? $app_settings->get('callback_url_placeholder');
$element['value']['#description'] = $this->getSetting('callback_url_description') ?? $app_settings->get('callback_url_description');
Expand Down

0 comments on commit 515f5c9

Please sign in to comment.