Welcome Guest, Not a member yet? Register   Sign In
Update Form Validation
#1

[eluser]harpster[/eluser]
Hi I'm learning CI and doing my first CRUD project. It's taken while but I have finally made a successful entry form with input fields, radios, checkboxes, & drop downs. The manual helped but I had to do quite a bit of on-line research to get it working correctly. Anyway I just added validation to it and some jquery for a date picker and showing/hiding form fields. It's working and will validate & then update the database correctly. Cool!

Now I'm dealing with the update form which worked OK before but now I need to add the validation to that and having some problems.

1. In my controller I have an entry method with all the validation for the entry form. Since this will be used on the update form I was going to copy all of the rules and put it in the controllers update method. Is this how it's usually done or do you uses the same rules for both entry and update - meaning only one set of rules that are called from either controller?

2. The real problem I'm having is the update form (view) is populated from the database so it looks like this

Code:
<?php foreach ($row as $row) { } ?>
    
<?= form_open('cycling/update2'); ?>

<?= form_hidden('id', $this->uri->segment(3));?>

<table>
    <tr>
      <td class="align_r">Date</td>
      <td>&lt;input type="text" name="ridedate" id="datepicker" value="&lt;?= $row-&gt;ridedate ?&gt;"</td>
    </tr>
    <tr>
      <td class="align_r">Distance</td>
      <td>&lt;?= form_input('distance', $row->distance); echo form_error('distance'); ?&gt;</td>
    </tr>
    <tr>
      <td class="align_r">Ride Time</td>
      <td>&lt;?= form_input('ridetime', $row->ridetime); echo form_error('ridetime'); ?&gt;</td>
    </tr>

So now when I try to validate the update form I get errors because there is no $row data as the fields are to be populated from $_POST validation and not the database data.

What's the best way to validate update forms to get around this problem? I'm sure I must be missing a simple concept in doing this as it has to be a pretty common action. Populate the update form from the db but validate changes from $_POST. Thanks for the help!
#2

[eluser]Jaketoolson[/eluser]
Within the controller you should do the DB vs $_POST comparison/processing/merging of the results then pass a results array to the view.

Here's a very bland example:
Code:
// $options = the DB array.
// $_POST = the $_POST array.  
// array_merge will overwrite existing key->value pairs (if they exist).

$options = array('ridetime' => '2 hours', 'distance' => '26 miles');
$_POST = array('distance' => '28 miles', 'ridedate' => '2011-02-01');

$data = array_merge($options,$_POST);

// $data outputs the following array:
array (
  'ridetime' => '2 hours',
  'distance'  => '28 miles',
  'ridedate'  => '2011-02-01'
)

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

As it pertains to form validation rules, you can create an array with rules and use them throughout the site. See 'Setting Rules Using an Array'.
#3

[eluser]harpster[/eluser]
I think I see what your saying but still missing something. When I first load the update view based on a record ID, I need to populate the view with that record data so there is no POST data initially. I'm doing that with this model method:

Code:
function get_row()
    {
        // fetch the update row by id (segment 3)
        $this->db->where ('id', $this->uri->segment(3));
        $query = $this->db->get('cycling');

        // return the result
        if ( $query )
        {
            return $query->result();
        }
        else
        {
            return FALSE;
        }
    }


And in the update view I'm using $row->field_data to populate the form. Maybe this is not the best way for form validation, I'm not sure?

Next I make some changes in the update form and submit it to the controller which does the validation check. The update form is expecting to see $row as the data array but the validation populates the form with the set_value like this example from my entry form.:

Code:
form_input('distance', set_value('distance'));

where as in my update view it's like this:

Code:
form_input('distance', $row->distance);

So I'm just wondering how I can restructure my update form to take either the database record ($row->distance) or the validation set_record. I understand the database side better than the validation part and I see how I could do what you said and compare the $_POST array to the database record but then not sure how to pass that along to the update view where the validation check would repopulate the fields?

Edit: After thinking about what you said some more, I'm thinking that maybe I should use the form validation for the error messages ONLY and NOT for repopulating the form. Is that the general idea?
#4

[eluser]harpster[/eluser]
I didn't get much response here so maybe I didn't ask the question well enough. I did a lot more research and finally found something related to what I was looking for. So when you first load the update form you check or set a variable and load your db record. Then if you submit the update form with an error it will check the variable ($ae in this case) and if unset will repopulate the form with he set_value('name') from the validation.

Code:
&lt;?php echo form_input('name',
    ($ae == 'edit') ? $records[0]->name : set_value('name')
); ?&gt;

Is there a more desired way to handle this or is this good? I'm working hard to learn CI bit it's taking some time and some of the tutorials and videos out there are either dated or incomplete in regards to the complete CRUD with validation. Thanks!
#5

[eluser]kirkaracha[/eluser]
deleted
#6

[eluser]ramm[/eluser]
Hello.

Maybe this will help.

Code:
&lt;?php echo form_input('email', set_value('email', $user->email)); ?&gt;

You will use the set_value() function, the first parameter ('email') will repopulate the fields when you have errors on send, and the second parameter ($user->email) will be the default data to populate the form on load.




Theme © iAndrew 2016 - Forum software by © MyBB