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

[Validator] Fixed string conversion in constraint violations #10687

Merged
merged 12 commits into from Jul 30, 2014

Conversation

webmozart
Copy link
Contributor

Q A
Bug fix? yes
New feature? no
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets #10675
License MIT
Doc PR -

}

if (is_array($value)) {
return 'Array';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe add the array size like Array(2) since Object and Resource also add further data

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't really like that writing, since Array(2) is the output of print_r when an array contains only the value 2 and might cause confusion.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

print_r returns Array ( [0] => 2 )

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still don't see the usefulness of this return value. It should format the value, i.e. the contents, but it basically returns the same as formatTypeOf. It returns the type and not the contents of the array. So it does not add anything new. So ideally it should output a concise representation of the array like ['foo', 1, 'Object(Author)']. Or at least the array size as I suggested above.

Imagine the this is used in the CountValidator.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this value needs to be very useful. In most cases, validation messages are displayed to the end user. Displaying something like ['foo', 1, 'Object(Author)'] to an average end users frightens them because they think something is broken.

Developers can always call $violation->getInvalidValue() to analyze the value in detail.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Resource ID and object class are also implementation detail not intended for end users.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with both of you @webmozart and @Tobion!

If this is displayed to the end users, we should not be too precise; so the resource id is not needed, but I would even say that displaying array, object, or resource to the end user looks weird as he probably did not enter that in a form value anyway.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I will change these outputs to just "array", "object" and "resource" then. I agree that this doesn't help end users very much. I'd even go further to say that messages should never include the value if that value is one of these types. If developers choose to include the value in the message anyway, the displayed information will be minimal.

@webmozart
Copy link
Contributor Author

Updated.

protected function valuesToString(array $values, $formatDates = false)
{
foreach ($values as $key => $value) {
$values[$key] = $this->valueToString($value, $formatDates);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indexing by $key should not be necessary

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you clarify what you mean?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nvm, didn't see you replace the interated array values. Maybe using another array like $convertedValues[] = ... would be cleaner.

@webmozart
Copy link
Contributor Author

I now introduced calls to formatValue() (former valueToString()) everywhere that it makes sense to me.

*
* @return string
*/
protected function formatValues(array $values, $formatDates = false)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in formatValue the parameter is named $prettyDateTime. Seems incosistent at the moment.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Fixed

webmozart added a commit to webmozart/symfony that referenced this pull request Jul 28, 2014
…e is an array, object or resource

This was decided in the discussion of symfony#10687.
@webmozart
Copy link
Contributor Author

Updated.

cleentfaar pushed a commit to cleentfaar/symfony that referenced this pull request Jul 29, 2014
cleentfaar pushed a commit to cleentfaar/symfony that referenced this pull request Jul 29, 2014
}

/**
* Returns a string representation of the value.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add some more explanations about when/why/how this is ever called? Something along the lines of:

"This method is used when a constraint error message contains the {{ value }} placeholder. By default, Symfony never includes it, but the developer can override those error messages. It's up to the developer to make sure it actually makes sense to include such string representation of submitted values in error messages."

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@fabpot
Copy link
Member

fabpot commented Jul 30, 2014

👍

1 similar comment
@Tobion
Copy link
Member

Tobion commented Jul 30, 2014

👍

@webmozart webmozart merged commit 32ae95b into symfony:2.3 Jul 30, 2014
webmozart added a commit that referenced this pull request Jul 30, 2014
…ns (eagleoneraptor, webmozart)

This PR was merged into the 2.3 branch.

Discussion
----------

[Validator] Fixed string conversion in constraint violations

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #10675
| License       | MIT
| Doc PR        | -

Commits
-------

32ae95b [Validator] Added more detailed inline documentation
08ea6d3 [Validator] Removed information from the violation output if the value is an array, object or resource
d6a783f [Validator] Renamed valueToString() to formatValue(); added missing formatValue() calls
71897d7 [Validator] Fixed CS
cea4155 [Validator] Fixed date-to-string conversion tests to match ICU 51
5aa7e6d [Validator] Added "{{ value }}" parameters where they were missing
f329552 [Validator] Simplified and explained the LuhnValidator
bff09f2 [Validator] Simplified IssnValidator
224e70f [Validator] Fixed and simplified IsbnValidator
fd58870 [Validator] Simplified IBAN validation algorithm
97243bc [Validator] Fixed value-to-string conversion in constraint violations
75e8815 [Validator] Fix constraint violation message parameterization
@webmozart webmozart deleted the issue10677 branch August 4, 2014 14:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants