• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Validation v/s Form_Validation

#1
[eluser]Yash[/eluser]
In old validation
Code:
$this->input->post('name');
$_POST['name'];
$this->validation->name;

In Form validation
Code:
$this->input->post('name');   //works
$_POST['name'];      //works
$this->form_validation->name;  //not working?

This is not a Qs really just curious why not we can't access field like this.

#2
[eluser]Yash[/eluser]
anyone ... any comments?

#3
[eluser]pistolPete[/eluser]
Just compare the two classes:
In Validation.php each form field is saved as a private class variable.

In Form_validation.php the form fields are saved in a private array called $_field_data.

#4
[eluser]Yash[/eluser]
ok I can see that

so how can I access
Code:
$this->form_validation->name;
??

something like this?
Code:
$this->form_validation->array['name'];

#5
[eluser]pistolPete[/eluser]
Why don't you just use the form helpers? http://ellislab.com/codeigniter/user-gui...elper.html

set_value(), set_select(), etc.

#6
[eluser]Yash[/eluser]
I know that as well.

I want give value from controller with a initial value. Like we want to do in edit case.

also when we are using callback function(s) if we need to access 2 fields then $this->validation->name would be very handy..any alternate to this?

Code:
// Check username/email for uniqueness
    function _check_username($username)
    {
        $email = $this->validation->member_email;
        $query = $this->db->getwhere('board_members', array('member_name'=>$username), 1, 0);
        if ($query->num_rows() != 0)
        {
            $this->validation->set_message('_check_username', 'This username has already been registered');
            return FALSE;
        }
        elseif($email!="")
        {
            $query = $this->db->getwhere('board_members', array('member_email'=>$email), 1, 0);
            if ($query->num_rows() != 0)
            {
                $this->validation->set_message('_check_username', 'This email address has already been registered');
                return FALSE;
            }
        }
        return TRUE;
    }

#7
[eluser]LuckyFella73[/eluser]
You can set an initial value using the form_validation like this:

Code:
<?php
# controller:
if ($query->num_rows() > 0)
{
    foreach ($query->result() as $row)
    {
        # whatever you want to edit from your db ..
        $data['title'] = $row->title;
        $data['date'] = $row->date;
        $data['text'] = $row->text;
    }
}

# view file:
?>
<input type="text" name="title" id="title" class="default" value="<?php echo set_value('title',$title); ?>"/>
<input type="text" name="date" id="date" class="default" value="<?php echo set_value('date',$date); ?>"/>
<input type="text" name="text" id="text" class="default" value="<?php echo set_value('text',$text); ?>"/>

In your controller you just need to check if your edit function is called
from your form - then set the $data['abcd'] part via your POST data instead
of db values.

Your callback function is only needed after submitting the form, so you can
use (for example):
Code:
$this->input->post('title')
to get the values.

Cheers

#8
[eluser]Phil Sturgeon[/eluser]
Wow, I really need to convert PyroCMS to use the new form validation library, the syntax and helper functions cut out A LOT of extra code!

#9
[eluser]Yash[/eluser]
@LuckyFella73

You are setting 2 times same value

what I'm saying that I set value once. I know what you're suggesting me. I'm currently using this.

anyone...

#10
[eluser]LuckyFella73[/eluser]
[quote author="Yash" date="1251385063"]@LuckyFella73
You are setting 2 times same value
[/quote]

Where do set the same value?

I just posted a bit code where the controller set the
initial value comming from database. After submitting
your form the "value-parameter" of your form element
has to be set using the post data.

In your controller/ edit function you need something like that:
Code:
<?php

# Controller - very simplified ..

function edit()
{
    if ( isset($_POST['form']) AND $_POST['form'] == 'form_edit' ) // the form has beend send
    {
        $data['title'] = $this->input->post('title');
        $data['date'] = $this->input->post('date');
        $data['text'] =$this->input->post('text');

        $this->form_validation->set_rules('title', 'Title', 'trim');
        $this->form_validation->set_rules('date', 'Date', 'trim');
        $this->form_validation->set_rules('text', 'Text', 'trim');

        if ($this->form_validation->run() == FALSE)
        {
            // in case validation fails, set values of form elements by using the POST values
            $this->load->view('news/view_news_edit.php', $data); // dummy filenames
        }
        else
        {
            // do your db stuff

            // call success page or whatever
        }
    }
    else // set initioal values from db:
    {
        # your query here

        if ($query->num_rows() > 0)
        {
            foreach ($query->result() as $row)
            {
                # whatever you want to edit from your db ..
                $data['title'] = $row->title;
                $data['date'] = $row->date;
                $data['text'] = $row->text;
            }
        }
        
        $this->load->view('news/view_news_edit.php', $data);
    }
}
?>

There are two parts in the code that set a form value but thats the
way you have to go if you want to edit data from db using a form because
the source of the values are different.

Sorry if I get you wrong completely


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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