Problems, need help? Have a tip or advice? Post it here.
7 posts Page 1 of 1
Hi, I wrote a regex and implemented it into the editable:

Code: Select all
<cms:editable name='count' label='Count' desc='must be number that is or within range of 0-24' type='text' required='1' validator='regex=/^([0-9]|1\d|20|2[1-4])$/' separator='#' validator_msg='regex=Number must be between 0 and 24' />


Since my regex contained the pipe character, I changed the separator to # as per the document.

- the pipe character appears within the regular expression itself, thus that validator cannot be combined with any other using a pipe.


The regex wasn't working so I did further research and found the function that handles the regex in functions.php on line 2584:

Code: Select all
static function validate_regex( $field, $args ){
   if( !preg_match(trim($args), trim($field->get_data())) ){
      return KFuncs::raise_error( "Does not match pattern" );
   }
}


To test further, I recreated that function within <cms:php> on my front-end code to test the regex:

Code: Select all
<cms:php>
   $args = "/^([0-9]|1\d|20|2[1-4])$/"; // validates a number that is or within range of 0-24
   $field = "24"; // changing value to number that is or between 0 and 24 results in a match.

   if( !preg_match(trim($args), trim($field)) ){
      echo "no match";
   } else {
      echo "match";
   }
</cms:php>


Using the <cms:php> test and changing the $field to a number that is 0-24 or within results in a match and works as expected, but I can't get the editable to properly validate the regex. Any help appreciated, thank you.
Hi,

I copy/pasted your editable region's definition to recreate it on my end - as far as I could test, the regex seems to be working just fine.
If possible, please try deleting the region and recreating it.
Hi KK, thanks for replying. I removed the editable from the template, revisited as admin, deleted the field via admin portal then re-added the editable to the template and revisited as super admin and had same results. Then I tried to put the editable in another template that has not seen it yet, same results.

When I try to enter one of the numbers regex validates for in the add new cloned page, upon saving I get the validator_msg error "Number must be between 0 and 24".

Interesting that your test worked, not sure where to go from here, any ideas?
That is strange. I tested again and is working for me.
Is your site online? If you wish, please give me FTP+Couh access to it and I'll take a look at the issue.
Working on localhost MAMP PHP 7.2, but I checked my PHP error log and I found:

Code: Select all
PHP Warning:  preg_match(): No ending delimiter '/' found in /Users/blutbaden/site/couch/functions.php


I am on the newest couch from Github, did an upgrade before testing again today and still have the same results. I am solely testing the validation from the Couch admin add page.
Ok, so since I cannot see for myself the issue I'll suggest a workaround instead -
Paste the following function in your couch/addons/kfunctions.php. It defines a custom validator.named 'my_validate_regex'.
Code: Select all
function my_validate_regex( $field ){
    $re = "/^([0-9]|1\d|20|2[1-4])$/"; // validates a number that is or within range of 0-24

    if( !preg_match(trim($re), trim($field->get_data())) ){
        return KFuncs::raise_error( "Number must be between 0 and 24" );
    }
}

Modify your editable region's definition yo make it use the custom validator as follows -
Code: Select all
<cms:editable name='count' label='Count' desc='must be number that is or within range of 0-24' type='text' required='1' validator='my_validate_regex' separator='#' />

Hope this helps.
hi kk, that worked perfectly nice trick! ty
7 posts Page 1 of 1
cron