public function BynderConfigurationForm::validateForm in Bynder 8
Same name and namespace in other branches
- 8.3 src/Form/BynderConfigurationForm.php \Drupal\bynder\Form\BynderConfigurationForm::validateForm()
- 8.2 src/Form/BynderConfigurationForm.php \Drupal\bynder\Form\BynderConfigurationForm::validateForm()
- 4.0.x src/Form/BynderConfigurationForm.php \Drupal\bynder\Form\BynderConfigurationForm::validateForm()
Form validation handler.
Parameters
array $form: An associative array containing the structure of the form.
\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.
Overrides FormBase::validateForm
File
- src/
Form/ BynderConfigurationForm.php, line 352
Class
- BynderConfigurationForm
- Configure bynder to enable OAuth based access.
Namespace
Drupal\bynder\FormCode
public function validateForm(array &$form, FormStateInterface $form_state) {
$credentials = $form_state
->getValue('credentials');
foreach ([
'consumer_secret',
'token_secret',
] as $name) {
if (!ctype_alnum(trim($credentials[$name]))) {
$form_state
->setError($form[$name], $this
->t('@label needs to contain only letters and numbers.', [
'@label' => $form[$name]['#title']
->render(),
]));
}
}
foreach ([
'consumer_key',
'token',
] as $name) {
$parts = explode('-', trim($credentials[$name]));
if (strlen($parts[0]) !== 8 || strlen($parts[1]) !== 4 || strlen($parts[2]) !== 4 || strlen($parts[3]) !== 16 || isset($parts[4])) {
$form_state
->setError($form[$name], $this
->t('@label needs to use the pattern 8-4-4-16.', [
'@label' => $form[$name]['#title']
->render(),
]));
}
foreach ($parts as $part) {
if (!ctype_alnum($part)) {
$form_state
->setError($form[$name], $this
->t('@label needs to use only numbers and letters separated with "-".', [
'@label' => $form[$name]['#title']
->render(),
]));
break;
}
}
}
// Makes sure we don't have a leading slash in the domain url.
$credentials['account_domain'] = rtrim($credentials['account_domain'], '/');
$form_state
->setValue([
'credentials',
'account_domain',
], $credentials['account_domain']);
if (!(substr(trim($credentials['account_domain']), 0, 8) === 'https://') || filter_var(trim($credentials['account_domain']), FILTER_VALIDATE_URL) === FALSE) {
$form_state
->setError($form['account_domain'], $this
->t('Account domain expect a valid secure url format, as provided to you by Bynder: ":url".', [
':url' => 'https://bynder-domain.extension/',
]));
}
if ($form_state
->getValue('test_connection')) {
if (!$form_state::hasAnyErrors() && !$this
->testApiConnection($credentials['consumer_key'], $credentials['consumer_secret'], $credentials['token'], $credentials['token_secret'], $credentials['account_domain'])) {
$form_state
->setErrorByName('credentials', $this
->t('Could not establish connection with Bynder. Check your credentials or <a href=":support">contact support.</a>', [
':support' => 'mailto:support@getbynder.com',
]));
}
}
parent::validateForm($form, $form_state);
}