1 <?php
2 /**
3 * @package Joomla.Libraries
4 * @subpackage Form
5 *
6 * @copyright Copyright (C) 2005 - 2017 Open Source Matters, Inc. All rights reserved.
7 * @license GNU General Public License version 2 or later; see LICENSE.txt
8 */
9
10 defined('JPATH_PLATFORM') or die;
11
12 use Joomla\Registry\Registry;
13
14 /**
15 * Form Rule class for the Joomla Platform.
16 *
17 * @since 11.1
18 */
19 class JFormRuleNotequals extends JFormRule
20 {
21 /**
22 * Method to test if two values are not equal. To use this rule, the form
23 * XML needs a validate attribute of equals and a field attribute
24 * that is equal to the field to test against.
25 *
26 * @param SimpleXMLElement $element The SimpleXMLElement object representing the `<field>` tag for the form field object.
27 * @param mixed $value The form field value to validate.
28 * @param string $group The field name group control value. This acts as an array container for the field.
29 * For example if the field has name="foo" and the group value is set to "bar" then the
30 * full field name would end up being "bar[foo]".
31 * @param Registry $input An optional Registry object with the entire data set to validate against the entire form.
32 * @param JForm $form The form object for which the field is being tested.
33 *
34 * @return boolean True if the value is valid, false otherwise.
35 *
36 * @since 11.1
37 * @throws InvalidArgumentException
38 * @throws UnexpectedValueException
39 */
40 public function test(SimpleXMLElement $element, $value, $group = null, Registry $input = null, JForm $form = null)
41 {
42 $field = (string) $element['field'];
43
44 // Check that a validation field is set.
45 if (!$field)
46 {
47 throw new UnexpectedValueException(sprintf('$field empty in %s::test', get_class($this)));
48 }
49
50 if ($input === null)
51 {
52 throw new InvalidArgumentException(sprintf('The value for $input must not be null in %s', get_class($this)));
53 }
54
55 // Test the two values against each other.
56 if ($value != $input->get($field))
57 {
58 return true;
59 }
60
61 return false;
62 }
63 }
64