Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

uniqueness for collection

Is there anything like "Uniqueness" validator for a collection?



6.9k

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



6.9k
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() {}
}


6.9k

phalcon 2.0.0b



20.0k
  1. new Uniqueness(["message" => "bla", "field" => "bla"]).
  2. Is validation, not validate.
  3. Inside it, you do $this->validate($validatorOfFirstPoint) (for each validator) and finally return !$this->validationHasFailed().


6.9k
  1. I have
$email->addValidators(array(
    new Uniqueness(array(
                    'field' => 'email',
                    'message' => 'This email is already registered'
                ))
));

2 Where should it be validation but not validate?



20.0k

public function validation()



20.0k

Ah, my fault. I wasn't thinking using own validators, so I'm not of much help...

BTW, the signature of the function looks good. Try specifying (Phalcon\Mvc\ModelInterface $record) (but I don't think it will solve it).



6.9k

yes, I tried that.. didn't help

edited Jan '15

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.

Here is the update code for collection model:

        $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
     }
 }

file for: Validator\Collection\UniqueValidator

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 :)



1.7k

You must add validation logic for updating, because when you update a recored, there is already a record.



20.0k

>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.



106
edited Jun '15

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?



106

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: http://stackoverflow.com/questions/13873094/phalcon-odm-validation-messages/13876657#13876657