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

Already on GitHub? Sign in to your account

Fix for regex_match #1514

Closed
wants to merge 3 commits into
from

Conversation

Projects
None yet
2 participants

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

Thanks

henriqueoliveira added some commits Jun 20, 2012

@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

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

regex_match[/my_regex/]

Contributor

narfbg commented Jun 22, 2012

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']);
}

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]

Contributor

narfbg commented Jun 22, 2012

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.

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])$/'
)
);

Contributor

narfbg commented Jun 22, 2012

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

Ok, i think its far enought

@narfbg narfbg closed this Nov 5, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment