Skip to content

Commit

Permalink
Collections now use the Validation component.
Browse files Browse the repository at this point in the history
  • Loading branch information
SidRoberts committed Sep 3, 2017
1 parent 6ab801d commit e8ff08a
Show file tree
Hide file tree
Showing 15 changed files with 74 additions and 1,508 deletions.
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

0 comments on commit e8ff08a

Please sign in to comment.