Welcome Guest, Not a member yet? Register   Sign In
Pre-populated inputs and validation
#1

[eluser]codex[/eluser]
Hi,

I'm not sure how to implement this, so I could use a bit of advice. I have an 'edit profile' form which is prepopulated with the user's data. Some fields can't be left empty so his needs to be validated. For instance, a user can edit his name, but he can't completely erase it.

To validate I have to populate the input value like so (it's based on an own helper function, last option is 'value'):

Code:
<?php echo input_text("user_name", 20, "input_text", $this->validation->user_name)?>

But this way I can't populate the initial value with '$query[0]->user_name'. What can I do?

I thought of this:

Code:
<?php
if (!$this->input->post('user_name')) {
$name = $query[0]->user_first_name }
else { $name = $this->validation->user_name
}
echo input_text("user_first_name", 20, "input_text", $name)?>

But then I get an 'undefined variable' error. And something tells me I'm not thinking correctly ;-)
#2

[eluser]Phil Sturgeon[/eluser]
Code:
// Just using the rules array to get a list of field names
foreach(array_keys($fields) as $field):
    $data[$field] = (isset($this->validation->$field)) ? $this->validation->$field : $this->something_model->getPropFromID($field, $foobarID);
endforeach;

I then just pass data to view. This is similar to what you have but you are going about it the wrong way.

Validation gives you errors, post will quietly get on with itself even without a value, so its best to test for validation with isset and if nothing there, shove in your default value from the DB.
#3

[eluser]codex[/eluser]
[quote author="thepyromaniac" date="1187123502"]
Code:
// Just using the rules array to get a list of field names
foreach(array_keys($fields) as $field):
    $data[$field] = (isset($this->validation->$field)) ? $this->validation->$field : $this->something_model->getPropFromID($field, $foobarID);
endforeach;

I then just pass data to view. This is similar to what you have but you are going about it the wrong way.

Validation gives you errors, post will quietly get on with itself even without a value, so its best to test for validation with isset and if nothing there, shove in your default value from the DB.[/quote]


Thanks, but I don't get it. :red:

Could you provide a sample of how it's used in a form?
#4

[eluser]Phil Sturgeon[/eluser]
Okie doke, using validation right? You make your big

Code:
<?
class Form extends Controller(){

function form1($farmerID = 0){

$rules = array(
   'wellies' = 'required|greather_than[1]',
   'velcrow_gloves' = 'required',
   'lonley_sheep' = 'required'
);

$fields = array(
   'wellies' = 'Rubber Boots',
   'velcrow_gloves' = 'Sticky Gloves',
   'lonley_sheep' = 'Critter Companion'
);

// run validation functions
$this->validation->whatever...

if(validation == true):
echo "Right on brother, have some fun with that critter!";
redirect('goto/quiet_barn');
else:
echo $this->validation->error_string;
endif;

$default_data = $this->farmer->getSheepRelationData($farmerID);

// Foreach form field
foreach(array_keys($fields) as $field):
    $data[$field] = (isset($this->validation->$field)) ? $this->validation->$field : $default_data[$field];
endforeach;


$this->load->view('whatever', $data);

}
}
?>
#5

[eluser]codex[/eluser]
Thanks, but I still don't get it. What do you put in your input value then?

Code:
<?php echo input_text("user_name", 20, "input_text", $this->validation->user_name)?>

Sorry, I'm new to CI. Still trying to understand how things work. :red:
#6

[eluser]smith[/eluser]
Put one hidden field to your form, for example "marker" and give it value=1.
So, now, when your form is displayed for the first time marker is "0". when your form is submited marker value is "1".
Second, try this:
if you are repopulating form from the database:
Code:
$sql = "SELECT some_field from some_table";
$query = $this->db->query($sql);
$row = $query->row();
$this->validation->some_field = $row->some_field;

so, when you want to populate the form, marker will be having value "0". you can use it in your IF's. when the form is submited you will be using values from the form, so you will not execute sql query and change $this->validation->some_field value.
#7

[eluser]BravoAlpha[/eluser]
I'm using something like this right now:
Code:
<?php

class User extends Comtroller {

    function edit()
    {
        // whatever
        
        if ($this->validation->run())
        {
            // whatever
            redirect('wherever');
        }
        else if (!$this->input->post('submit'))
        {
            foreach($query[0] as $key => $value)
                $this->validation->$key = $value;
        }
        
        // whatever
        $this->load->view('whatever', $data);
    }

}

?>
#8

[eluser]Chop[/eluser]
I know I'm resurrecting this from the past, but I'm having this issue right now.

First off - it would be really nice if CI had two extra bits of functionality with the validation class. They have $rules and $fields definitions - now lets us define $values. These would be seed values that would be default values of $this->validation->{field_name} when no submit has taken place.

Next up, it'd be offly nice if there was a BOOL that we could access to see if the POST operation had happened. I know that you can fudge it with hidden values and chacking the post data, but it would still be nice to have a $validation_processed variable that we could hit.

Now then, with respect to the code in this post - thepyromaniac - I like the way you think. My only issue is that this:

Code:
isset($this->validation->$field)


seems to always be true with me. I'm not sure if this is a CI version difference, a PHP difference, or what - but my check had to be:

Code:
($this->validation->$field!="")

I'm off to the request forums to see if anyone else has made this request.
#9

[eluser]BravoAlpha[/eluser]
[quote author="Chop" date="1187382560"]My only issue is that this:
Code:
isset($this->validation->$field)
seems to always be true with me.[/quote]
I think those values are always set once you call `$this->validation->set_fields()`.

Validation.php:
Code:
foreach($this->_fields as $key => $val)
        {        
            $this->$key = ( ! isset($_POST[$key]) OR is_array($_POST[$key])) ? '' : $this->prep_for_form($_POST[$key]);
#10

[eluser]Tom Vogt[/eluser]
I've got the same problem, and a different solution. What I do is check the validation return and if it's not there, return the default value, so in an example:

Code:
echo $this->validation->email_error;
    echo form_input(array('name'=>'email', 'size'=>'32', 'maxlength'=>'64', 'value'=>$this->validation->email?$this->validation->email:$userdata->email));

Works like chaarm. Maybe it's not so elegant, but it allows me a little more freedom to juggle the data around in the view, because I have both validation and default values from the database available.




Theme © iAndrew 2016 - Forum software by © MyBB