Welcome Guest, Not a member yet? Register   Sign In
form validation on detail screen
#1

Hi,

I have a detail screen, and I'm trying to implement form validation on it as described in 
https://www.codeigniter.com/userguide3/l...ation.html


This works fine for simple having a create screen , where no data is available from the database yet.

However, ideally I would like to reuse the same screen for editing, which means it has to retrieve data from the database.

I know how to get data from the database and display it on the screen, and I know how to show the validated screen, but I'm trying to get the combination of both to work :

1) Should get data from the database on it's values from the database
2) After adapting the screen, it should validate the screen, filling in the incorrect values while leaving the correct values

What is the best practice to deal with this behavior ? A simple example of a editing screen would suffice.

Thanks.
Reply
#2

The combination is in the set_value() function, that comes with the form helper.
Let's assume you want to edit a product record. It has a product name that is loaded from the database.
Normally, you would have a field in your form like this:

PHP Code:
<?php echo form_input('product_name'$product_name);?>

If $product_name has a value, it will be filled in as the default value for the field.
But this won't work with form validation.

This will work:
PHP Code:
<?php echo form_input('product_name'set_value('product_name'$product_name));?>
Reply
#3

(This post was last modified: 03-30-2016, 02:14 PM by cartalot.)

having to create two different forms is not ideal. the way i'm dealing with this is to use the ci form helper. so the first time you pass in the value from the database. if the form validation fails you then use set_value(). even though it failed validation you still want to show the user what they entered in the form so they can correct it (not have to redo it).

PHP Code:
// so grab your database values like 
$productName
// create arrays for your form fields, pass the db values

$field01 = array(
       'name'          => 'product',
       'id'            => 'product',
       'value'         => $productName,
       'maxlength'     => '100',
       'size'          => '50',
); 

PHP Code:
// in your view 
echo form_input($field01); 

ok the form does not validate - so its the same thing except instead of getting the value from the database, use set_value()

PHP Code:
$productName set_value('product');

// note this is exactly the same code
$field01 = array(
       'name'          => 'product',
       'id'            => 'product',
       'value'         => $productName,
       'maxlength'     => '100',
       'size'          => '50',
);

// in your view - its also exactly the same 
echo form_input($field01); 

this is a simple example but should give you some ideas. the other way i've done this, especially when working on large forms with lots of fields, is to create an array of form field arrays without the value like

PHP Code:
$form['field01'] = array(
       'name'          => 'product',
       'id'            => 'product',
       'maxlength'     => '100',
       'size'          => '50',
);

// and then add the value to the arrays in a separate method
// so first from the database
$form['field01']['value'] = $product->productname;

// then if form validation fails, its very easy because everything stays the same except 
// you use set value to assign the form value 
$form['field01']['value'] = set_value('product'); 
Reply
#4

You can also set an edit flag and check it in your form_validation routine.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#5

(This post was last modified: 05-11-2016, 10:54 AM by CINewb.)

Here's how I am doing it:

CONTROLLER

PHP Code:
public function edit($product_id 0) {
   $product $this->get_product($product_id); // Get an existing product, or a blank product
   $this->form_validation->set_rules('name''Name''trim|required');
   if ($this->form_validation->run() == true) {
       // Add or Update your product here
   }
   $this->load->view('product/edit', array('product' => $product));


VIEW

PHP Code:
<input type="text" name="name" id="name" value="<?php echo set_value('name', $product->name); ?>" /> 

This basically populates the "name" field accordingly:

1) If you're editing an existing product and the form hasn't been posted, it is populated with the name from the DB.

2) If you're editing an existing product and the form has been posted, it is populated with the name that was posted.

3) If you're adding a new product and the form hasn't been posted, the field will be blank.

4) If you're adding a new product and the form has been posted, it is populated with the name that was posted.

This is based on the second parameter of set_value() being a default value if the form wasn't posted.
Reply
#6

(05-11-2016, 10:45 AM)CINewb Wrote: Here's how I am doing it:

