How can I check if any of the fields in the model are empty or empty?

I have a model $userModel

. I want to check if any fields for this model are empty or null

.

I am currently doing this with a large if statement.

if(!empty($userModel->name) && !empty($userModel->address) ... && !empty($userModel->email))
{
   // All fields have values
}

      

This way works, but if later I need to add another field to the model, then I need to go back to the if and add another condition &&

.

How can I do this in one check?

Is there something like: $userModel::model()->areAllFieldsFilled();


Additional info: The model is already saved in the db and the user doesn't need to enter data. This is just me, checking how complete a particular model is, by no means will it be mandatory for all these fields in the database, just a few. Usually things like $userModel->bio

, stay null

.

I want to avoid checking 5-10 fields. I don't want to be giant if it needs to be saved when the model changes.

+3


source to share


3 answers


PHP allows you to iterate over the properties of an object . Validation for each property can be simplified with empty () :



$allHaveValues = TRUE;
foreach ($userModel as $key => $value) {
    if (empty($value)) {
       $allHaveValues = FALSE;
       break;
    }
}

if ($allHaveValues) {
    // Do something...
}

      

+2


source


Use empty()

if(!empty($userModel->name)) { .. }

      

empty () DOCS

  • "" (empty line)
  • 0 (0 as integer)
  • 0.0 (0 as float)
  • "0" (0 as string)
  • NULL
  • FALSE
  • array () (empty array)
  • $ var; (declared variable, but no value)

Update



$modelData = array($userModel->name, $userModel->address, $userModel->email);
if(!in_array('', $modelData) && !in_array(null, $modelData)) { .. }

      

in_array ()

Or you can use array_intersect

-

if(empty(array_intersect(array('', null), $modelData))) { .. }

      

array_intersect ()

+1


source


I guess you don't need to do this.
All you need to do is specify your validation rules.
For example:

<?php

class Brand extends CActiveRecord
{
    public function tableName()
    {
        return 'brand';
    }

    public function rules()
    {
        return [
            ['name, country', 'on' => 'insert'],
            ['name', 'type', 'type' => 'string', 'on' => 'insert'],
            ['name', 'length', 'max' => 100, 'on' => 'insert'],
            ['name', 'type', 'type' => 'array', 'on' => 'search'],
            ['country', 'type', 'type' => 'string'],
            ['country', 'length', 'max' => 50],
        ];
    }
}

      

When you use this model, you just need to check this model for $model->validate()

, and if it fails, show errors with $model->getErrors()

. In addition, you can specify which rule script you want to use. For example: a $model->senario = 'search';

validation rule will be used search

and the property name

should be an array. But when the script name insert

should be a string no longer than 100.

In my examples the fields: name, country - are required for insert ( ['name, country', 'on' => 'insert']

).

0


source







All Articles