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
[Form] Created FormErrorBag #9099
Conversation
*/ | ||
protected $errors = array(); | ||
|
||
private $_invalid = false; |
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.
Please remove the leading underscore. We don't use it in Symfony
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 used it because invalid is not a real property of the object, it's just a helper property. But I'll remove it
I've fixed the tests, added some things for BC (see below) and added the last missing features. I think it's ready for review @stof @bschussek
|
* | ||
* @author Wouter J <wouter@wouterj.nl> | ||
* | ||
* @since v2.4.0 |
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.
not sure if this is needed...
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.
Yes, but you can change v2.4.0
to 2.4
.
I think your ArrayAccess is not BC as the offset does not concern only errors but also child error collections. So getting |
*/ | ||
public function addCollection($formName, $collection) | ||
{ | ||
// BC with using arrays with errors |
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.
Why do we need to support arrays here?
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.
some tests are failing without this. BTW, do you know why the tests are failing now? It seems there is another BC bug...
fixed all comments. I know I'm one day after the feature freeze, but I'd like to see this included in 2.4... |
@wouterj You're lucky as I'm still working on merging the last PRs for 2.4... let's wait for @bschussek thoughts first. |
return array_keys($this->errors); | ||
} | ||
|
||
public function __toString() |
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.
missing phpdoc for many methods
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.
do we really want to have PHPdoc for PHP build-in methods, like __toString etc?
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.
no
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.
yes because otherwise it's not documented what the string representation actually returns
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.
and this is one of the main points of FormErrorBag
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.
also its not doccumented that it accepts a parameter
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.
the string representation wasn't documented on getErrorsAsString() too, I don't really know why we should do it here. And the parameter is only for internal use.
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.
where is the parameter used internally?
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.
It's used to indent the children forms with 4 spaces
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.
ok as long as its not used outside the class
2.4.0 | ||
----- | ||
|
||
* changed getErrors() to return a FormErrorBag, which is countable, |
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.
missing bc break info
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.
fixed
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.
what i also mean is that it should be tagged with [BC BREAK], see other changelog entries
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.
also getErrors now returns all errors, whereas it previously returned only direct child errors, or?
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.
@Tobion no it does not. It returns a RecusiveIterator instead of an array. So you can have all errors if you wrap it in a RecursiveIteratorIterator
, but the default iteration still gives you only the form errors only (and not the direct child errors as you said in your comment)
@fabpot @bschussek Any chance of getting this in |
} | ||
|
||
return $errors; | ||
return (string) $this->getErrors(); |
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.
it should be $this->getErrors()->__toString($level)
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.
why? Level should be the default, 0, and explicitly calling a magic method seems wrong to me. But if there is any reason why this is better or if this is the CS, I'm happy to change this
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.
@wouterj because if you ignore the $level
argument, you are not providing BC totally (even if it was considered as an internal argument, I'm sure some people were using it)
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.
fixed
ping |
ping @fabpot @bschussek I guess this won't make 2.4... |
is it possible to convert this bag to array like
? Where key is form field full name and value is list of the errors for field. It is useful for ajax responses (bind tooltips with errors to fields) |
@Koc no, that's not possible with the method itself. But with some array changer functions, you can create something like that. |
} | ||
|
||
return $errors; | ||
} | ||
|
||
/** | ||
* @deprecated Deprecated since version 2.4, to be removed in 3.0. Covert |
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.
should be 2.5 and Convert
instead of Covert
.
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.
Also, can you put everything on the same line?
@wouterj Besides some small typos, looks good to me. Can you also rebase on current master and add a ticket for the documentation? Thanks. |
This is fully BC with returning an array and it deprecates Form::getErrorsAsString()
@fabbot everything is now fixed and rebased |
doc issue: symfony/symfony-docs#3397 |
apparently, tests are broken |
I'm now stuck... Maybe someone can help me? When using the RecursiveIterator directly, it should only return the However, in the Imagine array(
[0] => FormError instance (...),
[child] => FormErrorBag instance (...)
) When iterating, the following things happen:
In the 5th step it should not have returned the How can we fix this, so that the 5th step isn't executed? We can't add a test on instance in the |
From what I can tell, you really have two different cases. And you should not be trying to make both work with the same iterator instance. The original iterator is a FormErrorBag that may recursively contain other FormErrorBag instances. I'll call it Case A is looking at the top level errors only. In simple terms, this means Case B is recursively walking over over the nested So what you really want is |
After looking at the current implementation I can also pinpoint the problem. It is calling What it comes down to is this. Currently, you are trying to do the recursion inside |
I think you right @igorw we can't mix them into one class. It would be nice if we could though... However, we have to be BC with an array, so I propose to have a @bschussek I'd love to hear your opinion about this. 🎆 |
Closing in favor of #9914 |
The only BC break in this PR is checking it with is_array. Iterating
over the array works just the same as previously:
RecursiveIterator
results in onlyiterating over the errors of the current form.
RecursiveIteratorIterator
results in iterating overall errors recursively.
Todo
__toString()
support and deprecatingForm::getErrorsAsString()
.Form:getErrors()