[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/src/Form/Rule/ -> SubformRule.php (source)

   1  <?php
   2  
   3  /**
   4   * Joomla! Content Management System
   5   *
   6   * @copyright  (C) 2019 Open Source Matters, Inc. <https://www.joomla.org>
   7   * @license    GNU General Public License version 2 or later; see LICENSE.txt
   8   */
   9  
  10  namespace Joomla\CMS\Form\Rule;
  11  
  12  use Joomla\CMS\Form\Field\SubformField;
  13  use Joomla\CMS\Form\Form;
  14  use Joomla\CMS\Form\FormRule;
  15  use Joomla\Registry\Registry;
  16  
  17  // phpcs:disable PSR1.Files.SideEffects
  18  \defined('JPATH_PLATFORM') or die;
  19  // phpcs:enable PSR1.Files.SideEffects
  20  
  21  /**
  22   * Form rule to validate subforms field-wise.
  23   *
  24   * @since  3.9.7
  25   */
  26  class SubformRule extends FormRule
  27  {
  28      /**
  29       * Method to test given values for a subform..
  30       *
  31       * @param   \SimpleXMLElement  $element  The SimpleXMLElement object representing the `<field>` tag for the form field object.
  32       * @param   mixed              $value    The form field value to validate.
  33       * @param   string             $group    The field name group control value. This acts as as an array container for the field.
  34       *                                       For example if the field has name="foo" and the group value is set to "bar" then the
  35       *                                       full field name would end up being "bar[foo]".
  36       * @param   Registry           $input    An optional Registry object with the entire data set to validate against the entire form.
  37       * @param   Form               $form     The form object for which the field is being tested.
  38       *
  39       * @return  boolean  True if the value is valid, false otherwise.
  40       *
  41       * @since   3.9.7
  42       */
  43      public function test(\SimpleXMLElement $element, $value, $group = null, Registry $input = null, Form $form = null)
  44      {
  45          // Get the form field object.
  46          $field = $form->getField($element['name'], $group);
  47  
  48          if (!($field instanceof SubformField)) {
  49              throw new \UnexpectedValueException(sprintf('%s is no subform field.', $element['name']));
  50          }
  51  
  52          if ($value === null) {
  53              return true;
  54          }
  55  
  56          $subForm = $field->loadSubForm();
  57  
  58          // Multiple values: Validate every row.
  59          if ($field->multiple) {
  60              foreach ($value as $row) {
  61                  if ($subForm->validate($row) === false) {
  62                      // Pass the first error that occurred on the subform validation.
  63                      $errors = $subForm->getErrors();
  64  
  65                      if (!empty($errors[0])) {
  66                          return $errors[0];
  67                      }
  68  
  69                      return false;
  70                  }
  71              }
  72          } else {
  73              // Single value.
  74              if ($subForm->validate($value) === false) {
  75                  // Pass the first error that occurred on the subform validation.
  76                  $errors = $subForm->getErrors();
  77  
  78                  if (!empty($errors[0])) {
  79                      return $errors[0];
  80                  }
  81  
  82                  return false;
  83              }
  84          }
  85  
  86          return true;
  87      }
  88  }


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer