framework: [5.2] Numeric fails to validate against empty value

https://github.com/laravel/framework/blob/5.2/src/Illuminate/Validation/Validator.php#L152

Having numeric (and others) in the $implicitRules variable implies that the particular field MUST have a value, and fails for empty values.

// InvoiceRequest
public function rules()
    {
        return [
            ...
            'amount' => 'required|numeric',
            'vat_amount' => 'numeric',
            ...
        ];
    }

// Checking the HTML output

<li>The vat amount must be a number.</li>

<input class="form-control" placeholder="VAT Amount" required="" name="vat_amount" type="text" value="">

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Comments: 25 (15 by maintainers)

Most upvoted comments

Guys, this is so sick. Now it is not possible (is it?) to validate some primitive formats with “require_if”. E.g. in the following (wild life) use-case, laravel 5.4:

       'voucher_price' => "required_if:voucher_enabled,1|integer",

Now no matter if my “voucher_enabled” checkbox is checked or not - the “voucher_price” is failing validation, because some empty price comes in post. Which is not illegal in this case.

And if I change the rule to:

       'voucher_price' => "required_if:voucher_enabled,1|integer|nullable",

Then it won’t fail validation even when it should: i.e. when price is empty, while required. And when I try this:

       'voucher_price' => "bail|required_if:voucher_enabled,1|integer",

Then if my voucher_enabled is 0, voucher_price is empty (not illegal) and validation fails on “integer” rule, because null is not integer.

Do you expect us to write our own “integer_if”, “min_if” “numeric_unless”, “email_if” custom validations?

I really don’t think this nullable rule makes a lot of sense (or I don’t understand it as much as you guys). I mean, we have already the required rule that validates if you actually pass some data. Having the nullable makes it able to not be required, like the opposite of the required rule BUT only for primitives. From my point of view, if it is not required then it is nullable, and this has nothing to do with the type of the data. I think previous (< L5.1) was more consistant with the wording. Am I missing something here ?

What if an additional ‘nullable’ rule is added to the validations. Wouldn’t that make more sense?

validate(
    ['nullable_field' => null],
    ['nullable_field' => 'numeric|nullable']]
);

This means that the required rule is not necessary any more for 'Array', 'Boolean', 'Integer', 'Numeric', 'String' (which sum up all the possible types you can pass from a form)

@jbajou I agree. To me, nullable should just be the default when a field is not required. If i send null, I don’t expect laravel to be trying to validate that it matches the rule I created for when a “normal value”(anything except null) is actually sent. I understand that laravel is just trying to be strict/explicit about what is being validated, but maybe doing the reverse would make more sense for the majority of devs. IOW, a “not_nullable” rule.

Yes, that is intentional. If you provide the field, it must be numeric, as your validation rules state.

On Tue, Dec 22, 2015 at 8:32 AM, Jordan Dobrev notifications@github.com wrote:

Brakes my project too.

validate( [‘nullable_number’ => null], [‘nullable_number’ => ‘numeric’] )

fails

— Reply to this email directly or view it on GitHub https://github.com/laravel/framework/issues/11452#issuecomment-166629821 .

Yeah I think it is intentional. If you send a value, it has to be numeric. So I would suggest sending 0 or something instead of an empty string.