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

[4.0.0] Collections now use the Validation component #12376

Merged
merged 1 commit into from
Oct 19, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions CHANGELOG-4.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@
- Change catch Exception to Throwable [#12288](https://github.com/phalcon/cphalcon/issues/12288)
- Removed deprecated methods (`Cli\Console::addModules()`, `Debug::getMajorVersion()`, `Http\Request::isSoapRequested()`, `Http\Request::isSecureRequest()`, `Mvc\Model\Criteria::addWhere()`, `Mvc\Model\Criteria::order()`, `Validation\Validator::isSetOption()`, `Security::hasLibreSsl()`, `Security::getSslVersionNumber()`).
- Renamed `Http\RequestInterface::isSoapRequested()` to `isSoap()` and `Http\RequestInterface::isSecureRequest()` to `isSecure()`.
- Collections now use the Validation component [#12376](https://github.com/phalcon/cphalcon/pull/12376)
116 changes: 43 additions & 73 deletions phalcon/mvc/collection.zep
Original file line number Diff line number Diff line change
Expand Up @@ -628,42 +628,24 @@ abstract class Collection implements EntityInterface, CollectionInterface, Injec
* Executes validators on every validation call
*
*<code>
* use Phalcon\Mvc\Model\Validator\ExclusionIn as ExclusionIn;
*
* class Subscriptors extends \Phalcon\Mvc\Collection
* {
* public function validation()
* {
* // Old, deprecated syntax, use new one below
* $this->validate(
* new ExclusionIn(
* [
* "field" => "status",
* "domain" => ["A", "I"],
* ]
* )
* );
*
* if ($this->validationHasFailed() == true) {
* return false;
* }
* }
* }
*</code>
*
*<code>
* use Phalcon\Validation\Validator\ExclusionIn as ExclusionIn;
* use Phalcon\Mvc\Collection;
* use Phalcon\Validation;
* use Phalcon\Validation\Validator\ExclusionIn;
*
* class Subscriptors extends \Phalcon\Mvc\Collection
* class Subscriptors extends Collection
* {
* public function validation()
* {
* $validator = new Validation();
* $validator->add("status",
*
* $validator->add(
* "status",
* new ExclusionIn(
* [
* "domain" => ["A", "I"]
* "domain" => [
* "A",
* "I",
* ],
* ]
* )
* );
Expand All @@ -673,73 +655,61 @@ abstract class Collection implements EntityInterface, CollectionInterface, Injec
* }
*</code>
*/
protected function validate(var validator)
protected function validate(<ValidationInterface> validator) -> boolean
{
var messages, message;

if validator instanceof Model\ValidatorInterface {
if validator->validate(this) === false {
for message in validator->getMessages() {
let this->_errorMessages[] = message;
}
}
} elseif validator instanceof ValidationInterface {
let messages = validator->validate(null, this);

// Call the validation, if it returns not the boolean
// we append the messages to the current object
if typeof messages != "boolean" {

messages->rewind();

// for message in iterator(messages) {
while messages->valid() {

let message = messages->current();

this->appendMessage(
new Message(
message->getMessage(),
message->getField(),
message->getType()
)
);

messages->next();
}

// If there is a message, it returns false otherwise true
return !count(messages);
}
let messages = validator->validate(null, this);

// Call the validation, if it returns not the boolean
// we append the messages to the current object
if typeof messages == "boolean" {
return messages;
} else {
throw new Exception("You should pass Phalcon\\Mvc\\Model\\ValidatorInterface or Phalcon\\ValidationInterface object");
}

for message in iterator(messages) {
this->appendMessage(
new Message(
message->getMessage(),
message->getField(),
message->getType(),
null,
message->getCode()
)
);
}

// If there is a message, it returns false otherwise true
return !count(messages);
}

/**
* Check whether validation process has generated any messages
*
*<code>
* use Phalcon\Mvc\Model\Validator\ExclusionIn as ExclusionIn;
* use Phalcon\Mvc\Collection;
* use Phalcon\Validation;
* use Phalcon\Validation\Validator\ExclusionIn;
*
* class Subscriptors extends \Phalcon\Mvc\Collection
* class Subscriptors extends Collection
* {
* public function validation()
* {
* $this->validate(
* $validator = new Validation();
*
* $validator->validate(
* "status",
* new ExclusionIn(
* [
* "field" => "status",
* "domain" => ["A", "I"],
* "domain" => [
* "A",
* "I",
* ],
* ]
* )
* );
*
* if ($this->validationHasFailed() == true) {
* return false;
* }
* return $this->validate($validator);
* }
* }
*</code>
Expand Down
99 changes: 0 additions & 99 deletions phalcon/mvc/model/validator.zep

This file was deleted.

96 changes: 0 additions & 96 deletions phalcon/mvc/model/validator/email.zep

This file was deleted.

Loading