Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Validators not working for me?
  • Hi! I just downloaded and installed Fuel, and use oil to generate a form for creating new user accounts. I'm having a problem with validators for some reason. I added validation rules to the action_create method, in two different formats just to get acquainted: $val = Validation::factory('my_validation');
    $val->add_field('first_name', 'First name')->add_rule('required')->add_rule('min_length',3);
    $val->add_field('can_discount', 'Can discount', 'required|numeric_min[0]|numeric_max[1]'); first_name should be required and min length 3, and can_discount should be either the number 0 or 1 Problem #1
    When I submit the form with no input, the first rule throws the following exception:
    ErrorException [ Warning ]: explode() expects parameter 2 to be string, array given COREPATH/classes/validation.php @ line 112: 111:
    112: $rules = explode('|', $rules);
    113: foreach ($rules as $rule)
    To get past this, I comment out the first rule. Problem #2
    The second rule properly generates an error message requiring me to input a value, however when I input a non-number like 's' the rule lets me past. Maybe I'm doing something wrong?
  • It's a bug though not an important one: the ->add_field() method is only meant to be used when passing rules as a string (which is a simple shortcut for using add()->add_rule()->etc...). Because the add_field() method was basicly a modified version of add() it still has array() as a default value. Which no one noticed as you're probably the first to use add_field() without passing rules. It'll be fixed in the dev branch, for now you can just use add() instead of add_field() there.
  • Thanks for the reply, but numeric_min[0] and numeric_max[1] still don't work in the following statement: $val->add_field('can_discount', 'Can discount', 'required|numeric_min[0]|numeric_max[1]'); It allows the entry 'S' through. ?
  • It doesn't check whether your input is numeric, it just converts the input to a float and compares that with the arguement you've given. In this case "S" is converted to 0.0 and matches "0" and is allowed. If you want to check wether the input type is correct, add a rule "is_numeric" - that way you can also give a far better error message. If you're interested, the method can be found here.
  • Thanks, this works, sorta (the user gets a not so nice message 'Validation rulle is_numeric failed for {field}' - yuk : ) I was confused on this issue because the documentation on this validation rule (http://fuelphp.com/docs/classes/validation.html) says numeric_min $min_val Tests whether the given input is a number that is greater than $min_val.
    numeric_max $max_val Tests whether the given input is a number that is smaller than $max_val. Thus I was lead to believe that is_numeric was being checked in the rule - would be a smart addition me thinks. Best.
  • Jon Pokress wrote on Friday 20th of May 2011:
    Thanks, this works, sorta (the user gets a not so nice message 'Validation rulle is_numeric failed for {field}' - yuk : )
    That is shown for every rule that doesn't have a message set
    I was confused on this issue because the documentation on this validation rule (http://fuelphp.com/docs/classes/validation.html) says numeric_min $min_val Tests whether the given input is a number that is greater than $min_val.
    numeric_max $max_val Tests whether the given input is a number that is smaller than $max_val.
    What it means is that both are cast to a float and then compared, not that they check whether the input is numeric. Might warrant a note about that for both though.
    Thus I was lead to believe that is_numeric was being checked in the rule - would be a smart addition me thinks.
    Might help, though it would be better if you checked is_numeric and were to cast it to a numeric value before by adding floatval or intval to the rules before testing the min/max values. I'll think about it, but as always: if you really feel that way, fork the repo, make the change and pull-request.
  • Just to round this out for the benefit of others who might hit this, this got me closer: $val = Validation::factory('my_validation');
    $val->set_message('valid_string', 'Sorry, but field :label is not a valid :param:1. Please check your entries and try again.');
    $val->add_field('can_discount', 'Can discount', 'required|valid_string[numeric]|numeric_min[0]|numeric_max[1]'); This gets me: Sorry, but field Can discount is not a valid numeric. Please check your entries and try again. Haven't quite figured out how to translate numeric into 'number', but hey, it's a start. : )
  • You should use PHP's build in is_numeric here, it's better than the validate_string rule for this goal.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion