We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Using Validation for unqiueness and valid value

Need to validate a field in database for uniqueness before saving in model, I can do :

     public function validation()
    {
        $validator = new Validation();
        $validator->add('unit', new Uniqueness([
            "message" => "The unit is already in use"
        ]));
        return $this->validate($validator);
    }

But I need to do two make two more tests:

  1. I need to first check UNIT in other table-- if it exists there or not ,
  2. Units with value = 0 has to be ignored as there are multiple units with 0 value.

So how can properly utilize phalcon Validation for my need.

edited Jul '18
if($unitExists) { // just check here if unit exists in other table
  $validator->add('unit', new Uniqueness([
              "message" => "The unit is already in use",
              "except" => [
                  0
              ]
  ]));
}


8.8k
edited Jul '18

Thanks , that was quick and precise, need little more guidance as I am still confused about proper use of MVC thing and placing of codes.

As per examples I am seeing for validation of Model, Say I have table named 'store' with following model where I need to use it. :

class Store extends Model
{

    public $id;

    public $name;

    public $unitId;

    public function validation()
    {
        $validator = new Validation();
        $validator->add('unitId', new Uniqueness([
            "message" => "The unit is already in use",
            "except" => [
               0
            ]
        ]));
        return $this->validate($validator); 
    }

}
  1. How method validation() is evoked on data of model, is it that validation() method is auto called when data is updated /saved by a MODEL ?

  2. I suppose to find unit is valid or not, I can use simply following code, but I am not sure where to place it, in a separate library file or in Store.php (model for store) or somehwere else. Probably can use it on controller but then data validation is being done in Model.
         $unit = Units::FindFirstById($unitId);
         $unitExist =  ($unit->count != 0 ) ? true : false;
edited Jul '18
  1. Validation is called on update/save/create, yes, it's pretty much one of model events https://docs.phalcon.io/bs/3.3/db-models-events here you can learn more about model events.

  2. The common case for this is to create seperate class called Repository, like UnitsRepository for all queries from database(you can also use it for writes though i don't like it). Also instead of findFirstById you can just do findFirst if $unitId is primary key of model. Also not sure what is count, can't you do it like this:

$unitExist = Units::count($unitId); > 0

might be enough in your case



8.8k
edited Jul '18

Thanks again for nice explanation.

Sorry for Naive questions, but for #2, I am still not sure where to place code you gave , currently I am using StoreController to do things . Since validation() has to be called in model Store, should I simply do this inside store model :

public function validation()
    {
    if ((Units::count($this->unitId)) > 0) {
        $validator = new Validation();
        $validator->add('unitId', new Uniqueness([
            "message" => "The unit is already in use",
            "except" => [
               0
            ]
        ]));
        return $this->validate($validator); 
        }
        else 
        {
        $this->getDi()->flash()->error('Invalid unit selected');
    }

Now with above problem, there problem as in case of invalid unit, it is showing error but unit is also getting updated in database, only incase of duplicate, its working as expected.