0

I am new to Yii framework, and m stuck in a simple task of checking weather user has supplied any inputs for update request or not.

considering the following code

Model Code:

 class Car extends ActiveRecord {
       public $id;
       public $name;
       public $registration;
       public $created_at;

       public function sceanrios(){
          return ['create'=>['name','registration'],
                  'update'=>['name','registration']];
       }  

       public function rules(){
          return [['name','registration'],'required','on'=>'create']
       }
    }

Controller:

class CarController extends yii\rest\ActiveController{
   public $modelClass = 'models/Car';
}

When I send PUT request to CarController without posting any attribute, it gives 200 OK response.

I want a validation rule that checks if user has actually posted any value then return OK after updating it; else return error message "Atleast one field should be set to update"

I have found following solution Yii2 dependency custom rule validation that only works for POST request i.e in create scenario, but its not working in update scenario as $this->attribute holds old values.

another solution with same issue Yii form model validation- either one is required that does not work for PUT request

found many others they only work for new record, but not for updating existing record.

Is there any validator for this?

Community
  • 1
  • 1
Innam Hunzai
  • 442
  • 1
  • 6
  • 17

2 Answers2

1

I developed solution myself from @silverFire idea, and posting answer here. All we need is a callback function on validation that will check for dirty attributes. So the Car class will be as follow :

class Car extends ActiveRecord {
           public $id;
           public $name;
           public $registration;
           public $created_at;

           public function sceanrios(){
              return ['create'=>['name','registration'],
                      'update'=>['name','registration']];
           }  

           public function rules(){
              return [
                      [['name','registration'],'required','on'=>'create'],
                      [['name','registration'], 'atLeastOne' 'on'=>'update'],
                    ];            

           }

           public function atLeastOne($attribute, $params)
           { 
                 if(empty($this->getDirtyAttributes(['name','registration'])){
                     $this->addError($attribute, "Nothing to update"); 
                 }
           }  
        }
Innam Hunzai
  • 442
  • 1
  • 6
  • 17
0

Yes, there is a code-on-yii/yii2-at-least-validator

SilverFire
  • 1,582
  • 13
  • 22
  • Hello @SilverFire On line number 97 of the AtLeastValidator class we can see this line of code $value = $model->$attributeName; which is the same I tried earlier, it only works for new records... not for updating existing record. – Innam Hunzai Sep 08 '16 at 08:48
  • You can set the value of both attributes to `null` in the model `init()` method, then filling one of them will be a must – SilverFire Sep 08 '16 at 08:58
  • In such case if user only pass name this will overwrite registration with null – Innam Hunzai Sep 08 '16 at 09:08
  • Well yes, but you've said you need only one of them :) You can use `$model->getDirtyAttributes(['name'])` instead of `$model->name` to check the modified value of some attribute – SilverFire Sep 08 '16 at 09:12
  • Well read the question again I said anyone of it, However I develop solution using the idea $model->getDirtyAttributes(['name','registration']); posting complete answer for it – Innam Hunzai Sep 09 '16 at 07:00