Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[4.0] Adding filter() and postProcessing() to FormField #12414

Merged
merged 40 commits into from Jan 18, 2019
Merged
Changes from 37 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
85fda57
Adding filter(), validate() and postProcessing() to Form library
Hackwar Dec 22, 2018
18f5e59
Merge branch '4.0-dev' into j4form
Hackwar Dec 22, 2018
a87810d
Fixing codestyle
Hackwar Dec 22, 2018
fb2a364
Fixing bugs
Hackwar Dec 25, 2018
6417b93
Merge branch 'j4form' of github.com:Hackwar/joomla-cms into j4form
Hackwar Dec 25, 2018
7d9f6ac
Merge branch '4.0-dev' into j4form
Hackwar Dec 25, 2018
a451d53
Fixing old JText to Text
Hackwar Dec 25, 2018
ac22db4
Merge branch 'j4form' of github.com:Hackwar/joomla-cms into j4form
Hackwar Dec 25, 2018
8b4d496
Update libraries/src/Form/FormHelper.php
Quy Dec 27, 2018
249455c
Update libraries/src/Form/FormFilterInterface.php
Quy Dec 27, 2018
cc1c95f
Update libraries/src/Form/Filter/TelFilter.php
Quy Dec 27, 2018
56c25f8
Merge branch '4.0-dev' into j4form
Hackwar Dec 27, 2018
4fc4d58
Fixing several minor bugs
Hackwar Dec 28, 2018
8eb997e
Merge branch '4.0-dev' into j4form
Hackwar Dec 28, 2018
e3c7747
Fixing more bugs
Hackwar Dec 29, 2018
61adaf4
Merge branch 'j4form' of github.com:Hackwar/joomla-cms into j4form
Hackwar Dec 29, 2018
699f2d7
Merge branch '4.0-dev' into j4form
Hackwar Dec 29, 2018
830e72f
Additional fixes
Hackwar Dec 29, 2018
d25784f
Merge branch 'j4form' of github.com:Hackwar/joomla-cms into j4form
Hackwar Dec 29, 2018
e0bb9a8
Alphasorting use statements
Hackwar Dec 29, 2018
09f3ab3
Merge branch '4.0-dev' into j4form
Hackwar Dec 29, 2018
0037270
Update libraries/src/Form/Filter/IntarrayFilter.php
Quy Dec 29, 2018
b332658
Update libraries/src/Form/Filter/SafehtmlFilter.php
Quy Dec 29, 2018
6063169
Update libraries/src/Form/Filter/UnsetFilter.php
Quy Dec 29, 2018
c716deb
Update libraries/src/Form/Filter/RulesFilter.php
Quy Dec 29, 2018
6807323
Update libraries/src/Form/Filter/RawFilter.php
Quy Dec 29, 2018
7e608ab
Merge branch '4.0-dev' into j4form
Hackwar Dec 29, 2018
7d5200f
Additional fixes
Hackwar Dec 30, 2018
88e6afa
Merge branch 'j4form' of github.com:Hackwar/joomla-cms into j4form
Hackwar Dec 30, 2018
a601c2e
Merge branch '4.0-dev' into j4form
Hackwar Jan 6, 2019
b0bab7f
Merge branch '4.0-dev' into j4form
Hackwar Jan 8, 2019
54dbd24
Merge branch '4.0-dev' into j4form
Hackwar Jan 9, 2019
6ab420c
Merge branch '4.0-dev' into j4form
wilsonge Jan 9, 2019
57134cf
Removing check for existing field object
Hackwar Jan 10, 2019
495e233
Merge branch '4.0-dev' into j4form
Hackwar Jan 10, 2019
b56e781
Merge branch '4.0-dev' into j4form
wilsonge Jan 10, 2019
2bc1cc5
Merge branch '4.0-dev' into j4form
wilsonge Jan 11, 2019
7d23104
Changing filtering behavior
Hackwar Jan 15, 2019
cbb357e
Removing default value in validation
Hackwar Jan 15, 2019
f008105
Merge branch '4.0-dev' into j4form
wilsonge Jan 17, 2019
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.
+953 −543
Diff settings

Always

Just for now

