CodeIgniter Forums
Validated & Non Validated Form Fields - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: Validated & Non Validated Form Fields (/showthread.php?tid=14183)



Validated & Non Validated Form Fields - El Forum - 12-21-2008

[eluser]Unknown[/eluser]
Hey Guys,

I have a question for you. I have a form where some fields require validation, and some do not. Since I only have some fields that need to be validated, I only have those ones in my validation rules:
Code:
$this->form_validation->set_rules('name', 'Name', 'required');
$this->form_validation->set_rules('email', 'Email Address', 'required|valid_email');
$this->form_validation->set_rules('message', 'Message', 'required');
There are two additional fields that are in the form, which are "company" and "website". I was using "set_value()" to return the value's to my fields in my view file, but for my "company" and "website" fields, they were not showing their value - obviously because I am not declaring them in my validation rules (because they don't need to be validated). What I am doing instead is using the post array through the input class to return values to all my fields.

My question is this: what is the best way to handle forms where I have required and non required fields. Should I add the non required fields into the form validation class, and just specify no rules so I can use the "set_value()" function, or should I set the values for all form fields in the view using the post array.

Thanks,
~Nicholas


Validated & Non Validated Form Fields - El Forum - 12-21-2008

[eluser]moodh[/eluser]
Well why not validate them IF something is written in them? just a simple lenght[1] or something, that should solve your problem.

Other rules works perfectly without 'required' =)


Validated & Non Validated Form Fields - El Forum - 12-22-2008

[eluser]Henry Weismann[/eluser]
Good question. I prefer creating the rules with an empty string but using the post function works well too. I think it is really up to your style of coding. The reason I like doing it by creating the rule is because I only want to use the set_value() method in my views for consistency instead of post on this one set_value on another one and so on. If I ever decide to add a rule or format it with a filter it is a quick thing and I also know all my form fields while working on a controller by looking at my rules.


Validated & Non Validated Form Fields - El Forum - 12-22-2008

[eluser]Unknown[/eluser]
Thanks guys for the suggestions. I think that is what I am going to do - add the non required fields into the validation rules, then they are there if I need to run any validation rules on that field, but I can still access them from the "set_value()" method.

Thanks,
~Nicholas