When using CodeIgniter's Form_validation
class, the set_rules()
method allows a user to add an infinite number of checks to their form fields. These rules are usually all defined in the same place, right before the run()
method is called.
VALID EXAMPLE:
// Require the user to be a werewolf.
$this->form_validation->set_rules('werewolf', 'Are you a Werewolf?', 'required');
// If the form validates.
if ($this->form_validation->run())
{
// [...]
(no, I do not actually run a werewolf website)
The code makes a lot of sense, and is very easy to understand. However, at times, it may be undesirable. For example, perhaps you don't care if the user is a werewolf unless it is midnight. Your code would probably look like this:
INVALID EXAMPLE
// If it is midnight.
if (date('G') == '0')
{
// Require the user to be a werewolf.
$this->form_validation->set_rules('werewolf', 'Are you a Werewolf?', 'required');
}
// If the form validates.
if ($this->form_validation->run())
{
// [...]
THIS WON'T WORK
At least, it won't work until midnight.
By default, $this->form_validation->run()
returns FALSE
if there aren't any rules declared. In the second example, unless it's midnight, there will not be any rules set, so the form will never validate. Logically, one would assume that if there are no rules, the form should validate no matter what. Instead, it fails no matter what.
/system/libraries/Form_validation :: run()
(lines 293-297)
// No validation rules? We're done...
if (count($this->_config_rules) == 0)
{
return FALSE;
}
My question is:
Why is this the default return value? Is it okay if I change it?