Skip to content

Commit

Permalink
pkp#6099 Added an optional country field to the context form.
Browse files Browse the repository at this point in the history
  • Loading branch information
jonasraoni committed Apr 15, 2021
1 parent 9048941 commit 7cefc95
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 2 deletions.
20 changes: 20 additions & 0 deletions classes/components/forms/context/PKPContextForm.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
use \PKP\components\forms\FieldOptions;
use \PKP\components\forms\FieldText;
use \PKP\components\forms\FieldRichTextarea;
use \PKP\components\forms\FieldSelect;
use \Sokil\IsoCodes\IsoCodesFactory;

define('FORM_CONTEXT', 'context');

Expand All @@ -39,6 +41,18 @@ public function __construct($action, $locales, $baseUrl, $context) {
$this->locales = $locales;
$this->method = $context ? 'PUT' : 'POST';

$isoCodes = new IsoCodesFactory();
$countries = array();
foreach ($isoCodes->getCountries() as $country) {
$countries[] = [
'value' => $country->getAlpha2(),
'label' => $country->getLocalName()
];
}
usort($countries, function ($a, $b) {
return strcmp($a['label'], $b['label']);
});

$this->addField(new FieldText('name', [
'label' => __('manager.setup.contextTitle'),
'isRequired' => true,
Expand All @@ -53,6 +67,12 @@ public function __construct($action, $locales, $baseUrl, $context) {
'groupId' => 'identity',
'value' => $context ? $context->getData('acronym') : null,
]))
->addField(new FieldSelect('country', [
'label' => __('common.country'),
'description' => __('manager.setup.selectCountry'),
'options' => $countries,
'value' => $context ? $context->getData('country') : null,
]))
->addField(new FieldRichTextarea('description', [
'label' => __('admin.contexts.contextDescription'),
'isMultilingual' => true,
Expand Down
12 changes: 10 additions & 2 deletions classes/validation/ValidatorFactory.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
use Illuminate\Filesystem\Filesystem;
use Illuminate\Translation\FileLoader;
use Illuminate\Translation\Translator;
use Illuminate\Validation\DatabasePresenceVerifier;
use Illuminate\Validation\Factory;
use Sokil\IsoCodes\IsoCodesFactory;

// Import VALIDATE_ACTION_... constants
import('lib.pkp.classes.services.interfaces.EntityWriteInterface');
Expand Down Expand Up @@ -118,11 +118,18 @@ static public function make($props, $rules, $messages = []) {

// Add custom validation rule for currency
$validation->extend('currency', function($attribute, $value, $parameters, $validator) {
$isoCodes = new \Sokil\IsoCodes\IsoCodesFactory();
$isoCodes = new IsoCodesFactory();
$currency = $isoCodes->getCurrencies()->getByLetterCode($value);
return isset($currency);
});

// Add custom validation rule for country
$validation->extend('country', function($attribute, $value, $parameters, $validator) {
$isoCodes = new IsoCodesFactory();
$country = $isoCodes->getCountries()->getByAlpha2($value);
return isset($country);
});

$validator = $validation->make($props, $rules, ValidatorFactory::getMessages($messages));

return $validator;
Expand Down Expand Up @@ -157,6 +164,7 @@ static public function getMessages($messages = []) {
],
'boolean' => __('validator.boolean'),
'confirmed' => __('validator.confirmed'),
'country' => __('validator.country'),
'currency' => __('validator.currency'),
'date' => __('validator.date'),
'date_format' => __('validator.date_format'),
Expand Down
3 changes: 3 additions & 0 deletions locale/en_US/common.po
Original file line number Diff line number Diff line change
Expand Up @@ -1826,6 +1826,9 @@ msgstr "This field must be true or false."
msgid "validator.confirmed"
msgstr "The confirmation for this field does not match."

msgid "validator.country"
msgstr "This is not a valid country."

msgid "validator.currency"
msgstr "This is not a valid currency."

Expand Down
6 changes: 6 additions & 0 deletions schemas/context.json
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,12 @@
"nullable"
]
},
"country": {
"type": "string",
"validation": [
"country"
]
},
"coverage": {
"type": "string",
"description": "Enable coverage metadata. `0` is disabled. `enable` will make it available in the workflow. `request` will allow an author to enter a value during submission. `require` will require that the author enter a value during submission.",
Expand Down

0 comments on commit 7cefc95

Please sign in to comment.