-
Notifications
You must be signed in to change notification settings - Fork 136
Validator result interface proposal #24
base: develop
Are you sure you want to change the base?
Changes from 2 commits
156d515
a1db762
2b28ea1
96c9d3a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
/** | ||
* Zend Framework (http://framework.zend.com/) | ||
* | ||
* @link http://github.com/zendframework/zf2 for the canonical source repository | ||
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) | ||
* @license http://framework.zend.com/license/new-bsd New BSD License | ||
*/ | ||
|
||
namespace Zend\Validator; | ||
|
||
/** | ||
* This interface provide methods for to know the result of a validation and the reasons when the result is not valid | ||
* | ||
* This interface resolve the following questions: | ||
* - Is the result valid or invalid? | ||
* - If not, Why is not valid? | ||
*/ | ||
interface ResultInterface | ||
{ | ||
/** | ||
* Is the validation result valid? | ||
* | ||
* @return bool | ||
*/ | ||
public function isValid(); | ||
|
||
/** | ||
* Is the validation result invalid? | ||
* | ||
* @return bool | ||
*/ | ||
public function isNotValid(); | ||
|
||
/** | ||
* Returns the list of error violations in a machine readable format. | ||
* | ||
* @return array | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we can constraint this to |
||
*/ | ||
public function getErrorCodes(); | ||
|
||
/** | ||
* Get the error messages (with the variables replaced) associated with the validation result | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing a description about each array key must be the associated error code. |
||
* | ||
* @return Translator\TranslatableMessageInterface[] | ||
*/ | ||
public function getMessages(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
/** | ||
* Zend Framework (http://framework.zend.com/) | ||
* | ||
* @link http://github.com/zendframework/zf2 for the canonical source repository | ||
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) | ||
* @license http://framework.zend.com/license/new-bsd New BSD License | ||
*/ | ||
|
||
namespace Zend\Validator\Translator; | ||
|
||
/** | ||
* This interface provide methods for extract the translatable parts of a message. | ||
*/ | ||
interface TranslatableMessageInterface | ||
{ | ||
/** | ||
* Returns the message to translate. | ||
* | ||
* The message may include placeholders for message variable interpolation. | ||
* | ||
* @return string | ||
*/ | ||
public function getMessageTemplate(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This method name may return two types of message.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So this means that sometimes, the messages are directly exploitable with this methods (when no placeholder), and sometimes, I must use another object to translate them? |
||
|
||
/** | ||
* Returns the variables to be replaced in the message template placeholders. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Variables may should have a cardinal number associated to the value for proper translation of plurals /cc @DASPRiD |
||
* | ||
* @return array | ||
*/ | ||
public function getMessageVariables(); | ||
|
||
/** | ||
* Returns the translation domain namespace. | ||
* | ||
* @return string | ||
*/ | ||
public function getTranslationDomain(); | ||
|
||
/** | ||
* Returns the message without translation and with the placeholders replaced by the variables. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm a bit confused by this, why "without translation"?
Alternatively to All we have in the interface for this is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Having messages translated means the validator needs to be translation aware, in order to inject each result with a translator instance, so that you can then get the translated messages. That's a lot of pointers running around, for something that is (a) not terribly common, and (b) outside the domain of validation entirely. It would be simple to create a helper utility that can give back an altered response instance with translated messages: $results = $validator->validate($data);
if (! $results->isValid()) {
$results = $utility->translate($results); // Returns a new result instance, with translated messages
} We already support message templates in a standard format; the main aspect would be getting that and the message variables to inject. I'm not convinced we need the translation domain, however; that seems like something that the consumer who wants translated messages would provide at the time they request translated messages. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The point is the ResultInterface defines a model, and models are not responsible how to render itself. For the above reason couple with a translator is not in my wishlist. About the need of the translation domain I agree I don't like to have that thing. The point is the result interface does not have any relation with the validator so there is no way of to give a namespace to the translation. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, I understand why translation shouldn't happen inside. However, from a User-PoV the naming then is really confusing. Essentially all I get is a message. I'm able to translate any message by other means, so personally I don't quite see the need to call this So This would bring us to what @weierophinney proposed (the way I understand it at least): skip translation.
Which I feel is perfectly fine. And if you need higher level translation you would probably do something along the lines of
From the user point of view (and the ones of people who write docs), i think this is a pretty good syntax. But with this said my question: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree the name is not the best. I open for ideas. Do you all agree with constraint __toString to only interpolation? Really I don't care if the message is translated or not. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Yes. |
||
* | ||
* @return string | ||
*/ | ||
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.
Why? I'm not sure this is really needed... It adds an additional method for few benefits.
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.
Agreed with @bakura10 - in fact, I just closed another issue that suggested it earlier.
isValid()
is a boolean method; if you want to determine if it returns false, you use the boolean operator (!
). Adding another method is confusing and unnecessary.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 think is expressiveness is very useful.
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 disagree with it being more expressive. It's one more method to implement and maintain, for a trivial boolean condition. Use the boolean operator; it's expressive enough, and it's its sole, well-known purpose.
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 see it close like when you have
getFields
andgetField($name)
with the first method you can do the same operations as the second, but the second is more user friendly.Also the implementation is trivial, the return is just the alternate version of isValid (!isValid)
For me expressiveness is human level of write. (!) not operator is not in the dictionary :)
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.
@ojhaujjwal a reason would be awesome :)
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 don't this is really 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.
Another reason for to have "isNotX" methods is like a way of complete pairs of methods
set - get
add - remove
has - notHas
is - isNot
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.
if - else
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.
We all know if and while are the most basic statements for control and loops but we all like for() and even more foreach()
Also this may help for all those people wants a whitespace after the not (!) operator.