Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lte/gte/gt/lt not using custom attribute labels #29441

Closed
tillkruss opened this issue Aug 7, 2019 · 11 comments · Fixed by #29716
Closed

lte/gte/gt/lt not using custom attribute labels #29441

tillkruss opened this issue Aug 7, 2019 · 11 comments · Fixed by #29716
Labels

Comments

@tillkruss
Copy link
Collaborator

  • Laravel Version: 5.8.31
  • PHP Version: 7.3

Description:

When using the lte or gte rules with a field, the custom attribute names aren't being used.

Expected:

The maximum price must be greater than or equal minimum price.

Actual result:

The maximum price must be greater than or equal price.min.

Notice that it not says:

The price.max must be greater than or equal price.min.

Steps To Reproduce:

$rules = [
    'price' => ['nullable', 'array', 'size:2'],
    'price.min' => ['nullable', 'numeric', 'min:1', 'lte:price.max'],
    'price.max' => ['nullable', 'numeric', 'min:1', 'gte:price.min'],
];

// validation attributes

return [
    'price.min' => 'minimum price',
    'price.max' => 'maximum price',
];
@driesvints
Copy link
Member

Does it work when you use price_min instead? I think this is probably conflicting with dot notation.

@tillkruss
Copy link
Collaborator Author

tillkruss commented Aug 8, 2019

price_min isn't a field on the request. This is the HTML:

<input type="number" step="any" min="1" name="price[min]">
<input type="number" step="any" min="1" name="price[max]">

@driesvints
Copy link
Member

driesvints commented Aug 8, 2019

@tillkruss I'm trying to figure out if the dots are indeed the issue here so can you try with the price_min naming?

@tillkruss
Copy link
Collaborator Author

tillkruss commented Aug 8, 2019

I was just testing that, you're too fast. Same error:

The maximum price must be greater than or equal price_min.

I'll try simple fields, one moment.

@tillkruss
Copy link
Collaborator Author

tillkruss commented Aug 8, 2019

If the field has a value then I get a better message:

The maximum price must be greater than or equal 20.

But if the field is empty, either as array price[min] or a regular field price_min I get the same message:

The maximum price must be greater than or equal price.min.
The maximum price must be greater than or equal price_min.

In my particular case, the min and max fields are nullable, because it's not a required field.

@driesvints
Copy link
Member

What if you try this in your validation translation file?

return [
    'price' => [
        'min' => 'minimum price',
        'max' => 'maximum price',
    ],
];

@tillkruss
Copy link
Collaborator Author

I did try that initially, but it didn't work:

return [
    'attributes' => [
        'price.min' => 'minimum price',
        'price.max' => 'maximum price',
        'price' => [
            'min' => 'minimum price',
            'max' => 'maximum price',
        ],
    ],
];

@driesvints
Copy link
Member

driesvints commented Aug 9, 2019

Okay, just tested this with the setup from the original comment and everything works as expected:

master $ http --json POST test-app.test/foo price:='{"min":5, "max":4}'                                                                                                      ~/Sites/test-app
HTTP/1.1 422 Unprocessable Entity
Cache-Control: no-cache, private
Connection: keep-alive
Content-Type: application/json
Date: Fri, 09 Aug 2019 11:58:09 GMT
Server: nginx/1.17.0
Set-Cookie: laravel_session=eyJpdiI6IkxFRXBTQlMxSzIwZW9WRFlCMnd4b2c9PSIsInZhbHVlIjoiRjhmVExTd29oeDc2UmQ3TGlKYmxHbm1yT05oMmNKTzFuWGRWTXNWQzZERWhUcDlWZmlLbnpsVVVqR2RBc2FXRCIsIm1hYyI6ImU1ODVhMDY2ZGQ1NjlkMDUwYjQ3ZGJhYjBhMDYyY2NmNjQwMzQ4MjJlNzdjYTVmNjg3ODA5NTJmNDU5Mzg4NmMifQ%3D%3D; expires=Fri, 09-Aug-2019 11:59:09 GMT; Max-Age=60; path=/; httponly
Transfer-Encoding: chunked
X-Powered-By: PHP/7.3.5

{
    "errors": {
        "price.max": [
            "The maximum price must be greater than or equal 5."
        ]
    },
    "message": "The given data was invalid."
}

So not sure if we're missing anything here?

@driesvints
Copy link
Member

I do think the default should be The :attribute must be greater than or equal to :value. (notices the extra "to").

@tillkruss
Copy link
Collaborator Author

Correct, now try it with min or max missing:

http --json POST test-app.test/foo price:='{"min":null, "max":4}' 

@driesvints
Copy link
Member

I can reproduce it now. Not sure what's going wrong here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
2 participants