summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorlogickal2017-07-22 06:15:17 (GMT)
committerMateu Aguiló Bosch2017-07-22 06:15:17 (GMT)
commit095bc9e7070edd90570822a7e0344302db2693fd (patch)
tree7513b1810afb23becee6db5d037a9c06d06483d9
parent9a67e446b21791414cada55fe92ec5c049cdd43b (diff)
fix(Form): Enforce unicity of form elements (#2892024 by logickal, mkolar, vpeltot)
-rw-r--r--src/Form/JsonapiResourceConfigForm.php51
1 files changed, 51 insertions, 0 deletions
diff --git a/src/Form/JsonapiResourceConfigForm.php b/src/Form/JsonapiResourceConfigForm.php
index 256d854..9372d4e 100644
--- a/src/Form/JsonapiResourceConfigForm.php
+++ b/src/Form/JsonapiResourceConfigForm.php
@@ -164,6 +164,57 @@ class JsonapiResourceConfigForm extends EntityForm {
/**
* {@inheritdoc}
*/
+ public function validateForm(array &$form, FormStateInterface $form_state) {
+ $form_values = $form_state->getValues();
+ $resourceFields = $form_values['resourceFields'];
+ $overrides = [];
+
+ // Get the field values
+ foreach ($resourceFields as $field => $data) {
+ // Only get the overridden fields.
+ if ($data['fieldName'] != $data['publicName']) {
+ // Store the publicName for comparison.
+ $overrides[$field] = $data['publicName'];
+ }
+ }
+
+ // Compare the overrides and find any duplicate values.
+ $deduped_overrides = array_unique($overrides);
+ $dupes = array_diff_assoc($overrides, $deduped_overrides);
+ // Set an error if there are duplicates.
+ if ($dupes) {
+ foreach ($dupes as $field => $value) {
+ $form_state->setErrorbyName('resourceFields][' . $field . '][publicName', $this->t('The override must be unique.'));
+ }
+ }
+ // Now compare the overrides with the default names to validate no dupes exist.
+ foreach ($overrides as $field => $override) {
+ if (array_key_exists($override, $resourceFields)) {
+ $form_state->setErrorByName('resourceFields][' . $field . '][publicName', $this->t('The override must be unique.'));
+ }
+ }
+
+ // Validate URL and resource type
+ $resource_types = $this->entityTypeManager
+ ->getStorage('jsonapi_resource_config')
+ ->loadByProperties(['disabled' => FALSE]);
+ foreach ($resource_types as $id => $resource_type) {
+ if ($this->entity->id() == $id) {
+ continue;
+ }
+
+ if ($resource_type->get('resourceType') == $form_values['resourceType']) {
+ $form_state->setErrorByName('resourceType', $this->t('There is already resource (:name) with this override.', [':name' => $resource_type->id()]));
+ }
+ if ($resource_type->get('path') == $form_values['path']) {
+ $form_state->setErrorByName('path', $this->t('There is already resource (:name) with this path.', [':name' => $resource_type->id()]));
+ }
+ }
+ }
+
+ /**
+ * {@inheritdoc}
+ */
public function save(array $form, FormStateInterface $form_state) {
$resource_config = $this->entity;
$status = $resource_config->save();