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

[6.x] Allow formatting an implicit attribute using a closure #31246

Merged
merged 5 commits into from Jan 27, 2020

Conversation

@themsaid
Copy link
Member

themsaid commented Jan 27, 2020

This PR adds a setImplicitAttributesFormatter method to the validator that allows the instance to output a message that says:

age at line 1 must be an integer

instead of

0.age must be an integer
validator(
    [['age' => 'thirty']],
    ['*.age' => 'integer']
)->setImplicitAttributesFormatter(function ($attribute) {
    [$line, $attribute] = explode('.', $attribute);

    return sprintf('%s at line %d', $attribute, $line + 1);
})->validate();
themsaid added 2 commits Jan 27, 2020
* @return $this
*/
public function setImplicitAttributesFormatter(callable $formatter)
public function setImplicitAttributesFormatter($formatter)

This comment has been minimized.

Copy link
@GrahamCampbell

GrahamCampbell Jan 27, 2020

Member

You can keep the typehint (callable $formatter = null)

@themsaid themsaid closed this Jan 27, 2020
@themsaid themsaid reopened this Jan 27, 2020
@taylorotwell taylorotwell merged commit 86dc9df into laravel:6.x Jan 27, 2020
2 checks passed
2 checks passed
continuous-integration/styleci/pr The analysis has passed
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
@MarceauKa

This comment has been minimized.

Copy link

MarceauKa commented Jan 28, 2020

Hi @themsaid , I'm just reading the 6.13 changelog. Can you help me to understand this new behavior with a real case usage? 😇

@PovilasKorop

This comment has been minimized.

Copy link

PovilasKorop commented Jan 29, 2020

Hi @themsaid! Great feature it seems, I want to shoot a video about it, but I don't understand - is it possible to use it with FormRequest classes? Or what would be the syntax with $this->validate() in Controller?

@themsaid

This comment has been minimized.

Copy link
Member Author

themsaid commented Jan 29, 2020

@PovilasKorop You'll want to use:

function withValidator($validator){
    $validator->setImplicitAttributesFormatter(function ($attribute) {
        [$line, $attribute] = explode('.', $attribute);

        return sprintf('%s at line %d', $attribute, $line + 1);
   });
}

@MarceauKa the example above is a real use case :)

@PovilasKorop

This comment has been minimized.

Copy link

PovilasKorop commented Jan 29, 2020

@themsaid thanks, it's working, tried it with a bit changed example, will shoot a video tomorrow.
For anyone else, here's the code:

class StoreOrderRequest extends FormRequest
{
    public function authorize()
    {
        return true;
    }

    public function rules()
    {
        return [
            'customer_name' => 'required',
            'products.*.product_id' => 'required',
        ];
    }

    function withValidator($validator){
        $validator->setImplicitAttributesFormatter(function ($attribute) {
            [$field, $line] = explode('.', $attribute);
            if ($field == 'products') {
                return 'product number ' . ($line+1);
            }
        });
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

5 participants
You can’t perform that action at this time.