Part of the EllisLab Network

Bug Report

Validation Class Does Not Allow Conditionally Required Rules

Date: 03/27/2008 Severity: Minor
Status: Bogus Reporter: ebynum
Version: 1.6.2 SVN
Keywords: Libraries, Validation Class

Description

Validation allows for inputs that are dependent on other inputs (see “matches”), but it does NOT allow an input to be “required” depending on the value of another field. If “required” is set and the field is empty, rule-processing is stopped immediately (with a validation error as expected). If “required” is NOT set and the field is empty, rule-processing is stopped immediately (without a validation error). However, this does not allow users to make a rule requiring the field only if another field is set to some specific value.

Code Sample

/* SUBMITTED IN POST:
     abc_required: 'yes'
     abc: ''
*/

$rules['abc_required'] = "required";
$rules['abc'] = "callback_abc[abc_required]";

function
special($str, $str2) {
   
if($_POST['$str2'] == 'yes') {
      
if(!isset($_POST['abc']) || $_POST['abc'] == '') {
         
return FALSE;
      
} else {
         
return TRUE;
      
}
   }
elseif($_POST['$str2'] == 'no' {
      
continue;
   
}
}

Expected Result

return FALSE

(Note: this is not the expected result - just the desired result)

Actual Result

continued (rules do not get processed)

Comment on Bug Report

Page 1 of 1 pages
Posted by: ebynum on 27 March 2008 7:03pm
no avatar

My suggested fix for this is to create a new native rule called “force_rules” that does nothing more than force the Validation class to process the rules even if “required” is not set and the input is empty. The new code in the Validation->run() function gets changed as follows:

************ ORIGINAL ************
if ( ! in_array(‘required’, $ex, TRUE))
{
  if ( ! isset($_POST[$field]) OR $_POST[$field] == ’‘)
  {
      continue;
  }
}


************ FIX ************
if ( ! in_array(‘required’, $ex, TRUE))
{
  if ( ! isset($_POST[$field]) OR $_POST[$field] == ’‘)
  {
      if ( ! in_array(‘force_rules’, $ex, TRUE))
      {
        continue;
      }
  }
}

Posted by: Derek Allard on 27 March 2008 7:45pm
Derek Allard's avatar

Greetings ebynum,

I do not see a forum thread associated with this bug report, and it looks like it may not be a bug.  Have you first read the bug reporting guidelines and used the Bug Report Forum to get help confirming that this is a bug before reporting?

Thanks!

Posted by: ebynum on 28 March 2008 3:22pm
no avatar

Derek -

I may have made a bad decision here. I assumed (after not finding any other way to report it) that bugs in “design logic” (which are basically the same thing as feature requests, if not exactly) got reported as bugs (like they do with other OS projects like WordPress).

Based on your response, it seems that that is not the case. Where should I report a design bug (as opposed to an implemented code bug)?

The issue here is that by the current design, it is impossible to require validation on empty fields. I understand that there are workarounds (put the validation of those fields on the field that is requiring them), but that doesn’t solve the design failing.

Thanks,
Ed

Posted by: Derek Allard on 28 March 2008 3:24pm
Derek Allard's avatar

Not a problem ebynum.  The best bet is to start a thread in one of the discussion forums.  Feature requests seems logical, or CodeIgniter discussion.

The main reason for this is that it lets us gather community input, and then really make informed decisions moving forward.  The validation class is indeed one we want to re-visit.

Thank Ed.

Name:

Email:

Location:

URL:

Remember my personal information

Notify me of follow-up comments?