Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

regex_match in CodeIgniter form_validation generates: Message: preg_match(): No ending delimiter '/' found

I've been looking in other similar posts and the problem seemed to be an unescaped slash. However I'm escaping them.

This is how the string should look:

23/12/2012

and this is how I'm declaring the validation rule:

regex_match[/^(0[1-9]|[12][0-9]|3[01])\/(0[1-9]|1[012])\/(19|20)[0-9]{2}$/]

The ending delimiter is there, and the two inbetween slashes for the date are being escaped with a backslash. I've also tried this which is slightly different, but I get the same error:

regex_match[/^(0[1-9]|[12][0-9]|3[01])\/(0[1-9]|1[012])\/(19|20)\d\d$/]

Where's the error?

EDIT:

Following your advice, I've tried using a callback function. This is the declaration, which is located within the controller class in which the form validation is being executed:

function mach_date($date) {
   /* DEBUG */ echo 'Here I am!'; exit; // execution should stop here displaying the echo
   return (bool)preg_match('/^(0[1-9]|[12][0-9]|3[01])\/(0[1-9]|1[012])\/(19|20)\d\d$/', $date);
}

Validation rules in application/config/form_validation.php:

$config = array(
     // other validation groups.....,
     'articles' => array(
          // other validated fields.....,
          array(
                'field' => 'date_p',
                'label' => 'Publishing date',
                'rules' => 'callback_match_date'
          )
     )
); 
like image 631
Luis Martin Avatar asked Dec 10 '12 16:12

Luis Martin


1 Answers

When you set the validation rules you separate them with a | so the |'s in your regex is causing the validation rule to split at those and that is causing the error. Discussion here on the issue. It seems it's a limitation or bug in codeigniter. You can test it out by running a regex with and without |'s and see if the usage of pipes will cause an error. If that is the case then you may have to validate by regex by other means, maybe use a callback function as detailed on this page where your function will do a preg_match using the regex which needs to be inside the function of course and then return true/false.

like image 169
cryptic ツ Avatar answered Nov 04 '22 09:11

cryptic ツ