@@ -13,6 +13,7 @@
use Joomla\CMS\Factory;
use Joomla\CMS\Form\FormField;
use Joomla\CMS\Language\Text;
use Joomla\Registry\Registry;
/**
* Form Field class for the Joomla Platform.
@@ -317,4 +318,67 @@ protected function getLayoutData()
return array_merge($data, $extraData);
}
/**
* Method to filter a field value.
*
* @param mixed $value The optional value to use as the default for the field.
* @param string $group The optional dot-separated form group path on which to find the field.
* @param Registry $input An optional Registry object with the entire data set to filter
* against the entire form.
*
* @return mixed The filtered value.
*
* @since __DEPLOY_VERSION__
*/
public function filter($value, $group = null, Registry $input = null)
{
// Make sure there is a valid SimpleXMLElement.
if (!($this->element instanceof \SimpleXMLElement))
{
throw new \UnexpectedValueException(sprintf('%s::filter `element` is not an instance of SimpleXMLElement', get_class($this)));
}
// Get the field filter type.
$filter = (string) $this->element['filter'];
$return = $value;
switch (strtoupper($filter))
{
// Convert a date to UTC based on the server timezone offset.
case 'SERVER_UTC':
if ((int) $value > 0)
{
// Get the server timezone setting.
$offset = Factory::getConfig()->get('offset');
// Return an SQL formatted datetime string in UTC.
$return = Factory::getDate($value, $offset)->toSql();
}
else
{
$return = '';
}
break;
// Convert a date to UTC based on the user timezone offset.
case 'USER_UTC':
if ((int) $value > 0)
{
// Get the user timezone setting defaulting to the server timezone setting.
$offset = Factory::getUser()->getParam('timezone', Factory::getConfig()->get('offset'));
// Return an SQL formatted datetime string in UTC.
$return = Factory::getDate($value, $offset)->toSql();
}
else
{
$return = '';
}
break;
}
return $return;
}
}
@@ -0,0 +1,53 @@
<?php
/**
* Joomla! Content Management System
*
* @copyright Copyright (C) 2005 - 2018 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
namespace Joomla\CMS\Form\Filter;
defined('JPATH_PLATFORM') or die;
use Joomla\CMS\Form\Form;
use Joomla\CMS\Form\FormFilterInterface;
use Joomla\Registry\Registry;
use Joomla\Utilities\ArrayHelper;
/**
* Form Filter class for integer arrays
*
* @since __DEPLOY_VERSION__
*/
class IntarrayFilter implements FormFilterInterface
{
/**
* Method to filter a field value.
*
* @param \SimpleXMLElement $element The SimpleXMLElement object representing the `<field>` tag for the form field object.
* @param mixed $value The form field value to validate.
* @param string $group The field name group control value. This acts as an array container for the field.
* For example if the field has name="foo" and the group value is set to "bar" then the
* full field name would end up being "bar[foo]".
* @param Registry $input An optional Registry object with the entire data set to validate against the entire form.
* @param Form $form The form object for which the field is being tested.
*
* @return mixed The filtered value.
*
* @since __DEPLOY_VERSION__
*/
public function filter(\SimpleXMLElement $element, $value, $group = null, Registry $input = null, Form $form = null)
{
if (is_object($value))
{
$value = get_object_vars($value);
}
$value = is_array($value) ? $value : array($value);
$value = ArrayHelper::toInteger($value);
return $value;
}
}
@@ -0,0 +1,43 @@
<?php
/**
* Joomla! Content Management System
*
* @copyright Copyright (C) 2005 - 2018 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
namespace Joomla\CMS\Form\Filter;
defined('JPATH_PLATFORM') or die;
use Joomla\CMS\Form\Form;
use Joomla\CMS\Form\FormFilterInterface;
use Joomla\Registry\Registry;
/**
* Form Filter class for raw values
*
* @since __DEPLOY_VERSION__
*/
class RawFilter implements FormFilterInterface
{
/**
* Method to filter a field value.
*
* @param \SimpleXMLElement $element The SimpleXMLElement object representing the `<field>` tag for the form field object.
* @param mixed $value The form field value to validate.
* @param string $group The field name group control value. This acts as an array container for the field.
* For example if the field has name="foo" and the group value is set to "bar" then the
* full field name would end up being "bar[foo]".
* @param Registry $input An optional Registry object with the entire data set to validate against the entire form.
* @param Form $form The form object for which the field is being tested.
*
* @return mixed The filtered value.
*
* @since __DEPLOY_VERSION__
*/
public function filter(\SimpleXMLElement $element, $value, $group = null, Registry $input = null, Form $form = null)
{
return $value;
}
}
@@ -0,0 +1,59 @@
<?php
/**
* Joomla! Content Management System
*
* @copyright Copyright (C) 2005 - 2018 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
namespace Joomla\CMS\Form\Filter;
defined('JPATH_PLATFORM') or die;
use Joomla\CMS\Form\Form;
use Joomla\CMS\Form\FormFilterInterface;
use Joomla\Registry\Registry;
/**
* Form Filter class for rules
*
* @since __DEPLOY_VERSION__
*/
class RulesFilter implements FormFilterInterface
{
/**
* Method to filter a field value.
*
* @param \SimpleXMLElement $element The SimpleXMLElement object representing the `<field>` tag for the form field object.
* @param mixed $value The form field value to validate.
* @param string $group The field name group control value. This acts as an array container for the field.
* For example if the field has name="foo" and the group value is set to "bar" then the
* full field name would end up being "bar[foo]".
* @param Registry $input An optional Registry object with the entire data set to validate against the entire form.
* @param Form $form The form object for which the field is being tested.
*
* @return mixed The filtered value.
*
* @since __DEPLOY_VERSION__
*/
public function filter(\SimpleXMLElement $element, $value, $group = null, Registry $input = null, Form $form = null)
{
$return = array();
foreach ((array) $value as $action => $ids)
{
// Build the rules array.
$return[$action] = array();
foreach ($ids as $id => $p)
{
if ($p !== '')
{
$return[$action][$id] = ($p == '1' || $p == 'true') ? true : false;
}
}
}
return $return;
}
}
@@ -0,0 +1,44 @@
<?php
/**
* Joomla! Content Management System
*
* @copyright Copyright (C) 2005 - 2018 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
namespace Joomla\CMS\Form\Filter;
defined('JPATH_PLATFORM') or die;
use Joomla\CMS\Filter\InputFilter;
use Joomla\CMS\Form\Form;
use Joomla\CMS\Form\FormFilterInterface;
use Joomla\Registry\Registry;
/**
* Form Filter class for safe HTML
*
* @since __DEPLOY_VERSION__
*/
class SafehtmlFilter implements FormFilterInterface
{
/**
* Method to filter a field value.
*
* @param \SimpleXMLElement $element The SimpleXMLElement object representing the `<field>` tag for the form field object.
* @param mixed $value The form field value to validate.
* @param string $group The field name group control value. This acts as an array container for the field.
* For example if the field has name="foo" and the group value is set to "bar" then the
* full field name would end up being "bar[foo]".
* @param Registry $input An optional Registry object with the entire data set to validate against the entire form.
* @param Form $form The form object for which the field is being tested.
*
* @return mixed The filtered value.
*
* @since __DEPLOY_VERSION__
*/
public function filter(\SimpleXMLElement $element, $value, $group = null, Registry $input = null, Form $form = null)
{
return InputFilter::getInstance(null, null, 1, 1)->clean($value, 'html');
}
}
@@ -0,0 +1,120 @@
<?php
/**
* Joomla! Content Management System
*
* @copyright Copyright (C) 2005 - 2018 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
namespace Joomla\CMS\Form\Filter;
defined('JPATH_PLATFORM') or die;
use Joomla\CMS\Form\Form;
use Joomla\CMS\Form\FormFilterInterface;
use Joomla\Registry\Registry;
/**
* Form Filter class for phone numbers
*
* @since __DEPLOY_VERSION__
*/
class TelFilter implements FormFilterInterface
{
/**
* Method to filter a field value.
*
* @param \SimpleXMLElement $element The SimpleXMLElement object representing the `<field>` tag for the form field object.
* @param mixed $value The form field value to validate.
* @param string $group The field name group control value. This acts as an array container for the field.
* For example if the field has name="foo" and the group value is set to "bar" then the
* full field name would end up being "bar[foo]".
* @param Registry $input An optional Registry object with the entire data set to validate against the entire form.
* @param Form $form The form object for which the field is being tested.
*
* @return mixed The filtered value.
*
* @since __DEPLOY_VERSION__
*/
public function filter(\SimpleXMLElement $element, $value, $group = null, Registry $input = null, Form $form = null)
{
$value = trim($value);
// Does it match the NANP pattern?
if (preg_match('/^(?:\+?1[-. ]?)?\(?([2-9][0-8][0-9])\)?[-. ]?([2-9][0-9]{2})[-. ]?([0-9]{4})$/', $value) == 1)
{
$number = (string) preg_replace('/[^\d]/', '', $value);
if (substr($number, 0, 1) == 1)
{
$number = substr($number, 1);
}
if (substr($number, 0, 2) == '+1')
{
$number = substr($number, 2);
}
$result = '1.' . $number;
}
// If not, does it match ITU-T?
elseif (preg_match('/^\+(?:[0-9] ?){6,14}[0-9]$/', $value) == 1)
{
$countrycode = substr($value, 0, strpos($value, ' '));
$countrycode = (string) preg_replace('/[^\d]/', '', $countrycode);
$number = strstr($value, ' ');
$number = (string) preg_replace('/[^\d]/', '', $number);
$result = $countrycode . '.' . $number;
}
// If not, does it match EPP?
elseif (preg_match('/^\+[0-9]{1,3}\.[0-9]{4,14}(?:x.+)?$/', $value) == 1)
{
if (strstr($value, 'x'))
{
$xpos = strpos($value, 'x');
$value = substr($value, 0, $xpos);
}
$result = str_replace('+', '', $value);
}
// Maybe it is already ccc.nnnnnnn?
elseif (preg_match('/[0-9]{1,3}\.[0-9]{4,14}$/', $value) == 1)

This comment has been minimized.

Copy link
@Quy

Quy Dec 27, 2018

Contributor

Add ^?

This comment has been minimized.

Copy link
@Hackwar

Hackwar Dec 28, 2018

Author Member

This has been copied as is from JForm. My Regexp-Foo isn't the best, so I would leave that for another developer. 😄

{
$result = $value;
}
// If not, can we make it a string of digits?
else
{
$value = (string) preg_replace('/[^\d]/', '', $value);
if ($value != null && strlen($value) <= 15)
{
$length = strlen($value);
// If it is fewer than 13 digits assume it is a local number
if ($length <= 12)
{
$result = '.' . $value;
}
else
{
// If it has 13 or more digits let's make a country code.
$cclen = $length - 12;
$result = substr($value, 0, $cclen) . '.' . substr($value, $cclen);
}
}
// If not let's not save anything.
else
{
$result = '';
}
}
return $result;
}
}
Oops, something went wrong.
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.