Skip to content

Commit

Permalink
feat: view form validation errors with no mapped element
Browse files Browse the repository at this point in the history
  • Loading branch information
janbritz committed Feb 6, 2025
1 parent d6025e8 commit 5d80eaa
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 4 deletions.
2 changes: 2 additions & 0 deletions classes/api/package_api.php
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,8 @@ private function post_and_maybe_retry(string $uri, array $options = [], bool $al
/**
* Creates the multipart parts array.
*
*
*
* @param array $main main JSON part
* @param string|null $questionstate optional question state
* @return array
Expand Down
3 changes: 1 addition & 2 deletions classes/api/qpy_http_client.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class qpy_http_client extends http_client {
public function __construct(array $config = []) {
$config['base_uri'] ??= rtrim(get_config('qtype_questionpy', 'server_url'), '/') . '/';
$config[RequestOptions::TIMEOUT] ??= get_config('qtype_questionpy', 'server_timeout');

parent::__construct($config);
}

Expand All @@ -63,7 +64,6 @@ public function __construct(array $config = []) {
* @return ResponseInterface
* @throws GuzzleException
* @throws request_error
* @throws options_form_validation_error
*/
public function get($uri, array $options = []): ResponseInterface {
return parent::get($uri, $options);
Expand All @@ -80,7 +80,6 @@ public function get($uri, array $options = []): ResponseInterface {
* @return ResponseInterface
* @throws GuzzleException
* @throws request_error
* @throws options_form_validation_error
*/
public function post($uri, array $options = []): ResponseInterface {
return parent::post($uri, $options);
Expand Down
18 changes: 17 additions & 1 deletion edit_questionpy_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ public function __construct(string $submiturl, object $question, object $categor
bool $formeditable = true) {
$this->api = di::get(api::class);
$this->packagefileservice = di::get(package_file_service::class);

parent::__construct($submiturl, $question, $category, $contexts, $formeditable);
}

Expand Down Expand Up @@ -312,6 +311,8 @@ public function set_data($question): void {
* @throws moodle_exception
*/
private function validate_options_form(array $data, ?stored_file $package, array &$errors): void {
$errorswithnoelement = [];

try {
$packagehash = $data['qpy_package_hash'] ?? $data['qpy_package_file_hash'];
// TODO: create a dedicated endpoint?
Expand All @@ -321,9 +322,24 @@ private function validate_options_form(array $data, ?stored_file $package, array
$element = 'qpy_form[' . str_replace('.', '][', $field) . ']';
if ($this->_form->elementExists($element)) {
$errors[$element] = $error;
} else {
$errorswithnoelement[$element] = $error;
}
}
}

if (!empty($errorswithnoelement)) {
$content = '';
foreach ($errorswithnoelement as $element => $error) {
$content .= get_string('options_form_validation_error_element', 'qtype_questionpy', [
'name' => $element,
'error' => $error,
]);
}
$composederrorstring = get_string('options_form_validation_error_container', 'qtype_questionpy', $content);
// Use the 'Question name' input field to view the errors.
$errors['name'] = $composederrorstring;
}
}

/**
Expand Down
3 changes: 3 additions & 0 deletions lang/en/qtype_questionpy.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@
$string['max_package_size_kb'] = 'Maximum file size of a QuestionPy package';
$string['max_package_size_kb_description'] = 'Maximum file size in kB';
$string['open_website'] = 'Open website';
$string['options_form_validation_error_container'] = 'The following errors occurred while validating the form and could not'
. ' be mapped to an input element: <ul>{$a}</ul>';
$string['options_form_validation_error_element'] = '<li>{$a->name}: {$a->error}</li>';
$string['package_not_found'] = 'The requested package {$a->packagehash} does not exist.';
$string['packages_subheading'] = 'Packages';
$string['pluginname'] = 'QuestionPy';
Expand Down
2 changes: 1 addition & 1 deletion version.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
defined('MOODLE_INTERNAL') || die();

$plugin->component = 'qtype_questionpy';
$plugin->version = 2024101700;
$plugin->version = 2024101704;
$plugin->requires = 2024042200;
$plugin->maturity = MATURITY_ALPHA;
$plugin->release = '0.1';
Expand Down

0 comments on commit 5d80eaa

Please sign in to comment.