Welcome Guest, Not a member yet? Register   Sign In
Validation run() with no data
#1

[eluser]tchule[/eluser]
One little suggestion, I was testing a form with only one rule where my field (a radio button) is mandatory.

When the form was submitted with no value, the rule "required" was not tested because of the first lines of the run() method of the Validation library.

I've changed the code like this:

Code:
if (count($_POST) == 0 OR count($this->_rules) == 0)
{
    return FALSE;
}


Code:
if (count($_POST) == 0 AND count($this->_rules) == 0)
{
    return FALSE;
}

I havn't fully tested the impacts on other forms ...

Regards,

Tchule.
#2

[eluser]wiredesignz[/eluser]
One solution is to use a hidden field with the same name as your radio field, give it a disallowed value, place it before the radio field on the form. Validation will then run and return FALSE.
#3

[eluser]Unknown[/eluser]
Guys,

I had a similar dificulty on this same line of code, but I think there is a bug here. I was starting to use CI now and was going through the User Guide to learn how everything works. But doing the first example from the Validation Class (User Guide -> Validation Class) I faced a problem. Look at the line of code above:

Code:
if (count($_POST) == 0 OR count($this->_rules) == 0)
{
    return FALSE;
}

In my point of view, this code says that if you don't have any validation rule your validation will be always false. But a basic (maybe stupid Smile ) use of this class that don't use any validation rule would mean that my data is always correct. So a better solution could be:


Code:
if (count($_POST) == 0)
{
    return FALSE;
}
elseif (count($this->_rules) == 0)
{
    return TRUE;
}

Maybe I'm forgetting something here once I'm not so experienced with CI and maybe this could have some bad effect, but the way it is implemented now is not working...
#4

[eluser]tchule[/eluser]
Hello,

I've just met again this problem on a new projet with CI v1.6.2.

These lines of code seems to be an optimisation to avoid running the checks when it's not necessary.
The default behavior can depend on what we expect but it seems to me that adding an hidden field in the view is more a hack.

I tend to agree with Jader and I've changed my version of the library like this :

Code:
// if no rule is set then the validation is always true
if (count($this->_rules) == 0)
{
     return TRUE;
}

The fact that the form contains no data at all is handled by the "required" rule.
#5

[eluser]xwero[/eluser]
I think the problem can be solved simply by adding a name to the submit button. When the button has a name the value will be send, this makes the check false and the actual validation will run.




Theme © iAndrew 2016 - Forum software by © MyBB