Skip to content

Commit

Permalink
Model validation now correctly adds code to message
Browse files Browse the repository at this point in the history
  • Loading branch information
Jurigag committed Mar 3, 2017
1 parent d8e4219 commit ca7255b
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
- Added `prepend` parameter to `Phalcon\Loader::register` to specify autoloader's loading order to top most
- Fixed `Phalcon\Session\Bag::remove` to initialize the bag before removing a value [#12647](https://github.com/phalcon/cphalcon/pull/12647)
- Fixed `Phalcon\Mvc\Model::getChangedFields` to correct detect changes from NULL to Zero [#12628](https://github.com/phalcon/cphalcon/issues/12628)
- Fixed `Phalcon\Mvc\Model::validate` to correctly set code message [#12645](https://github.com/phalcon/cphalcon/issues/12645)

# [3.0.4](https://github.com/phalcon/cphalcon/releases/tag/v3.0.4) (2017-02-20)
- Fixed Isnull check is not correct when the model field defaults to an empty string. [#12507](https://github.com/phalcon/cphalcon/issues/12507)
Expand Down
4 changes: 3 additions & 1 deletion phalcon/mvc/model.zep
Original file line number Diff line number Diff line change
Expand Up @@ -1505,7 +1505,9 @@ abstract class Model implements EntityInterface, ModelInterface, ResultInterface
new Message(
message->getMessage(),
message->getField(),
message->getType()
message->getType(),
null,
message->getCode()
)
);
}
Expand Down
63 changes: 63 additions & 0 deletions tests/_data/models/Validation/Robots.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

namespace Phalcon\Test\Models\Validation;

use Phalcon\Mvc\Model;
use Phalcon\Mvc\Model\Resultset\Simple;
use Phalcon\Test\Models\RobotsParts;
use Phalcon\Validation;
use Phalcon\Validation\Validator\PresenceOf;

/**
* \Phalcon\Test\Models\Validation\Robots
*
* @method static int countByType(string $type)
* @method static Simple findByType(string $type)
* @method static Robots findFirstById(string | int $id)
*
* @copyright 2011-2017 Phalcon Team
* @link https://phalconphp.com
* @author Andres Gutierrez <[email protected]>
* @author Nikolaos Dimopoulos <[email protected]>
* @package Phalcon\Test\Models\Validation
*
* The contents of this file are subject to the New BSD License that is
* bundled with this package in the file docs/LICENSE.txt
*
* If you did not receive a copy of the license and are unable to obtain it
* through the world-wide-web, please send an email to [email protected]
* so that we can send you a copy immediately.
*/
class Robots extends Model
{
public function initialize()
{
$this->hasMany(
'id',
RobotsParts::class,
'robots_id',
[
'foreignKey' => true,
'reusable' => false,
'alias' => 'parts',
]
);
}

public function validation()
{
$validation = new Validation();
$validation->add(
'name',
new Validation\Validator\StringLength(
[
'min' => '7',
'max' => '50',
'code' => 20,
]
)
);

return $this->validate($validation);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Phalcon\Test\Models;
namespace Phalcon\Test\Models\Validation;

use Phalcon\Mvc\Model;
use Phalcon\Validation;
Expand All @@ -14,7 +14,7 @@
use Phalcon\Validation\Validator\StringLength;

/**
* \Phalcon\Test\Models\Subscriptores
* \Phalcon\Test\Models\Validation\Subscriptores
*
* @property int id
* @property string email
Expand All @@ -25,7 +25,7 @@
* @link http://www.phalconphp.com
* @author Andres Gutierrez <[email protected]>
* @author Serghei Iakovlev <[email protected]>
* @package Phalcon\Test\Models
* @package Phalcon\Test\Models\Validation
*
* The contents of this file are subject to the New BSD License that is
* bundled with this package in the file docs/LICENSE.txt
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/Mvc/Model/Helpers/Validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use UnitTester;
use Phalcon\Db\RawValue;
use Phalcon\Mvc\Model\Message;
use Phalcon\Test\Models\Subscriptores;
use Phalcon\Test\Models\Validation\Subscriptores;

class Validation
{
Expand Down
26 changes: 26 additions & 0 deletions tests/unit/Mvc/ModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Phalcon\Test\Unit\Mvc;

use Helper\ModelTrait;
use Phalcon\Mvc\Model\Message;
use Phalcon\Test\Models\Users;
use Phalcon\Cache\Backend\Apc;
use Phalcon\Test\Models\Robots;
Expand All @@ -19,6 +20,7 @@
use Phalcon\Test\Models\BodyParts\Body;
use Phalcon\Test\Models\News\Subscribers;
use Phalcon\Test\Models\AlbumORama\Albums;
use Phalcon\Test\Models\Validation;

/**
* \Phalcon\Test\Unit\Mvc\ModelTest
Expand Down Expand Up @@ -613,4 +615,28 @@ function () {
}
);
}


/**
* Tests setting code in message from validation messages
*
* @issue 12645
* @author Wojciech Ślawski <[email protected]>
* @since 2017-03-03
*/
public function testIssue12645()
{
$this->specify(
"Issue #12645 is not fixed",
function () {
$robots = new Validation\Robots();
$robots->name = "asd";
expect($robots->create())->false();
/** @var Message $message */
$message = $robots->getMessages()[0];
expect($message)->isInstanceOf(Message::class);
expect($message->getCode())->equals(20);
}
);
}
}

0 comments on commit ca7255b

Please sign in to comment.