⚠ In case you've missed it, we have migrated to our new website, with a brand new forum. For more details about the migration you can read our blog post for website migration. This is an archived forum. ⚠

  •     

profile picture

custom message when use set_rules



hunter205
  • profile picture
  • Member

Posted 09 June 2019 - 08:43 AM

I tried to validate input time as hh:ss by regex, but it didn't work. can anyone show me my mistake????? it seems callback function in set_rules() didn't work.
 
set_rule() as follow:
 /* VALIDATE START TIME - END TIME in hh:ss format */
        /* /^([01]?[0-9]|2[0-3]):[0-5][0-9]$/ */
        $crud->set_rules('startTime', 'Begin Time', 'callback_test_check(' . $this->input->post('startTime') . ')');

and this is callback function:

function test_check($str) {
        if (!preg_match('/^([01]?[0-9]|2[0-3]):[0-5][0-9]$/', $str)) {   
            $this->form_validation->set_message('test_check', 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA.');
            return false;
        }
        return true;
    }

And it's always throw this message although i tried many times.

Unable to access an error message corresponding to your field name Begin Time.(test_check(22:22))

hunter205
  • profile picture
  • Member

Posted 09 June 2019 - 12:56 PM

oh i found it. i did wrong syntax @@  this is worked 

/* VALIDATE START TIME - END TIME in hh:ss format */
/* /^([01]?[0-9]|2[0-3]):[0-5][0-9]$/ */
$crud->set_rules('startTime', 'Begin Time', 'callback_validate_time_format');

and 

function validate_time_format($str) {
        if (!preg_match('/^([01]?[0-9]|2[0-3]):[0-5][0-9]$/', $str)) {
            $this->form_validation->set_message('validate_time_format', 'The {field} is not in the correct format (hh:ss).');
            return false;
        }
        return true;
    }

however, i don't know why following code "regex_match" of grocery, does't work... so i need to do more as above

$crud->set_rules('startTime', 'Begin Time', 'regex_match[/^([01]?[0-9]|2[0-3]):[0-5][0-9]$/]');