[ Index ] |
PHP Cross Reference of Joomla 4.2.2 documentation |
[Summary view] [Print] [Text view]
1 <?php 2 3 /** 4 * Joomla! Content Management System 5 * 6 * @copyright (C) 2011 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\Form; 13 use Joomla\CMS\Form\FormRule; 14 use Joomla\Registry\Registry; 15 16 // phpcs:disable PSR1.Files.SideEffects 17 \defined('JPATH_PLATFORM') or die; 18 // phpcs:enable PSR1.Files.SideEffects 19 20 /** 21 * Form Rule class for the Joomla Platform. 22 * Requires the value entered be one of the options in a field of type="list" 23 * 24 * @since 1.7.0 25 */ 26 class OptionsRule extends FormRule 27 { 28 /** 29 * Method to test the value. 30 * 31 * @param \SimpleXMLElement $element The SimpleXMLElement object representing the `<field>` tag for the form field object. 32 * @param mixed $value The value to validate. 33 * @param string $group The field name group control value. This acts 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 1.7.0 42 */ 43 public function test(\SimpleXMLElement $element, $value, $group = null, Registry $input = null, Form $form = null) 44 { 45 // Check if the field is required. 46 $required = ((string) $element['required'] === 'true' || (string) $element['required'] === 'required'); 47 48 // Check if the value is empty. 49 $blank = empty($value) && $value !== '0' && $value !== 0 && $value !== 0.0; 50 51 if (!$required && $blank) { 52 return true; 53 } 54 55 // Make an array of all available option values. 56 $options = array(); 57 58 // Create the field 59 $field = null; 60 61 if ($form) { 62 $field = $form->getField((string) $element->attributes()->name, $group); 63 } 64 65 // When the field exists, the real options are fetched. 66 // This is needed for fields which do have dynamic options like from a database. 67 if ($field && \is_array($field->options)) { 68 foreach ($field->options as $opt) { 69 $options[] = $opt->value; 70 } 71 } else { 72 foreach ($element->option as $opt) { 73 $options[] = $opt->attributes()->value; 74 } 75 } 76 77 // There may be multiple values in the form of an array (if the element is checkboxes, for example). 78 if (\is_array($value)) { 79 // If all values are in the $options array, $diff will be empty and the options valid. 80 $diff = array_diff($value, $options); 81 82 return empty($diff); 83 } else { 84 // In this case value must be a string 85 return \in_array((string) $value, $options); 86 } 87 } 88 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Wed Sep 7 05:41:13 2022 | Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer |