Welcome Guest, Not a member yet? Register   Sign In
validation dunamically created fields
#1

[eluser]dimis[/eluser]
I have this function for validation of a form
Code:
function myvalidproduct()
    {
        //   $lancode=array();
        $lancode=$this->lans->returnonlycode();
        $this->load->library('validation');

        $fields['code']= "code";
        //  $fields['active']= "active";
        //  $fields['material']= "material";
        foreach ($lancode as $lan)
        {
            $el="name_".$lan['lan_code'];
            $fields[$el]= "$el";
            $rules[$el]= "required";

        }
        $rules['code']    = "required";
        //   $rules['active']    = "active";
        //  $rules['material']    = "material";
        $this->validation->set_fields($fields);
        $this->validation->set_rules($rules);
    }
... $this-> myvalidproduct();

But the code
Code:
foreach ($lancode as $lan)
        {
            $el="name_".$lan['lan_code'];
            $fields[$el]= "$el";
            $rules[$el]= "required";

        }
does not work. I have some text inputs with name name_gr, name_en,name_it etc that have last 2 letters from database.
HOw can I fix it and validtaete thiw inputs
#2

[eluser]Weblizard[/eluser]
Remove
Code:
$lancode=array();
from myvalidproduct function
#3

[eluser]dimis[/eluser]
I have this code
Code:
function add()
    {
      
        $name=array();
        $lancode=$this->lans->returnonlycode($lancode);

        // echo ($lancode[1]['lan_code']);
        $this-> myvalidproduct($lancode);
        if ($this->validation->run() == FALSE)
        { echo  $this->validation->error_string;

        }.......
Code:
function myvalidproduct($lancode)
    {      
    
        $this->load->library('validation');

        $fields['code']= "code";
        //  $fields['active']= "active";
        //  $fields['material']= "material";
        foreach ($lancode as $lan)
        {
            $el="name_".$lan['lan_code'];
            $fields["$el"]= "$el";
            $rules["$el"]= "required";

        }
        $rules['code']    = "required";
        //   $rules['active']    = "active";
        //  $rules['material']    = "material";
        $this->validation->set_fields($fields);
        $this->validation->set_rules($rules);
    }
Code:
function returnonlycode()
    {
        $data = array();
        $this->db->select('lan_code');
        $query= $this->db->get('lan');
        if ( $query-> num_rows() > 0){
            foreach ( $query-> result_array() as $row){
                $data[] = $row;
            }
        }
        $query-> free_result();

        return $data;
    }
I make this validation with ajax, when I remove the code for the array the validation works also with the code for the array the validation does not work!
#4

[eluser]dimis[/eluser]
I thing I solve it.
Dimis
#5

[eluser]xwero[/eluser]
You are making it to complex i think
Code:
$lancode=$this->lans->returnonlycode($lancode);
The $lancode as parameter doesn't exist so it has no value and your returnonlycode code has no parameters so there is no need to add it.

Code:
function returnonlycode()
    {
        $data = array();
        $this->db->select('lan_code');
        $query= $this->db->get('lan');
        if ( $query-> num_rows() > 0){
            foreach ( $query-> result_array() as $row){
                $data[] = $row;
            }
        }
        $query-> free_result();

        return $data;
    }
The result_array method returns an empty array if nothing is found so
Code:
function returnonlycode()
    {
        $this->db->select('lan_code');
        $query= $this->db->get('lan');
        return $query->result_array();
   }
will do.

Why are you putting the rules in another function?

For the rules not being added you could check if with print_r($rules) to check if the rules are added. If you keep the function you can check with print_r($this->validation->_rules) if the rules in your function are attached to the validation class and are the ones that are going to be checked.
#6

[eluser]dimis[/eluser]
Quote:Why are you putting the rules in another function?
I do it just for some separation of code, now is works.
How can I make a loop with the error messages?
Thank you
Dimis




Theme © iAndrew 2016 - Forum software by © MyBB