-
-
Notifications
You must be signed in to change notification settings - Fork 835
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
Use laravel validator to replace avatar validation error params #2946
Conversation
When we switched to intl icu message format, the translation parameters have become required to be in the format `{param}`, so we can no longer use the translator here. This PR switches to consistently rely on the laravel validator to make the replacements, just like with all other validation errors in the rest of the application.
[ci skip] [skip ci]
Why don't we change the format on the validation.yml strings instead? |
The strings in Hence why I opted to just rely on the laravel validator as well, just like we do by default, because we're now in a situation where the translator requires |
I don't know, it still feels like a lot of fuss to get something working. Can't we override the translator instance in the validator to use the ICU one? It's a long shot, but it would result in much cleaner translation files. Now we have to explain why one file behaves differently than the rest. |
The laravel validator already uses the flarum translator But it doesn't use the translator to replace the parameters, it uses it just to get the raw message string, then it makes the replacements on its own, hence the We could also just do manual replacement here instead of relying on the laravel validator for that, I dislike recreating existing logic, but since we're only using three validation strings here, they would all be straightforward replacements. That would be more simple and less of a hustle, and fine for this particular spot, the avatar validator is the only one that does this custom validation anyway. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there no way that we could redo this to avoid custom validation instead? Then we wouldn't have a special case. Could we first extract the attributes we need, then run those through the regular validator?
I haven't deeply looked into it yet, but that doesn't seem to be an option by looking at the relevant commit that introduced the custom avatar validation 1cbb2a3#diff-f91b182a730d6646f10d001c0afd69d4042b5eedf5a1c0355e28452855263a18 |
Instead of trying to run the file directly / copied through the validator, we could preprocess the avatar to extract relevant fields: |
Trying to get back to this
that doesn't solve the problem actually though, if we extract those values and run a validator on them instead then the validation error messages generated by the laravel validator will not be descriptive of what's actually going on. $extractedValues = [
'status' => $file->getError() === UPLOAD_ERR_OK,
'size' => $file->getSize(),
'mimetype' => $this->getMimeType($file),
];
$validator->validate($extractedValues, [
'status' => 'accepted',
'size' => 'lte:2048',
'mimetype' => Rule::in($this->getAllowedTypes()),
]); the error messages would look something like:
Which for some works but then not really. That said looking at this again, I don't like using the laravel validator to make the replacements either, so we could always
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I understand what you mean now, this looks good to me. One thing though: it would be nice to add a brief comment in the code explaining why we've made this change
Fixes #2933
Changes proposed in this pull request:
When we switched to intl icu message format, the translation parameters
have become required to be in the format
{param}
, so we can no longeruse the translator here.
This PR switches to consistently rely on the laravel validator to make
the replacements, just like with all other validation errors in the rest
of the application.
Reviewers should focus on:
Is this the best way to go about this ?
Confirmed
composer test
).