Skip to content

[BUG] Uniqueness validator incorrectly fails validation on update #1527

@pgasiorowski

Description

@pgasiorowski

With the following model $id is the primary key and $name must be unique within $project_id

use Phalcon\Mvc\Model\Validator\Uniqueness;

class Tags extends \Phalcon\Mvc\Model {
  public $id;
  public $project_id;
  public $name;

  public function initialize() {
    $this->setSource('table_tags');
  }

  public function validation()
  {
    $this->validate(new Uniqueness([
      'field' => ['project_id', 'name'],
      'message' => 'The tag already exists in selected project'
    ]));

    return ($this->validationHasFailed() != true);
  }
}

Lets say I have the following data:

$data = array(
    'id' => 1,
    'project_id' => 1,
    'name' => 'Some tag',
);

It works fine when creating a new record with Tags::save(), but then Tags::update() is failing the validation. There might be something I'm missing here or the Uniqueness validation is not excluding current $id from the check.

Tested on:
Phalcon 1.3.0 and 1.2.4
PHP 5.4.18 and 5.5.5
Windows 7

Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions