Welcome Guest, Not a member yet? Register   Sign In
Custom form validation rule
#6

(This post was last modified: 09-05-2015, 10:24 AM by jLinux. Edit Reason: PHP code inserted )

Im going to use something like table.column:existing_value instead of  table.column|existing_value, that should work.

Quick regex question. Anyone know how to use preg_match to match for both of those? And just check if the third matched value is there? Im having trouble with it, im not a Regex ninja..

PHP Code:
<?php
$s 
= [
 
   'table.column',
 
   'table.column:value'
 
   ];

//$pattern = '/^(\S+)\.(\S+)\:(\S+)?$/';
$pattern '/^(\S+)\.(\S+)$/';

foreach(
$s as $str){
 
   if(preg_match($pattern$str$matches))
 
   {
 
       echo "Successfuly matched string: {$str}\n";
 
       print_r($matches);
 
   }
 
   else
    
{
 
       echo "No matches for string: {$str}\n";
 
   }
 
   echo "==============\n";


Result:
Quote:Successfuly matched string: table.column

Array
(
    [0] => table.column
    [1] => table
    [2] => column
)
==============
Successfuly matched string: table.column:value
Array
(
    [0] => table.column:value
    [1] => table
    [2] => column:value
)
==============

What im looking for is:
Quote:Successfuly matched string: table.column
Array
(
   [0] => table.column
   [1] => table
   [2] => column
)
==============
Successfuly matched string: table.column:value
Array
(
   [0] => table.column:value
   [1] => table
   [2] => column
   [2] => value
)
==============

I know I can use the question mark to match for the last string or not, but im having issues including the : in that condition..

What I have now works, its just too complicated when REGEX can get rid of some code..
PHP Code:
<?php defined('BASEPATH') OR exit('No direct script access allowed');

class 
MY_Form_validation extends CI_Form_validation {

 
   public function is_unique($str$field)
 
   {
 
       // No : found, use the default is_unique
 
       if(strpos($field':') === FALSE)
 
       {
 
           return parent::is_unique($str$field);
 
       }

 
       // If we can match for a table.column:value, assume were editing
 
       ifpreg_match('/^(\S+)\.(\S+)\:(\S+)?$/'$field$matches) !== FALSE )
 
       {
 
           $table $matches[1];
 
           $field $matches[2];
 
           $ignore $matches[3];

 
           $this->CI->db
                
->select('*')
 
               ->from($table)
 
               ->where($field$str);

 
           if( ! is_null($ignore))
 
               $this->CI->db->where_not($field$ignore);

 
           return $this->CI->db->num_rows() === 0;
 
       }

 
       // If : was found but regex not matched..
        
Logger_lib::error("Failed to match 'table.column' as well as 'table.column:value' for is_unique form validation check"TRUE);
 
       return FALSE;
 
   }



Thanks
Reply


Messages In This Thread
Custom form validation rule - by jLinux - 08-29-2015, 11:20 AM
RE: Custom form validation rule - by jLinux - 08-30-2015, 09:00 AM
RE: Custom form validation rule - by CroNiX - 08-30-2015, 09:02 AM
RE: Custom form validation rule - by jLinux - 08-30-2015, 10:14 AM
RE: Custom form validation rule - by mwhitney - 08-31-2015, 09:35 AM
RE: Custom form validation rule - by jLinux - 09-05-2015, 10:07 AM
RE: Custom form validation rule - by mwhitney - 09-08-2015, 07:17 AM



Theme © iAndrew 2016 - Forum software by © MyBB