[DataManager] Add validation for input fields#152
Conversation
| var digitizer = $('.mb-element-digitizer').data('mapbenderMbDigitizer'); | ||
| var regexPattern = (settings.hasOwnProperty('pattern')) ? settings.pattern : digitizer.options.regexPattern; | ||
| if (!$input.attr('pattern')) { | ||
| $input.attr('pattern', regexPattern); |
There was a problem hiding this comment.
This results in a misleading error message being shown when the regex validates incorrectly
Also, since the JS regex parser does not allow \p{L} (see mapbender/mapbender#1824 (review)) maybe it's after all best to just use serverside validation. This would also avoid the problem of having to get the digitizer/datamanager data above.
| .attr('name', settings.name || null) | ||
| .addClass('form-control') | ||
| ; | ||
| var digitizer = $('.mb-element-digitizer').data('mapbenderMbDigitizer'); |
There was a problem hiding this comment.
This should also work for pure DataManagers that don't use the Digitizer and therefore does not have mapbenderMbDigitizer set
There was a problem hiding this comment.
client side validation removed, see above
| protected function validateInputData($elementConfig, $schemaConfig) | ||
| { | ||
| $regexPattern = $elementConfig['regexPattern']; | ||
| $this->iterateFormItems($schemaConfig['formItems'], $regexPattern); | ||
| } | ||
|
|
||
| protected function iterateFormItems($formItems, $regexPattern) | ||
| { | ||
| foreach ($formItems as $formItem) { | ||
| if (array_key_exists('children', $formItem)) { | ||
| $this->iterateFormItems($formItem['children'], $regexPattern); | ||
| } else { | ||
| if (array_key_exists('name', $formItem)) { | ||
| $pattern = (array_key_exists('pattern', $formItem)) ? $formItem['pattern'] : $regexPattern; | ||
| if (!preg_match('/' . $pattern . '/', $formItem['name'])) { | ||
| throw new BadRequestHttpException('api.query.error-invalid-data'); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } |
There was a problem hiding this comment.
the server side validation does not seem to work. When I remove the pattern attribute, I can still save any string even though it does not match the pattern.
|
@Phocacius |
Adds a validation for all input fields with the following regex:
^[\p{L}0-9_\-\s@]*$Can be overwritten selectively by adding the pattern attribute to the digitizer's yaml configuration:
pattern attribute existed before, see: https://doc.mapbender.org/en/elements/editing/digitizer/digitizer_configuration.html#customization-by-attr-object-definitions