Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Fix for regex_match #1514

Closed
wants to merge 3 commits into from

2 participants

@henriqueoliveira

Hi, now i took a different approach, and tested a lot, i think its working good

Thanks

henriqueoliveira added some commits
@henriqueoliveira henriqueoliveira Fixed the problem when using form validation regex_match with the Pip…
…e char "|"

The Problem: When you have a regex with pipe char, the old explode function split your regex without your consent
Now its exploding correctly.
d33ee5d
@henriqueoliveira henriqueoliveira Possible fix for regex_match validation
1dc7523
@henriqueoliveira henriqueoliveira Fix duplicated code
3af2ef4
@henriqueoliveira

i forgot to tell
The regex_match call has to be called with slashes like preg_match

regex_match[/my_regex/]

@narfbg
Owner

Here's something based on your idea, that should perform better, not have the slash limitation and only executes if the regular expression does have a pipe in it:

// Handle regex_match[] rules containing pipes
if (preg_match('/\|?(regex_match\[[^\]\|]+\|[^\]]+\])\|?/i', $row['rules'], $match))
{
        // Avoid merging rule names if regex_match is not the first or last rule
        if ($match[0][0] === substr($match[0], -1, 1))
        {
                $match[0] = substr($match[0], 1);
        }

        $exploded_rules = explode('|', str_replace($match[0], '', $row['rules']));
        $exploded_rules[] = $match[1];
}
else
{
        $exploded_rules = explode('|', $row['rules']);
}
@henriqueoliveira

Hii, thanks for the tip, i was trying to make it work without slashes, and didnt find a way, but with your code it does not work with the pipe

heres an example

required|strtolower|valid_email|regex_match[/^([01][0-9])|(2[0-3]):([0-5][0-9])$/]|greater_than[5]|is_natural|match[form]

@narfbg
Owner

It does work with a pipe, it doesn't seem to work with square brackets though ... There will always be some kind of a limitation, that's why I wasn't really keen on this from the start.

@henriqueoliveira

what do you think about create a new position and function, like for example, regex_rule

$config['agenda_period'] = array(
array(
'field' => 'period[day_period_id]',
'label' => 'Período',
'rules' => 'required|is_natural'
),
array(
'field' => 'period[start_time]',
'label' => 'Hora início',
'rules' => 'required',
'regex_rule' => '/^([01][0-9])|(2[0-3]):([0-5][0-9])$/'
)
);

@narfbg
Owner

I'm thinking the built-in rules are supposed to be basic and anything more complex should be implemented as a callback.

@henriqueoliveira

Ok, i think its far enought

@narfbg narfbg closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jun 20, 2012
  1. @henriqueoliveira

    Fixed the problem when using form validation regex_match with the Pip…

    henriqueoliveira authored
    …e char "|"
    
    The Problem: When you have a regex with pipe char, the old explode function split your regex without your consent
    Now its exploding correctly.
  2. @henriqueoliveira
  3. @henriqueoliveira

    Fix duplicated code

    henriqueoliveira authored
This page is out of date. Refresh to see the latest.
Showing with 13 additions and 2 deletions.
  1. +13 −2 system/libraries/Form_validation.php
View
15 system/libraries/Form_validation.php
@@ -459,8 +459,19 @@ public function run($group = '')
{
$this->_field_data[$field]['postdata'] = $validation_array[$field];
}
-
- $this->_execute($row, explode('|', $row['rules']), $this->_field_data[$field]['postdata']);
+
+ // This block will threat regex_match in a different way, then others callbacks will continue
+ // working normally
+ $rules = $row['rules'];
+ $regex_match_str = preg_replace("/.*(regex_match\[\/.*\/\]).*/", "$1", $rules, 1, $count);
+ if($count === 0)
+ {
+ $regex_match_str = '';
+ }
+ $rules = str_replace($regex_match_str, "", $rules);
+ $exploded_rules = explode('|', $rules);
+ $exploded_rules[] = $regex_match_str;
+ $this->_execute($row, array_filter($exploded_rules), $this->_field_data[$field]['postdata']);
}
// Did we end up with any errors?
Something went wrong with that request. Please try again.