public static function JsonEditorWidget::validateJsonSchema in JSON Field 8
Ensure the JSON schema is itself valid and supported by the PHP library.
Parameters
$element:
\Drupal\Core\Form\FormStateInterface $form_state:
File
- src/
Plugin/ Field/ FieldWidget/ JsonEditorWidget.php, line 192
Class
- JsonEditorWidget
- Plugin implementation of the 'json_editor' widget.
Namespace
Drupal\json_field\Plugin\Field\FieldWidgetCode
public static function validateJsonSchema($element, FormStateInterface $form_state) {
try {
// Do not use Json::decode since it forces a return as Array.
$value = json_decode($element['#value']);
// If the schema is empty do not try to validate as it will always fail
// and it will not be possible to save the form.
if (!empty($value)) {
$schema = Schema::import($value);
}
} catch (\Exception $e) {
$form_state
->setError($element, t('JSON Schema is not valid.'));
}
}