Question about checkbox in FORM_TITLE_ABSTRACT

Hi @NateWr,
I am trying to create a checkbox in the following places: Quicksubmitform submissionsubmitstep3form, and FORM_TITLE_ABSTRACT.

The checkbox works great in the quicksubmitform and submissionsubmitstep3form. However the FORM_TITLE_ABSTRACT is a little bit different. I tried to use this post as a guide:

Here you are suggesting to use Form::config::before and schema::get::publication, which I did. The checkbox shows up and the value is correctly saved in the publication_settings table.
However, every time I uncheck the checkbox in FORM_TITLE_ABSTRACT it goes back to “checked” after I click save. I am not sure how to approach this issue, since the values are correctly saved but the checkbox is shown incorrectly. Do you have an idea what the problem might be? Am I missing another hook so it shows up unchecked if I uncheck it and save it?

I am using OJS version 3.3.0-3.

Code:

public function addtoForm($hookName, $form)
	{

		// Only modify the form title abstract.
		if (!defined('FORM_TITLE_ABSTRACT') || $form->id !== FORM_TITLE_ABSTRACT) {
			return;
		}

		// Don't do anything at the site-wide level, only dependent context/journal.
		$request = Application::get()->getRequest();
		$context = $request->getContext();
		if (!$context) {
			return;
		}

		// Add a field to the form.
		$form->addField(new \PKP\components\forms\FieldText('ppn', [
			'label' => __('plugins.generic.ppn.field.title'),
		]));

		// Add a field to the form.
		$form->addField(new \PKP\components\forms\FieldText('customComment', [
			'label' => __('plugins.generic.custom.comment.field.title'),
		]));

		$form->addField(new \PKP\components\forms\FieldOptions('nativePub', [
			'label' => __('plugins.generic.isDigital.section.title'),
			'options' => [
				['label' => __('plugins.generic.digital.version.field.title')],
			],
		]));

		return false;
	}

Best,
Tina

Hi @vmayer,

Whenever you add a FieldOptions to the form, each option must have a value. Try the following:

$form->addField(new \PKP\components\forms\FieldOptions('nativePub', [
    'label' => __('plugins.generic.isDigital.section.title'),
    'options' => [
        [
            'value' => false,
            'label' => __('plugins.generic.digital.version.field.title')
        ],
    ],
    'value' => ...,
]));

That might solve your problem. However, typically a Form needs to provide the initial data. For example, the form in OJS uses the $publication object to set each field’s value:

public function __construct($action, $locales, $publication) {

    $this->addField(new FieldText('prefix', [
        // ...
        'value' => $publication->getData('prefix'),
    ]));
}

Unfortunately, the $publication object is not available from the hook. That’s an oversight. Ideally, the $publication object should be assigned to the form so that it can be reused in hooks, like this:

class PKPTitleAbstractForm extends FormComponent {
    
    /** @var Publication */
    public $publication;

    public function __construct($action, $locales, $publication) {
        $this->publication = $publication;

        // ...
    }
}

Then you would be able to use it in your hook’s callback:

public function addtoForm($hookName, $form)
{
    $form->addField(..., [
        'value' => $form->publication->getData(...)
    ]);
}

If you’d be willing to contribute that change to our stable-3_3_0 branch, I’d be happy to merge it in.

I filed an issue for this, so you can add details there if you’re able to create a pull request for this: https://github.com/pkp/pkp-lib/issues/7681

1 Like

Hi @NateWr ,

thank you so much for your quick reply. I noticed that the Publication object is missing, so I used a workaround like in the JATSParserPlugin in which they used the path to get the publication:

I will create a PR to include the publication object into this form.

Back to my problem: I tried what you said and unfortunately the checkbox still goes back to “checked” when saving while the setting_value in publication_settings is correctly saved as false.
Could it be a problem with the data type that is saved in the DB? I read here that checkbox values should be arrays (mine is a string): Several checkboxes in theme - #3 by UBWolf

Best,
Tina

Ok I found out what the Problem was. I saved the value of the checkbox as string instead of boolean.
The schema looks like this now:

		$schema->properties->nativePub = (object) [
			'type' => 'boolean',
			'apiSummary' => true,
			'multilingual' => false,
			'validation' => ['nullable']
		];

FieldOptions looks like this:

		$form->addField(new \PKP\components\forms\FieldOptions('nativePub', [
			'label' => __('plugins.generic.isDigital.section.title'),
			'options' => [
				['value' => $form->publication->getData('nativePub'), 'label' => __('plugins.generic.digital.version.field.title')],
			],
		]));

Now when I uncheck the checkbox it actually stays unchecked and the value is saved correctly.

1 Like

That’s great! Yeah, so the value of the property for a FieldOptions depends on how it’s used:

  • boolean for a FieldOptions with one option that’s true/false.
  • array for a FieldOptions that is a checkbox with several options
  • usually string or int for a FieldOptions with inputType of radio where only one option can be selected at a time.