[OJS]Affiliation as a required field

Hi,

I need to make the affiliation field a required one for users but i don’t know how.

I already managed to change the country and mailing Address for the registrations process:

classes/user/form/RegistrationForm.inc.php: function RegistrationForm()

$this->addCheck(new FormValidator($this, 'mailingAddress', 'required', 'user.profile.form.mailingAddressRequired')); $this->addCheck(new FormValidator($this, 'country', 'required', 'user.profile.form.countryRequired'));

However, if i use the same for the affiliation field, it always accepts empty inputs, i believe this may have something to do with the affiliation field being localized.

What should i do?

Thanks.

Hi @samueloph,

What software are you using, and what version?

Regards,
Alec Smecher
Public Knowledge Project Team

It’s OJS 2.4.7-1, but we should upgrade to 2.4.8 soon.

Hi @samueloph,

For localized fields, try using FormValidatorLocale instead. See classes/author/form/submit/AuthorSubmitStep3Form.inc.php for an example of this class being used.

Regards,
Alec Smecher
Public Knowledge Project Team

Thanks @asmecher, that did the trick.

@asmecher

How should i proceed to require affiliation for classes/author/form/submit/AuthorSubmitStep3Form.inc.php too?

This should be different since i will be dealing with a localized field from an array, so neither
FormValidatorArray or FormValidatorLocale works.

I believe i could mix the two of them by working with FormValidatorCustom but i’m not really sure about how to do it.

Thanks.

Hi @samueloph,

Try tinkering with a FormValidatorArrayCustom validator. There’s no specific validator class to do what you want, but you can supply a custom function to that one.

Regards,
Alec Smecher
Public Knowledge Project Team

Hi @asmecher,

I will try to use FormValidatorArrayCustom then, i know that
$this->addCheck(new FormValidatorArrayCustom($this, 'authors', 'required', 'user.profile.form.affiliationRequired', create_function('$affiliation', 'return empty($affiliation);'), array('affiliation')));
won’t work, but how i’m supposed to deal with affiliation? I don’t know how localized fields are treated inside OJS, is it an array with affiliation + locale? I will try to use array_key_exists but i wanna make sure that if it doesn’t works it’s something i’m doing wrong and the approach is fine.

Thanks for your help.

Hi @samueloph,

If I recall correctly (!), the authors form variable will come in as a list of authors, each an array with its own structures. So from the top level it will be something like

$authors[0]['affiliation']['en_US']

…for the first author’s affiliation in English. In create_function the variable that’s passed in should be called $author, and within that you can look up e.g. $author['affiliation']['en_US'].

I’d suggest using something like error_log(print_r($someVariable, true)); within the function to dump the structure if you’re having trouble.

Regards,
Alec Smecher
Public Knowledge Project Team

Hi @asmecher,

I’m trying to work by copying the orcid one:
$this->addCheck(new FormValidatorArrayCustom($this, 'authors', 'required', 'user.profile.form.orcidInvalid', create_function('$orcid', '$validator = new ValidatorORCID(); return empty($orcid) ? true : $validator->isValid($orcid);'), array(), false, array('orcid')));

I’ve tried
$this->addCheck(new FormValidatorArrayCustom($this, 'authors', 'required', 'user.profile.form.affiliationRequired"', create_function('$affiliation', 'return empty($affiliation[AppLocale::getPrimaryLocale()]);'), array(), false, array('affiliation')));

Without success, i couldn’t log the variables authors nor author too, i believe the problem resides on the AppLocale part.

Can you give me a hint on what i’m doing wrong?

Thanks.

Hi @samueloph,

At the least, I think you’ve got your test inverted; it should be !empty not empty, I think.

Regards,
Alec Smecher
Public Knowledge Project Team

Hi @asmecher,

Bullseye! I was in a hurry and did not made proper analysis of the code.

Thanks a lot! Really!

1 Like