Part of the EllisLab Network
   
 
form-validation depending on a checkbox
Posted: 04 July 2009 11:27 AM   [ Ignore ]  
Lab Assistant
Avatar
RankRank
Total Posts:  263
Joined  07-31-2008

hey there

I’m stuck with a common problem, I guess…

I have a form, where a user needs to select from certain drop-downs. Nothing fancy so far, simple form-validation.
Additionally, I have a checkbox, from which further form-validation would depend. E.g. “alternative delivery address” - if checked, the following fields are mandatory, if not, then not.

I couldn’t come up with a smart solution so far. The only thing I can think of is for each address-field (name, street, city…) to define a callback. In each callback, I would check if the checkbox is marked, and then do the rest of the check.
This would result in many small callbacks, not so nice I’d say… could somebody think of a smarter solution?

Thx for any tipps!
Oliver

 Signature 

RostElyn - Russian Language in Switzerland
TableEditor: No more tirying CRUD-code

Profile
 
 
Posted: 04 July 2009 03:21 PM   [ Ignore ]   [ # 1 ]  
Summer Student
Total Posts:  18
Joined  07-03-2009

maybe:

...
   
// all the validations you want to run by default
   
$this->form_validation->set_rules('email', 'E-mail', 'trim|required|valid_email');

   if (isset(
$this->input->post('checkbox_name')) && $this->input->post('checkbox_name') == 'on')
   
{
      $this
->form_validation->set_rules('other_variables', 'blablabla', 'trim|required');
   
}

   
if ($this->form_validation->run() === TRUE) {
...
Profile
 
 
Posted: 05 July 2009 05:04 AM   [ Ignore ]   [ # 2 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  263
Joined  07-31-2008

I do store the rules in rule-groups in an external file, therefore I call them like

if ($this->form_validation->run('signup') == FALSE) { ...

Unfortunately, I can’t chain/cascade different groups according to the manual I think, which would come real handy in here. Would be nice as an additional feature…

 Signature 

RostElyn - Russian Language in Switzerland
TableEditor: No more tirying CRUD-code

Profile
 
 
Posted: 05 July 2009 11:38 PM   [ Ignore ]   [ # 3 ]  
Summer Student
Total Posts:  18
Joined  07-03-2009

then maybe

if (isset($this->input->post('checkbox_name')) && $this->input->post('checkbox_name') == 'on')
   
{
      
if ($this->form_validation->run('validation_if_checkbox_is_off') == FALSE || $this->form_validation->run('rest_of_validation_if_checkbox_is_on') == FALSE)
      
{
         
...
      
}
   }
else {
      
if ($this->form_validation->run('validation_if_checkbox_is_off') == FALSE)
      
{
         
...
      
}
   }

?

Profile
 
 
Posted: 06 July 2009 02:25 AM   [ Ignore ]   [ # 4 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  263
Joined  07-31-2008

yeah, this would work. it results in redundant rules in the external file, though. But might be still better than many small callbacks, I guess…

 Signature 

RostElyn - Russian Language in Switzerland
TableEditor: No more tirying CRUD-code

Profile
 
 
   
 
 
‹‹ Problem while loding page      One page view ››
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 819, on March 11, 2010 11:15 AM
Total Registered Members: 120524 Total Logged-in Users: 35
Total Topics: 126590 Total Anonymous Users: 5
Total Replies: 665515 Total Guests: 398
Total Posts: 792105    
Members ( View Memberlist )
Newest Members:  AmityBetbulJahhnvicrussell@gmail.comdefrtymusclesseownikaiyuharry295tanyagrey312