@CINewb this is interesting but i'm not understanding what is going to happen if the form fails validation? Lets say this is a product form with 30 fields - the admin is editing an existing product - they have changed 20 of the fields, but one of the changes fails validation - so we need to show the form again. We don't want to show them the original values from the database because then they lose all the work that they just did on those 20 fields. Or is there something i'm missing?
Reply
#7

(05-11-2016, 12:48 PM)cartalot Wrote:
(05-11-2016, 10:45 AM)CINewb Wrote: Here's how I am doing it:

@CINewb this is interesting but i'm not understanding what is going to happen if the form fails validation? Lets say this is a product form with 30 fields - the admin is editing an existing product - they have changed 20 of the fields, but one of the changes fails validation - so we need to show the form again. We don't want to show them the original values from the database because then they lose all the work that they just did on those 20 fields. Or is there something i'm missing?

Yes, set_value('name', $product->name) will display whatever work they did in the input field called "name", when the form is posted.  It will only show the $product->name (which comes from the database) when the form is first displayed, before it is submitted.  So it should do exactly what you have said above.
Reply
#8

(This post was last modified: 05-13-2016, 10:47 AM by cartalot.)

(05-11-2016, 01:42 PM)CINewb Wrote: Yes, set_value('name', $product->name) will display whatever work they did in the input field called "name", when the form is posted.  It will only show the $product->name (which comes from the database) when the form is first displayed, before it is submitted.  So it should do exactly what you have said above.

so working through this idea - here is example. i'm just modifying the form examples from the manual. so to build the form fields - in your controller or model or wherever you feel comfortable something like this. so you have gotten the database record for a user and then call this method

PHP Code:
public function returnFormFields($user){

 
       $form['field1'] = array(
 
           'name'          => 'username',
 
           'id'            => 'username',
 
           'value'         => set_value('username',$user->name),
 
           'maxlength'     => '100',
 
           'size'          => '50',
 
       );

 
       $form['field2'] = array(
 
           'name'          => 'email',
 
           'id'            => 'email',
 
           'value'         => set_value('email',$user->email),
 
           'maxlength'     => '100',
 
           'size'          => '50',
 
       );
 
       return $form ;
 
   


in your controller get the form, load it so its available for view (you can also use $data if you prefer)

PHP Code:
       $form $this->userform->returnFormFields($user) ;
 
       $this->load->vars($form); 

in the view
PHP Code:
<h5>Username</h5>
<?
php echo form_input($field1  ?>


<h5>Email Address</h5>
<?php echo form_input($field2) ;  ?>

confirmed that if ci form validation is being used and the form validation fails
- the subsequent load of the form will display the field values submitted - not the database values. this is amazing in how simple and flexible it is!

edit - ok the only disclaimer is - and i could be wrong but it appear that if the validation passes - its still going to display the value that was submitted in the form. in other words its only going to use the values from the database the first time the form is shown - which in most cases should be fine. but if you need to show the updated values - from the database - in the form then the field value will need to be done separately.
Reply
#9

If form validation has not passed, you shouldn't update your database in the first place, but display the form again. Set_value() populates the fields with the user's input. Always echo validation_errors() to inform the user what he has done wrong.
Reply
#10

(This post was last modified: 05-12-2016, 12:14 AM by CINewb.)

(05-11-2016, 05:59 PM)cartalot Wrote: edit - ok the only disclaimer is - and i could be wrong but it appear that if the validation passes - its still going to display the value that was submitted in the form. in other words its only going to use the values from the database the first time the form is shown - which in most cases should be fine. but if you need to show the updated values - from the database - in the form then the field value will need to be done separately.

Yes that's correct. However, if your form has passed validation and you update the database with the value of your field, I would think that 99% of the time the value in both your field and your database will be the same. An exception to this might be if your database silently truncates the data (or converts it to a different type), but this has never been enough to worry me.

If you think there is a major flaw in what I've said, please let me know as I've been doing it this way for years! (before learning CodeIgniter).

Anyway, you can completely eliminate any concerns you have by doing a redirect() directly back to the edit page, after updating your database. This will then show the updated values from the database, and has the added benefit that it helps with the "document expired" message that you sometimes get when using the back button in the browser. If you want to carry a message across with the redirect saying, "Information updated" you can use flashdata for this.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB