Is there anything like "Uniqueness" validator for a collection?
|
Jun '15 |
10 |
1929 |
0 |
I trying to use : https://github.com/phalcon/dasshy/blob/master/app/models/Users.php#L34 https://github.com/phalcon/dasshy/blob/master/app/validators/Uniqueness.php
But getting : Fatal error: Declaration of Truffula\Validators\Uniqueness::validate() must be compatible with Phalcon\Mvc\Model\ValidatorInterface::validate(Phalcon\Mvc\ModelInterface $record) in /Users/IGonza/git/truffula/app/validators/Uniqueness.php on line 9
namespace xxx\Validators;
use Phalcon\Mvc\Model\Validator,
Phalcon\Mvc\Model\ValidatorInterface,
Phalcon\Mvc\Model\Message;
class Uniqueness extends Validator implements ValidatorInterface
{
/**
* Validates that the record is unique
*
* @param Phalcon\Mvc\Collection $record
* @return boolean
*/
public function validate($record)
{
$field = $this->getOption('field');
$count = $record::count(array(
array($field => $record->readAttribute($field))
));
if ($count > 0) {
$message = $this->getOption('message');
$this->appendMessage(new Message($message, $field));
return false;
}
return true;
}
//public function getMessages() {}
}
I don't think so. Check this : https://github.com/phalcon/dasshy/blob/master/app/validators/Uniqueness.php
I have also similar issue but I have implemented Uniqueness validator it works perfectly on new records. The problem occures when I try to update an entry, validation fires an uniqueness error. It handles operation like create event.
$shelf = Collections\ShelfCollection::findFirst(array(
array('label' => 'my-label')
));
if($shelf) {
$shelf->value = 'changed-value';
if ($shelf->save() == false) {
foreach ($shelf->getMessages() as $message) {
echo $message, "\n";
}
} else {
echo "Shelf value updated successfully!<br />";
}
}
else {
echo 'Shelf label not found';
}
Simply you can take a look to my collection model:
<?php
namespace Collections;
use Validator\Collection\UniqueValidator As Uniqueness,
Phalcon\Mvc\Model\Message as Message;
class ShelfCollection extends \Phalcon\Mvc\Collection {
public $label;
public $value;
public function getSource()
{
return "shelf";
}
public function initialize()
{
// Auto generate _id keys
$this->useImplicitObjectIds(true);
}
public function validation()
{
$this->validate(new Uniqueness(
array(
"field" => "value",
"message" => "Value must be unique"
)
));
$this->validate(new Uniqueness(
array(
"field" => "label",
"message" => "Label must be unique"
)
));
return $this->validationHasFailed() != true;
}
public function beforeSave()
{
// May Apply Here Business Layout Model
}
}
namespace Validator\Collection;
use Phalcon\Mvc\Model\Validator,
Phalcon\Mvc\Model\ValidatorInterface;
class UniqueValidator extends Validator implements ValidatorInterface
{
public function validate($record)
{
$field = $this->getOption('field');
if ($record->count(array("field" => $record->readAttribute($field)))) {
$this->appendMessage("The ".$field." must be unique", $field, "Unique");
return false;
}
return true;
}
}
I guess we need to redevelop the validate method for uniqueness :)
You must add validation logic for updating, because when you update a recored, there is already a record.
You must add validation logic for updating, because when you update a recored, there is already a record.
Yep, I said that in other post. You've to control which UNIQUE fields are "updated" or not (I use a private variable). Then, in the validator, if it has changed, add the validator.
Doesn't work for me
Fatal error: Declaration of app\common\components\validators\Unique::validate() must be compatible with Phalcon\Mvc\Model\ValidatorInterface::validate(Phalcon\Mvc\ModelInterface $record)
But if i use declaration as
public function validate(\Phalcon\Mvc\ModelInterface $record)
I got new error
Catchable fatal error: Argument 1 passed to app\common\components\validators\Unique::validate() must implement interface Phalcon\Mvc\ModelInterface, instance of \Phalcon\Mvc\Collection given
How can i solve this?
I'm also experiencing this issue. Anyone have a solution there?
Doesn't work for me
Fatal error: Declaration of app\common\components\validators\Unique::validate() must be compatible with Phalcon\Mvc\Model\ValidatorInterface::validate(Phalcon\Mvc\ModelInterface $record)
But if i use declaration as
public function validate(\Phalcon\Mvc\ModelInterface $record)
I got new error
Catchable fatal error: Argument 1 passed to app\common\components\validators\Unique::validate() must implement interface Phalcon\Mvc\ModelInterface, instance of \Phalcon\Mvc\Collection given
How can i solve this?
Anyone needing to solve this can follow this answer: https://stackoverflow.com/questions/13873094/phalcon-odm-validation-messages/13876657#13876657
This not work with latest framework for me cause of incopatible declaration of validate method.
Did you try this on phalcon 2.0?
Anyone needing to solve this can follow this answer: https://stackoverflow.com/questions/13873094/phalcon-odm-validation-messages/13876657#13876657