Welcome Guest, Not a member yet? Register   Sign In
Validation with database data
#1

[eluser]faceh[/eluser]
I just started using your validation class, which is extremely useful.

I have one question however. I am using a form for a customer to enter his address details in to a form, which must be validated and of course have the input data re-inserted in to the form in the case of failure.

My issue is that I originally want to fill the form with database data of his default address, and then have this data replaced if he submits.

ie. On first page load, the customers default address is grabbed from the database and the form is populated.

If he then makes changes and hits submit, but the form has errors, the form will be re-displayed with the POST data rather than database data.


Whats the best practice to achieve the above?
I can do it with lots of if's in the VIEW, but there must be a cleaner way.
#2

[eluser]Derek Allard[/eluser]
This is one area where we could find a nicer way to handle this. Here's how I currently do it in BambooInvoice. This is in the view.

Code:
<input value="<?php echo ($this->validation->company_name) ? ($this->validation->company_name) : ($row->company_name);?>" />
#3

[eluser]Rick Jolly[/eluser]
You can go with Derek's example, or you can prepopulate the validation object in the controller with the database data. After you set the fields ($this->validation->set_fields($fields)Wink, you can populate them ($this->validation->some_field = $some_db_dataWink. Of course, you would only want to populate the fields if the form hasn't yet been submitted. To do that, you can check if the $_POST array is defined or check the $_POST array for some hidden field or the submit button field.
#4

[eluser]danoph[/eluser]
I have been using Derek's method with the ternary operator for a while:

Code:
<?=(a = b) ? 'do something if true' : 'do something if false'?>
Code:
<?=form_input('company_name', (!empty($this->validation->company_name)) ? $this->validation->company_name : 'default value'?>
#5

[eluser]faceh[/eluser]
yup, thats the way im doing it basically, but prepopulating the fields in the controller rather than in the view - lots of ifs, lots of extra code - just looks a bit ugly. I suppose its the only way though, as you have to grab each piece of data from DB, sometimes different places. Oh well. Soldier on I guess!
#6

[eluser]gon[/eluser]
UPDATE:

Actually the solution I proposed has been told about.




Theme © iAndrew 2016 - Forum software by © MyBB