Different behavior of similar methods in validation
The getMessages methods of Models and Forms return different data types. Models return an array, and forms return object like Phalcon\Validation\Message\Group which any more not to transfer in Flash. Whether these methods to return identical data type owe?
Since Phalcon 2.1.x Phalcon\Mvc\Model\Validation is deprecated in favor of Phalcon\Validation.
The functionality of both components is merged into one, allowing us to reduce the codebase while offering the same functionality as before.
Previously validations were implemented as follows:
namespace Invo\Models;
use Phalcon\Mvc\Model;
use Phalcon\Mvc\Model\Validator\Email as EmailValidator;
use Phalcon\Mvc\Model\Validator\Uniqueness as UniquenessValidator;
class Users extends Model
{
public function validation()
{
$this->validate(
new EmailValidator(
[
'field' => 'email',
]
)
);
$this->validate(
new UniquenessValidator(
[
'field' => 'username',
'message' => 'Sorry, That username is already taken',
]
)
);
if ($this->validationHasFailed() == true) {
return false;
}
}
}
Introducing Phalcon\Validation, you will need to change the above to:
namespace Invo\Models;
use Phalcon\Mvc\Model;
use Phalcon\Validation;
use Phalcon\Validation\Validator\Email as EmailValidator;
use Phalcon\Validation\Validator\Uniqueness as UniquenessValidator;
class Users extends Model
{
public function validation()
{
$validator = new Validation();
$validator->add(
'email',
new EmailValidator()
);
$validator->add(
'username',
new UniquenessValidator(
[
'message' => 'Sorry, That username is already taken',
]
)
);
return $this->validate($validator);
}
}
You will agree that this change makes the code much smaller and easier to read.
Unfortunately, you did not understand my question, I think.
Phalcon\Mvc\Model:
public function getMessages(var filter = null) -> <MessageInterface[]> { //... }
Phalcon\Forms\Form:
public function getMessages(boolean byItemName = false) -> <Group> { //... }
Same as intended methods return different types of data. I just think it's wrong.
Unfortunately, you did not understand my question, I think.
Ah, yes. I inattentively read
Well, I copypasted the example from the Blog. There is a misspelling in the Blog's example itself. I fixed my answer. In general I think your comment is logical. We should have a common interface for both cases.
Thank you for contributing to this issue. As it has been 90 days since the last activity, we are automatically closing the issue. This is often because the request was already solved in some way and it just wasn't updated or it's no longer applicable. If that's not the case, please feel free to either reopen this issue or open a new one. We will be more than happy to look at it again! You can read more here: https://blog.phalconphp.com/post/github-closing-old-issues