Welcome Guest, Not a member yet? Register   Sign In
Form validation issue in form already populated!
#1

[eluser]Fierymind[/eluser]
First, the form validation of CI 1.7 is GREAT, it really save a lot of time and hours of coding.

I could use From validation when I fill an empty form .. I could make form re-populate the correct form fields if the validation failed .. (really great feature too)

Problem now in Validation and re-population of already filled form, Imagine an EDIT form, the form will get $data from a DB and fill form inputs in the view file the regular way:

Code:
<?php echo form_input('name',$name); ?>
<?php echo form_input('address',$address); ?>

but if you want to use the form validation + the re-population feature, you will need the view file to look like that:

Code:
<?php echo form_input('name',set_value('name')); ?>
<?php echo form_input('address',set_value('address')); ?>

but in that case, form will loaded with EMPTY fields

any idea to how to load filled inputs and implement the re-population feature of the form validation?
#2

[eluser]Fierymind[/eluser]
Got it, its in manual Smile .. here is hte solution

Code:
<?php echo form_input('name',set_value('name',$name)); ?>

this will load the default value from DB, and will be re-populated just fine if validation failed.

CI IS GREAT THING
#3

[eluser]Aken[/eluser]
I'm glad they added this in 1.7, because writing a bunch of IF statements to check for the validation and such was a PITA Big Grin

Although now I have turned to AJAX form validation, so I don't need any of the validation rules (though I do put most things through xss_clean just in case).
#4

[eluser]Fierymind[/eluser]
I think all developers use Javascript now to validate forms, but still, you need to add server side validation for the obvious security reasons.
#5

[eluser]Erwin Setiawan[/eluser]
Yeah you'll get empty^^
You forget some scripts..

Quote:you must set rules on it from input to required.

for example:

Code:
<?php echo form_input('name',$name); ?>
<?php echo form_input('address',$address); ?>

on controller you must add:

Code:
$this->form_validation->set_rules('name', 'Name', 'trim|required');
$this->form_validation->set_rules('address', 'Address', 'trim|required');

Try it to submit your form. And you'll get your value back.
Smile
#6

[eluser]Unknown[/eluser]
Hi Erwin,
when you define the rules, in this case "name" and "address", the framework don't automatically create the variables in the context for you. In your example case
Code:
<?php echo form_input('address',$address); ?>
the variable $address must be passed to view by the controller.
The true is, in this new form validation model the framework lost some of the flexibility to re-populate the fields data. I wish that the Core Team review this model and try to make the thinks better.
#7

[eluser]everdaniel[/eluser]
[quote author="Fierymind" date="1227402859"]Got it, its in manual Smile .. here is hte solution

Code:
<?php echo form_input('name',set_value('name',$name)); ?>

this will load the default value from DB, and will be re-populated just fine if validation failed.

CI IS GREAT THING[/quote]

Hey Fireymind!, did you were able to make the set_value to work with the data from your database? I posted a comment earlier today about a problem I have with set_value, it's not using the value from the db (it's returning an empty value) in my case.

Thanks!




Theme © iAndrew 2016 - Forum software by © MyBB