Skip to content

Commit 63080c2

Browse files
committed
✨ add apply() method to apply filters on form fields
Signed-off-by: otengkwame <developerkwame@gmail.com>
1 parent 216dfbb commit 63080c2

1 file changed

Lines changed: 45 additions & 1 deletion

File tree

CodeIgniter/Framework/libraries/Form_validation.php

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -667,7 +667,7 @@ public function run($group = '')
667667
*
668668
* This function does all the work.
669669
*
670-
* @deprecated 2.1.1 use true() instead Alias to the run() method
670+
* @deprecated 2.1.1 use true() instead, alias to the run() method
671671
* @param string $group
672672
* @return bool
673673
*/
@@ -691,6 +691,50 @@ public function true($group = '')
691691

692692
// --------------------------------------------------------------------
693693

694+
/**
695+
* Applies filters on fields
696+
*
697+
* This method lets you apply functions to form fields
698+
* so that it can be returned back as an array
699+
* e.g ucfirst, trim etc
700+
*
701+
* @param string|array $functions
702+
* @param string|array|null $fields
703+
* @param array $data
704+
* @return array
705+
*/
706+
public function apply($functions, $fields = null, $data = [])
707+
{
708+
709+
$data = clean($data) ?: clean($this->CI->load->input->post());
710+
711+
if (is_null($fields)) {
712+
$fields = array_keys($data); // Use all available fields
713+
}
714+
715+
if (!is_array($functions)) {
716+
$functions = explode('|', str_replace(',', '|', $functions));
717+
}
718+
719+
if (!is_array($fields)) {
720+
$fields = explode('|', str_replace(',', '|', $fields));
721+
}
722+
723+
foreach ($fields as $field) {
724+
if (isset($data[$field])) {
725+
foreach ($functions as $function) {
726+
if (function_exists($function)) {
727+
$data[$field] = $function($data[$field]);
728+
}
729+
}
730+
}
731+
}
732+
733+
return $data;
734+
}
735+
736+
// --------------------------------------------------------------------
737+
694738
/**
695739
* Prepare rules
696740
*

0 commit comments

Comments
 (0)