Welcome Guest, Not a member yet? Register   Sign In
Using Validation library to populate a form either with incorrect data or starting data?
#1

[eluser]Nameless One[/eluser]
I usually use a single controller method for adding new entries and editing existing ones in my projects. The User Guide explains how Validation class can be used to validate the submitted data and return it to the form in its starting state, but how can I set the $this->validation->something member variables so that I could populate the form with starting data fetched from the database?

To make my question clearer, here is an example of my code in combination of php and pseudo language in comments:

Code:
function edit($entry_id = 0)
{
    if ($this->input->post('submit'))
    {
        $this->load->library('validation');
        //set validation rules
        if ($this->validation->run())
        {
            //process the data, insert it into database and redirect
        }
        else
        {
            //set form field names for Validation object with set_field
            $error = TRUE;
        }
    }
    //fetch additional data needed to display the form, like select options for example
    if ($entry_id && !$error)
    {
        //fetch the entry that needs to be edited
    }
    //load the form view and pass it the data it needs
}

Now, in the part where I do "//fetch the entry that needs to be edited", I would like to somehow put the fetched data into the Validation object so that it can be displayed in the form without something like <?=(is_object($data))?$data->data_field:$this->validation->data_field?>

Is this possible without subclassing the Validation class?
#2

[eluser]mihailt[/eluser]
why don't use flash messages?
#3

[eluser]Nameless One[/eluser]
I'm not adverse to using other methods to pass the data to the form, but using Validation class for both the erroneous data and the data just fetched from the database for editing would be a very nice and efficient way to do it, if it is possible.

Another way I would like to use if Validation doesn't work is creating the query result object populated with post data if possible. For example, in ZF I would do $entry = new Zend_Db_Table_Row(array('data'=>$data)). Is something like this possible in CodeIgniter if using Validation is not an option?
#4

[eluser]TheFuzzy0ne[/eluser]
I use set_value(), which is defined in the form helper, and auto-loaded by the Form Validation library? The second parameter is where you specify the default value; set the defaults from the database.
#5

[eluser]mihailt[/eluser]
that you could move validation in one method, call it from other and act depending on what validation method returns.
#6

[eluser]Nameless One[/eluser]
[quote author="TheFuzzy0ne" date="1238540440"]I use set_value(), which is defined in the form helper, and auto-loaded by the Form Validation library? The second parameter is where you specify the default value; set the defaults from the database.[/quote]

I'm quite sure this results in an if for each field. I prefer having a single if in the controller which determines which set of data to use for the form.

Since I keep the form field names such that all data is submitted associative arrays that are members of $_POST, for example name="data[entry_name]", the best solution I found right now is this:
When the data is fetched from the database, I pass it on to the view and it is used to populated the fields. When the user is returned to the form because of an error, I pass on the variable $entity = (object) array_merge($this->input->post($data),$this->input->post($additional_data)) which results in the object which is the same as the one fetched from the database (except for the primary key which is not used in populating the fields anyway).


I have another problem with Validation class now, which might prevent me from using it at all. It doesn't seem to work with field names that I use: name="data[field]". I set the rules like this:
Code:
$this->validation->set_rules(array(
    'data[field1]' => 'required',
    'data[field2]' => 'required|alpha_numeric'
));


I also tried like this:
Code:
$this->validation->set_rules(array(
    'data' => array(
        'field1' => 'required',
        'field2' => 'required|alpha_numeric'
    )
));

Nothing works.
#7

[eluser]TheFuzzy0ne[/eluser]
What am I missing? I don't understand what's wrong with set_value(). I use it all the time for my update forms; This is what you're doing is it not?
#8

[eluser]Nameless One[/eluser]
[quote author="TheFuzzy0ne" date="1238541567"]What am I missing? I don't understand what's wrong with set_value(). I use it all the time for my update forms; This is what you're doing is it not?[/quote]

Maybe I am missing something. The example in User Guide for set_values() is:
Code:
<input type="text" name="quantity" value="<?php echo set_value('quantity', '0'); ?>" size="50" />

set_value() has to use an if statement to check whether it should return the default value. Using set_value() on X fields in a form results in X if statements. My way results in only one if statement in the controller which basically chooses between two data sets and passes one to the view.
#9

[eluser]Nameless One[/eluser]
Another problem with my forms is that set_values('data[field]',$some_default_value) probably wouldn't work.
#10

[eluser]Xeoncross[/eluser]
Ya, I always handle my edit forms this way. It is rather hard to explain but here is the basic idea.

1) Check for an ID passed in to the method "edit($id=null)"
A) if null then create an empty object
B) If not then fetch row object

2) Create an array of all required object keys (id, title, body, author)

3) Check empty object/db row for each POST element (id, title, etc..) and update that value if found ($row->title = $_POST[title])

4) Send the object out to the form and use print $row->key instead of the form validation junk.

There is a lot more to it than that - but you should be able to figure the rest out.




Theme © iAndrew 2016 - Forum software by © MyBB