• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
My Form validation

#1
[eluser]Unknown[/eluser]
Hello I want to create my form validation.

This is libraries/my_form_validation.php:
Code:
<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class My_Form_validation extends CI_Form_validation {

    function __construct() {
        parent::__construct();
        $CI = & get_instance();
    }

    // --------------------------------------------------------------------

    /**
     */
    function validation_text($str) {

        $CI->form_validation->set_message('validation_text', 'Incorrect Cyrillic text.');

        return (preg_match("/^[a-zA-Z\p{Cyrillic}0-9\s\-\.\!\?]+$/u", $str)) ? TRUE : FALSE;
    }

}

And use controler method
Code:
$this->load->library('form_validation');
        $this->load->library('my_form_validation');
$this->form_validation->set_rules('newstitle', 'News title', 'trim|required|min_length[10]|validation_text[newstitle]');
if ($this->form_validation->run()) { .........

My form valdiation is not work.

I test preg_match in test.php(outside ci) and add some text and work, no problem
but in codeigniter not receive

Where am I wrong?

Thank you

#2
[eluser]Unknown[/eluser]
The only thing I see would be that you are calling set_rules from the main CI validation library and not your custom one. It should probably look like this:
Code:
$this->my_form_validation->set_rules('newstitle', 'News title', 'trim|required|min_length[10]|validation_text[newstitle]');

#3
[eluser]CroNiX[/eluser]
In validation_text(), it doesn't know what $CI is...$CI should be $this->CI.

Additionally, CI always first looks for a MY_whatever, so there is no need to manually load it. Just load form_validation and it will automatically load MY_Form_validation.

#4
[eluser]moth[/eluser]
I've been having much the same problem, except that instead of declaring my validation rules directly in my Controller methods, I've moved them to a config file in /application/config/form_validation.php

Is there a gotcha in combining MY_Form_validation with Config-based rules?

I just can't seem to get it to work.

#5
[eluser]InsiteFX[/eluser]
it should be saved as ./application/libraries/MY_Form_validation.php

#6
[eluser]moth[/eluser]
It is.

I have;

/application/config/form_validation.php
(as described here: http://ellislab.com/codeigniter/user-gui...ngtoconfig)

..and
/application/libraries/MY_Form_validation.php

--

/application/libraries/MY_Form_validation.php

Code:
class MY_Form_validation extends CI_Form_validation {

    function __construct() {

        parent::__construct();

        log_message('debug', 'MY_Form_validation is loaded');
    }
    
    function is_valid_url($url) {

       if(preg_match("/^http(|s):\/{2}(.*)\.([a-z]){2,}(|\/)(.*)$/i", $url)) {

              if(filter_var($url, FILTER_VALIDATE_URL)) {
                  return true;
              }

          }
          $this->CI->form_validation->set_message('is_valid_url', 'The %s must be a valid URL');
          return false;

    }
}


/application/config/form_validation.php

Code:
$config = array(
                 'links' => array(
                                    array(
                                            'field' => 'url',
                                            'label' => 'URL',
                                            'rules' => 'required|min_length[5]|prep_url|is_valid_url'
                                         ),
                                    array(
                                            'field' => 'title',
                                            'label' => 'Title',
                                            'rules' => 'required|min_length[4]|max_length[255]'
                                         ),
                                    array(
                                            'field' => 'description',
                                            'label' => 'Description',
                                            'rules' => 'required|min_length[4]|max_length[255]'
                                         )
                                    )                  
               );

#7
[eluser]moth[/eluser]
MY_Form_validation Loads - I see my debug message, I just don't get any validation...

#8
[eluser]moth[/eluser]
Ok, it looks like the rules weren't getting passed.

I tried this, and I seem to be fixed;

http://ellislab.com/forums/viewthread/181888/#863106

Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class MY_Form_validation extends CI_Form_validation {

    function __construct($config = array())
    {
        parent::__construct($config);
    }  
}
?>

#9
[eluser]CroNiX[/eluser]
One other thing:
Code:
$this->load->library('form_validation');
$this->load->library('my_form_validation');
You don't manually load "my_form_validation", only the regular form_validation. IF there is a MY_Form_validation, CI will automatically load it (if it's in the correct location).


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.