Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Validation Class match_pattern error.
  • The following rule:
    match_pattern[^([123]0|[012][1-9]|31)(/)(0[1-9]|1[012])(/)(19[0-9]{2}|2[0-9]{3})$]
    

    causes the error:
    Warning!
    ErrorException [ Warning ]: preg_match() [function.preg-match]: No ending delimiter '#' found
    COREPATH/classes/validation.php @ line 673: Seeing the following post: http://fuelphp.com/forums/topics/view/5338. I tried adding # as suggested but that didn't work either. I think I've narrowed it down to the way the rules are exploded within the core and that my pattern actually contains '|'. Before I add a issue to the github repo does anyone have any possible solutions?
  • Sorry, I don't have a direct answer to your question. It seems, however, that you are trying to use regex to check for a valid DATE. I have a validation function that I've been using successfully for that, and you are free to use it, and modify it to your needs, if it helps you:
    // PHP function that tries to see if an inputed string is a valid date, 
    // and if it is, returns it in the format yyyy-mm-dd - ready for MySQL.
    
    public function validate_date($date=null)
     {
      //if date is already in format 'yyyy-mm-dd' 
      //then leave it as-is:
      if (preg_match('#^(\d{2}|\d{4})-\d{1,2}-\d{1,2}$#', $date)){ return $date; }
    
      //if date is in format 'dd/mm/yyyy', 
      //or "dd.mm.yyyy" or "dd-mm-yyyy"
      //then convert it to 'yyyy-mm-dd', and return it:
      elseif (preg_match('#^(\d{1,2})[./-](\d{1,2})[./-](\d{2}|\d{4})$#', $date, $matches)) { return $matches[3].'-'.$matches[2].'-'.$matches[1]; }
    
      //if date is in any other format, fail validation:
      else { return false; }
     }
    
  • It must be a full pattern, not just the part between its delimiters. Otherwise you wouldn't be able to pass modifiers. Also I wouldn't use match_pattern with add_field(), add()->add_rule() is a lot safer as add_field() depends on string parsing the part between the square brackets and may trip over any part of the regex (like the actual square brackets in there). To get the rules from the string passed to add_rule() it needs regex, and regexing a regex is asking for trouble.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion