• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
MY_Form_validation doesn’t work

#1
[eluser]crazytereza[/eluser]
I create "MY_Form_validation" library with additional rules. I wrote "MY_" like a prefix in config.php. And load "Form_validation" library in autoload.php.
As I know if you have prefix in config file then it’s being downloaded "MY_Form_validation" instead of "Form_validation" even if u load “Form_validation” in autoload.php.

Here is my constructor for MY_Form_validation:
function __construct() {
parent::CI_Form_validation();
$CI=&get;_instance();
$CI->lang->load('validation_new'); // Information about new mistakes-'validation_new'
echo "Constructor works!!!";
}

After checking I cant see "Constructor works!!!". So, it means constructor doesn’t work and the library “MY_Form_validation" with additional rules doesn’t work either. “Form_validation” library only works.

Maybe someone know where is a problem or the mistake.
Thanks.

P.S. Sorry if my English is bad. I posted it on the Russian codeigniter website but noone knows the answer.

#2
[eluser]bretticus[/eluser]
Please show your:

1. Your application/config/autoload.php line of code where you auto-load form_validation.
2. Your MY_Form_validation class in it's entirety.
3. Your controller/model method where you reference form_validation.

Thanks

#3
[eluser]crazytereza[/eluser]
1. Here is my autoload file:

$autoload['libraries'] = array('database','session','lib_view','lib_mng','form_validation');

2. MY_Form_validation class:

<?php
/** Расширение класса валидации форм
* @author
* @copyright 2010
*/
if (!defined ('BASEPATH')) exit ('No direct script access allowed');

class MY_Form_Validation extends CI_Form_validation{

function __construct() {
parent::CI_Form_validation();
$CI=&get;_instance();
$CI->lang->load('validation_new'); // Information about new mistakes-‘validation_new’
echo "Constructor works!!! ";
}
/**
* Функция проверки на наличие маленьких букв и цифр
*/
function az_numeric($str){
return (!preg_match("/^([a-z0-9])+$/i",$str)) ? FALSE : TRUE;
}
/**
* Валидация URL
*/
function valid_url(){
return (!preg_match('/(http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:;.?+=&%@!\-\/]))?/',$str)) ? FALSE : TRUE;
}
/**
* Валидное название
*/
function valid_title($str){
return(!preg_match('/^[А-Яа-яЁё\w\d\s\.\,\+\-\_\!\?\#\%\@\№\/\(\)\[\]\:
&\$\*]{1,250}$/',$str)) ? FALSE : TRUE;
}
/**
* Проверка на уникальность
*/
function uniq ($str,$param){
//Объект CI
$CI=&get;_instance();
//Имя таблицы
$tname=str_replace('_id','s',$param);
$CI->db->where($param,$str);
return ($CI->db->count_all_results($tname)==0);
}
}
?>

3. Reference form_validation in model:

class Crud extends Model{
var $table='';//table name
var $idkey='id';//Key ID
var $add_rules=array();//Validation rules for adding
var $edit_rules=array();//Validation rules for editing
//Construstor
function Crud(){
parent::Model();

}

//Добавление
function add(){
$this->form_validation->set_rules($this->add_rules);
if ($this->form_validation->run()){
$data=array();
foreach($this->add_rules as $one){
$f=$one['field'];
$data[$f]=$this->input->post($f);
}

$this->db->insert($this->table,$data);
return $this->db->insert_id();
}
else{
return FALSE;}

}


class mdl_link extends Crud{
var$table='links';//table name
var $idkey='link_id';
var $add_rules=array(
array(
'field'=>'link_id',
'label'=>'ID ссылки',
'rules'=>'az_numeric|required|uniq[link_id]',
),
array(
'field'=>'descr',
'label'=>'Описание',
'rules'=>'required|valid_title',
),
array(
'field'=>'url',
'label'=>'URL',
'rules'=>'required|valid_url',
),
);

}


In controller then I use mdl_link model

Thank you

#4
[eluser]Thorpe Obazee[/eluser]
[quote author="crazytereza" date="1285785571"]
function __construct() {
parent::CI_Form_validation();
$CI=&get;_instance();
$CI->lang->load('validation_new'); // Information about new mistakes-'validation_new'
echo "Constructor works!!!";
}
[/quote]

You don't need the constructor. Try autoloading the language file.

#5
[eluser]crazytereza[/eluser]
Just tried to autoload my language file. But it's still the same - only 'form_validation' rules work =(
And even i can autoload that language file why the constructor doesnt work. It must show my echo text. But it doesn't. Can't understand where is a problem.

#6
[eluser]Thorpe Obazee[/eluser]
Code:
$CI=&get;_instance();
Could it me that you have a typo here?

#7
[eluser]bretticus[/eluser]
Sorry, haven't looked it this for a few days. Thanks for posting your code.

I think this may be a class naming issue. Note the difference in caps:

Code:
class MY_Form_Validation extends CI_Form_validation{

Try changing the V in My_Form_Validation to lowercase:

Code:
class MY_Form_validation extends CI_Form_validation{

Off the top of my head, I'm not sure whether case determines whether a MY_ version of the core class exists. It's an easy change to try though.

#8
[eluser]crazytereza[/eluser]
[quote author="bargainph" date="1285916852"]
Code:
$CI=&get;_instance();
Could it me that you have a typo here?[/quote]

sorry. no i don't. it's just i put it somehow when was posting here.

[quote author="bretticus" date="1285918560"]Sorry, haven't looked it this for a few days. Thanks for posting your code.

I think this may be a class naming issue. Note the difference in caps:

Code:
class MY_Form_Validation extends CI_Form_validation{

Try changing the V in My_Form_Validation to lowercase:

Code:
class MY_Form_validation extends CI_Form_validation{

Off the top of my head, I'm not sure whether case determines whether a MY_ version of the core class exists. It's an easy change to try though.[/quote]

Thanks for your advise. I tried but it's still the same. Maybe i need to find another way to do my validation=(


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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