OJS OCS OMP OHS

You are viewing the PKP Support Forum | PKP Home Wiki



Form validation for multiple biographies (text submission)

Are you responsible for making OJS work -- installing, upgrading, migrating or troubleshooting? Do you think you've found a bug? Post in this forum.

Moderators: jmacgreg, btbell, michael, bdgregg, barbarah, asmecher

Forum rules
What to do if you have a technical problem with OJS:

1. Search the forum. You can do this from the Advanced Search Page or from our Google Custom Search, which will search the entire PKP site. If you are encountering an error, we especially recommend searching the forum for said error.

2. Check the FAQ to see if your question or error has already been resolved.

3. Post a question, but please, only after trying the above two solutions. If it's a workflow or usability question you should probably post to the OJS Editorial Support and Discussion subforum; if you have a development question, try the OJS Development subforum.

Form validation for multiple biographies (text submission)

Postby Looted » Sun Nov 10, 2013 7:22 pm

Hello,

I'm trying to make it neccessary for authors to enter biographies for every author of a submitted text. I did well with abstracts and titles (also managed to check if *both* language versions are entered, not only the main language version). The author biographies stopped me in my tracks however, being both multilangual, and an array it seems?

I've tried:
Code: Select all
$this->addCheck(new FormValidatorLocale($this, 'biography', 'required', 'author.submit.form.biography', 'pl_PL'));

and:
Code: Select all
$this->addCheck(new FormValidatorLocale($this, 'authors[0][biography]', 'required', 'author.submit.form.biography', 'pl_PL'));

also tried more advanced version:
Code: Select all
for ($x=0, $count2=count(authors); $x < $count2; $x++){
$strink='authors['.$x.'][biography'];
$this->addCheck(new FormValidatorLocale($this, $strink, 'required', 'author.submit.form.biogramPL', 'pl_PL'));
$this->addCheck(new FormValidatorLocale($this, $strink, 'required', 'author.submit.form.biogramUS', 'en_US'));


Nothing seems to work, I don't understand why even straight out typing the field name doesn't work:
Code: Select all
$this->addCheck(new FormValidator($this, 'authors[0][biography][pl_PL]', 'required', 'author.submit.form.biography'));


The problem with it is, that it is both localized and some kind of subarray of authors? I don't know how to do it, i checked almost every form class in OJS to find similar situation, but did not find one. I would be gratefull if someone could help me with this.
Cheers!
Looted
 
Posts: 13
Joined: Tue Oct 29, 2013 10:06 am

Re: Form validation for multiple biographies (text submissio

Postby Looted » Mon Nov 11, 2013 11:29 am

Ok, i managed to bruteforce the function somehow. I added a new validator class, which functions similarilly to FormValidatorArray, but checks even deeper for the locale values. Anyway, I'm leaving the code here in case someone needs it in the future:
Code: Select all
class FormValidatorArrayPL extends FormValidator {

   /** @var array Array of fields to check */
   var $_fields;

   /** @var array Array of field names where an error occurred */
   var $_errorFields;

   /**
    * Constructor.
    * @param $form Form the associated form
    * @param $field string the name of the associated field
    * @param $type string the type of check, either "required" or "optional"
    * @param $message string the error message for validation failures (i18n key)
    * @param $fields array all subfields for each item in the array, i.e. name[][foo]. If empty it is assumed that name[] is a data field
    */
   function FormValidatorArrayPL(&$form, $field, $type, $message, $fields = array()) {
      parent::FormValidator($form, $field, $type, $message);
      $this->_fields = $fields;
      $this->_errorFields = array();
   }


   //
   // Setters and Getters
   //
   /**
    * Get array of fields where an error occurred.
    * @return array
    */
   function getErrorFields() {
      return $this->_errorFields;
   }


   //
   // Public methods
   //
   /**
    * @see FormValidator::isValid()
    * Value is valid if it is empty and optional or all field values are set.
    * @return boolean
    */
   function isValid() {
      if ($this->getType() == FORM_VALIDATOR_OPTIONAL_VALUE) return true;

      $data = $this->getFieldValue();
      if (!is_array($data)) return false;

      $isValid = true;
      foreach ($data as $key => $value) {
         if (count($this->_fields) == 0) {
            // We expect all fields to contain values.
            if (is_null($value) || trim((string)$value) == '') {
               $isValid = false;
               array_push($this->_errorFields, $this->getField()."[{$key}]");
            }
         } else {
            // In the two-dimensional case we always expect a value array.
            if (!is_array($value)) {
               $isValid = false;
               array_push($this->_errorFields, $this->getField()."[{$key}]");
               continue;
            }

            // Go through all sub-sub-fields and check them explicitly
            foreach ($this->_fields as $field) {
               if (!isset($value[$field]) || trim((string)$value[$field]) == '') {
                  $isValid = false;
                  array_push($this->_errorFields, $this->getField()."[{$key}][{$field}]");
               }
            }
            // Go through all sub-sub-fields and check them explicitly
            foreach ($this->_fields as $field) {
               if (!isset($value[$field]['pl_PL']) || trim((string)$value[$field]['pl_PL']) == '') {
                  $isValid = false;
                  array_push($this->_errorFields, $this->getField()."[{$key}][{$field}]['pl_PL']");
               }
            }
         }
      }

      return $isValid;
   }
}
Looted
 
Posts: 13
Joined: Tue Oct 29, 2013 10:06 am


Return to OJS Technical Support

Who is online

Users browsing this forum: Bing [Bot] and 6 guests