Skip to content

Commit 1954f0a

Browse files
authored
[5.1] Form: Add regex validation rule (#42657)
1 parent 96618f6 commit 1954f0a

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
/**
4+
* Joomla! Content Management System
5+
*
6+
* @copyright (C) 2024 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('_JEXEC') or die;
18+
// phpcs:enable PSR1.Files.SideEffects
19+
20+
/**
21+
* Form Rule class for the Joomla Platform.
22+
*
23+
* @since __DEPLOY_VERSION__
24+
*/
25+
class RegexRule extends FormRule
26+
{
27+
/**
28+
* Method to test the value.
29+
*
30+
* @param \SimpleXMLElement $element The SimpleXMLElement object representing the `<field>` tag for the form field object.
31+
* @param mixed $value The form field value to validate.
32+
* @param string $group The field name group control value. This acts as an array container for the field.
33+
* For example if the field has name="foo" and the group value is set to "bar" then the
34+
* full field name would end up being "bar[foo]".
35+
* @param ?Registry $input An optional Registry object with the entire data set to validate against the entire form.
36+
* @param ?Form $form The form object for which the field is being tested.
37+
*
38+
* @return boolean True if the value is valid, false otherwise.
39+
*
40+
* @since __DEPLOY_VERSION__
41+
* @throws \UnexpectedValueException if rule is invalid.
42+
*/
43+
public function test(\SimpleXMLElement $element, $value, $group = null, Registry $input = null, Form $form = null)
44+
{
45+
if ((string) $element['validate_regex']) {
46+
$this->regex = (string) $element['validate_regex'];
47+
}
48+
49+
if ((string) $element['validate_modifier']) {
50+
$this->modifiers = (string) $element['validate_modifier'];
51+
}
52+
53+
return parent::test($element, $value, $group, $input, $form);
54+
}
55+
}

0 commit comments

Comments
 (0)