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

Im trying to overwrite the is_unique rule, and make it so you can either use as it is, or do something like: is_unique[table.field|value_to_ignore]
Example:
PHP Code:
$this->form_validation->set_rules('Page_Title''page_title'"is_unique[pages.title|{$current_title}]"); 

This way it can be used to check for unique values, but if its the current value, it wont say its an existing value.

The problem though, is it doesnt seem to be working.. The error that gets returned from the validation is: Unable to access an error message corresponding to your field name Page Title.(is_unique[pages.title)

Heres my code thus far.

MY_Form_validation.php
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)
    {
        if( ! isset(
$this->CI->db))
            return 
FALSE;

        if( 
preg_match('/^(.*)\.(.*)\|(.*)$/'$field$matches) !== FALSE )
        {
            
$table $matches[1];
            
$field $matches[2];
            
$ignore $matches[3];
        }
        elseif( 
preg_match('/^(.*)\.(.*)$/'$field$matches) !== FALSE )
        {
            
$table $matches[1];
            
$field $matches[2];
            
$ignore NULL;
        }
        else
        {
            return 
FALSE;
        }

        
$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;
    }


form_validation_lang.php
PHP Code:
<?php
$lang
['form_validation_is_unique'] = 'not unique'

Controller Validation Snippet
PHP Code:
...
$this->form_validation->set_rules(
    
'page_title''Page Title'"required|alpha_numeric_spaces_dash|min_length[5]|max_length[50]|is_unique[pages.title|{$data['settings']->title}]",
    array(
        
'required'  => 'Need to specify a unique page title',
        
'is_unique' => 'The page title specified already exists for another page'
    
)
);
... 

Im guessing that theres something that checks the regex of the rule string before it passes it to the form validation rules...
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