Welcome Guest, Not a member yet? Register   Sign In
Yet another form + validation inquiry (sorry :-( )
#1

[eluser]stevefink[/eluser]
Hey all,

So I've decided to create a separate controller/method for editing a form with data populated that a user originally submitted on a prior date. My question is, how do I keep validation for input fields if my markup looks like this:

Code:
<input type="text" name="ext_color" value="<?= $this->validation->ext_color; ?>" />

In a form without CI type validation, I'd normally put something like:

value="<?= $from_db_value ?>" etc.

There anyway I can end up doing this without copying over a new view?

Thanks all. :-)

- sf
#2

[eluser]Colin Williams[/eluser]
This is why the validation class needs a set_values() method. I think someone whipped this up in a library, actually. Try a search.
#3

[eluser]Derek Allard[/eluser]
Here's how I generally go about it.
Code:
<input name="company_name" type="text" id="company_name" value="<?php echo ($this->validation->company_name) ?
($this->validation->company_name) : ($row->company_name);?>" />

or more stretched out: if there is validation data, use that, else, lead the row from the database.
#4

[eluser]Colin Williams[/eluser]
That makes for nasty view code, in my opinion. I think that logic makes more sense happening in the Validation class.

For example:

Code:
$fields['username'] = 'Username';
$fields['email'] = 'email';
$this->validation->set_fields($fields);

$values['username'] = $row->username;
$this->validation->set_values($values);

And by your example, the logic could break down if someone had a field that was populated with a value, say 'foo.' They attempt to set this value to blank, generate a validation error elsewhere, when the form is reloaded, they would see 'foo' in the field they left blank. I believe you would need ($this->validation->company_name !== FALSE) ? [...]
#5

[eluser]stevefink[/eluser]
Thanks for the replies folks.

I'll search for the forums to try to find the wrapper utility that's been written for the Validation class. If I'm not successful in finding it, I'll spend some time putting together something and share it with the community in the Wiki.

- sf
#6

[eluser]ariok[/eluser]
did'you find some soulutions?
#7

[eluser]stevefink[/eluser]
ariok -

I haven't had time to do the search. I'm currently using the methods suggest by Derek above.

hth

- sf
#8

[eluser]phester[/eluser]
Steve,
You might want to check out this post. Let me know if it helps!

Thanks,
#9

[eluser]parrots[/eluser]
I needed a function similar to the set_value one described above, so I went ahead and added it to the Validation class. I didn't see this posted anywhere else, sorry if it's a dupe of anyone's previous code. It's ideal for an edit form where you might want to pre-populate a form with data from a database but once they submit (and possibly zero out something) you don't want to use that data anymore. Figured it'd be worth posting here for people, even if it was a pretty easy mod...

Add this global variable up with the rest of them in system/libraries/validation.php:
Code:
var $_defaults = array();

Replace the foreach block in the set_fields method with this (as per Darren Inwood's somewhat similar mod here):
Code:
foreach($this->_fields as $key => $val)
{        
    if ( isset($_POST[$key]) AND ! is_array($_POST[$key]) )
    {
        $this->$key = $this->prep_for_form($_POST[$key]);
    }
            
    if ( ! isset($this->$key) )
    {
        $this->$key = '';
    }
            
    $error = $key.'_error';
    if ( ! isset($this->$error))
    {
        $this->$error = '';
    }
}

Then add the following method anywhere in the class:
Code:
// --------------------------------------------------------------------
    
/**
* Set Default Values
*
* This function takes an array of field names and value
* defaults as input and uses them if the form was not
* posted.
*
* @access    public
* @param    mixed
* @param    string
* @return    void
*/
function set_values($data, $value = '')
{
    if ( ! is_array($data))
    {
        if ($value == '')
            return;
            
        $data[$data] = $value;
    }

    foreach ($data as $key => $val)
    {
        $this->_defaults[$key] = $val;
    }
    
    foreach($this->_defaults as $key => $val)
    {        
        if (count($_POST) == 0) {
            $this->$key = $this->prep_for_form($val);
        }
    }        
}

Then use it as such in your controller:
Code:
$rules['email']        = "trim|required|valid_email|xss_clean";
$rules['name']        = "trim|required|xss_clean";
$rules['message']    = "trim|required|xss_clean";
$fields['email']    = 'email';
$fields['name']        = 'name';
$fields['message']    = 'message';

$data['email'] = $this->session->userdata('email');
$data['name'] = $this->session->userdata('name');
$this->validation->set_values($data);

This makes it so you can always set your form values to $this->validation->inputname, like so, and you'll never have to worry about where the data is coming from in your view.
Code:
<label for="name">Name</label>
&lt;input type="text" id="name" name="name" value="&lt;?php echo $this-&gt;validation->name;?&gt;" size="35" />
#10

[eluser]phester[/eluser]
Parrots, that's a great fix, thanks for sharing! I usually try to avoid changing the CI's existing libraries to make my application forward compatible with future versions of CI. Another quick fix I thought about is this:

Say you have an text field named "email", on your first load of the form, do this:

Code:
$this->validation->email= $this->session->userdata('email');

This works as well - however you solution is more elegant since it prep's the data to be loaded for the form as well.

I'm curious to see what everyone's thoughts are for this non-obtrusive approach.




Theme © iAndrew 2016 - Forum software by © MyBB