Skip to content
This repository has been archived by the owner on Jan 31, 2020. It is now read-only.

Validator result interface proposal #24

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions src/ResultInterface.php
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();
Copy link
Contributor

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.

Copy link
Member

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.

Copy link
Member Author

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.

Copy link
Member

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.

Copy link
Member Author

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 and getField($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 :)

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 :)

Copy link
Contributor

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

Copy link
Member Author

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

Copy link
Member Author

Choose a reason for hiding this comment

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

if - else

Copy link
Member Author

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.


/**
* Returns the list of error violations in a machine readable format.
*
* @return array
Copy link
Member Author

Choose a reason for hiding this comment

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

I think we can constraint this to integer or string types

*/
public function getErrorCodes();

/**
* Get the error messages (with the variables replaced) associated with the validation result
Copy link
Member Author

Choose a reason for hiding this comment

The 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();
}
46 changes: 46 additions & 0 deletions src/Translator/TranslatableMessageInterface.php
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();
Copy link
Member Author

Choose a reason for hiding this comment

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

This method name may return two types of message.

  • Messages with placeholders
  • Messages without placeholders.

Copy link
Contributor

Choose a reason for hiding this comment

The 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.
Copy link
Member Author

Choose a reason for hiding this comment

The 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.

Choose a reason for hiding this comment

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

I'm a bit confused by this, why "without translation"?
I also - just going by the names of the functions - don't know how I would render the message itself. I assume it'd be

$res = $foo->getValidationResult(); // pseudo

foreach ($res->getMessages() as $msg) {
    echo sprintf("<li>%s</li>", (string) $msg);
}

Alternatively to (string) $msg I'd like to have a clear getter that will give me the error message. Something like $msg->getMessage() or $msg->getText() or $msg->getTranslatedMessage().

All we have in the interface for this is getMessageTemplate() (as far as I can understand it) which in my opinion isn't that clear.

Copy link
Member

Choose a reason for hiding this comment

The 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.

Copy link
Member Author

Choose a reason for hiding this comment

The 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.

Choose a reason for hiding this comment

The 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 TranslatableMessageInterface then. Furthermore the messages would be returned from the validator. So the validator needs to know the textdomain as well so it could pass it into the message object. I feel that this is passing along metadata that may not even be needed.

So This would bring us to what @weierophinney proposed (the way I understand it at least): skip translation.

foreach ($messages as $msg) {
    $translator->translate((string) $msg);
}

Which I feel is perfectly fine. And if you need higher level translation you would probably do something along the lines of

foreach ($messages as $msg) {
    $translator->translate($msg->getMessageTemplate(), $msg->getMessageVariables());
}

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:
Why the need for anything translation related in those messages ?

Copy link
Member Author

Choose a reason for hiding this comment

The 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.

Copy link
Member

Choose a reason for hiding this comment

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

Do you all agree with constraint __toString to only interpolation?

Yes.

*
* @return string
*/
public function __toString();
}