t0berius's avatar

laravel validation rules

I'm struggeling with laravel validation rules.

I've got the following fields:

'productPrice'  => 'required|numeric|min:1|max:100',

'reqAmount1'            => 'sometimes|nullable|integer|min:2|required_with:newproductPrice1',
'newproductPrice1'      => 'sometimes|nullable|numeric|required_with:reqAmount1|lt:productPrice|max:100',

How to extend the ruleset for a new

reqAmount2
newproductPrice2

The input should be optional. Here's the "rule" set required:

-if reqAmount2 is filled reqAmount1 & newproductPrice1 & newproductPrice2 are required (!= null)
-if reqAmount2 is filled, it needs to be greather than reqAmount1 (the amount of purchased products needs to be larger than reqAmount1)
-if newproductPrice2 is filled, it needs to be less than newproductPrice1

Hope you understand the functionality itself, the seller should be able to define new product prices, depending on the amount a user is purchasing.

Using sometimes and nullable won't work for the new fields reqAmount2 and newproductPrice2 I think.

0 likes
8 replies
bobbybouwmann's avatar

sometimes works fine as long as you don't send the value along! Otherwise you need to use nullable. However if the value is not null all the rules will be applies. So I think you either need to use sometimes or nullable, not both!

The rules itself look good to me!

Also if the regular laravel rules are really hard to apply you might want to make a custom rule yourself so you can apply more logic there ;)

t0berius's avatar

@bobbybouwmann

So I should apply only sometimes or nullable to my existing ruleset? What about my reqAmount2 & newproductPrice2 any suggestions on how to proceed there?

I can't figure out how to make the reqAmount1 & newproductPrice1 required if reqAmount2 & newproductPrice2 are filled. Currently this doesn't work.

t0berius's avatar

Okay, I'll take a look into custom rules for my needs. The fields are send, but they are empty. The form itself is send (no AJAX).

@bobbybouwmann

How to pass values to the custom rule? Rule was defined, using artisan command.

I'm unsure if this is correct (custom rule)

public function passes($attribute, $value, $parameters)
{
    Log::info($attribute);
    Log::info($value);
    Log::info($parameters);
}

Rule applied at the moment using:

'scalePriceAmount2'     => ['nullable', 'integer', 'required_with_all:scalePriceAmount1,scalePriceBasePrice1', new ScaledPrice],

But how to pass now values to this rule? For my custom ruleset I would need to pass other form inputs to the rule, to validate if they are set etc.

Tray2's avatar

I have a bad feeling in my stomach when reading your code. When you start using numbers in your field names you have a bad design. I suggest using another table for all those fields that way it's scalable out of the box.

Classic one to many scenario.

t0berius's avatar

@tray2

No need to feel bad. The values are inserted into database, but for sure they are stored using a table, but this is not part of the business inside this case.

t0berius's avatar

May somebody may be able to assist me? It seems like a hercules task to me, the laravel validation rules seems to cause a henn eg problem to me when trying to define the rule.

The ruleset would be:

'basePrice'                     => 'bail|required|numeric|min:1|max:1000',

'scalePriceAmount1'
    => only required if scalePriceBasePrice1 is filled
    => needs to be integer
    => needs to be gt 1
    => integer value
'scalePriceBasePrice1'
    => only required if scalePriceAmount1 is filled
    => requires basePrice to be "validated" by its rule
    => numeric
    => less than basePrice but > 0

'scalePriceAmount2'
    => only required if scalePriceBasePrice2 is filled
    => requires scalePriceAmount1 & scalePriceBasePrice1 to be set AND validated
    => needs to be integer
    => needs to be gt scalePriceAmount1
    => integer value
'scalePriceBasePrice2'
    => only required if scalePriceAmount2 is filled
    => requires scalePriceAmount1 & scalePriceBasePrice1 to be set AND validated
    => numeric
    => less than scalePriceBasePrice1 but > 0

I've defined a custom rule already with the switch statements already:

public function passes($attribute, $value) {

    switch ($attribute) {

        case 'scalePriceAmount1':

    //request values can be accessed using:
    $this->parameters->scalePriceAmount1;

        break;

        case 'scalePriceBasePrice1':

        break;

        case 'scalePriceAmount2':

        break;

        case 'scalePriceBasePrice2':

        break;

        case 'scalePriceAmount3':

        break;

        case 'scalePriceBasePrice3':

        break;

        case 'scalePriceAmount4':

        break;

        case 'scalePriceBasePrice4':

        break;
    }
}

Please or to participate in this conversation.