Yii 1.1: emptynullvalidator

Validator to set empty values to null

This extension is a validator that is used to convert non-null empty values to null. It should only be used on fields that are nullable, and is very useful where the column has a foreign key contraint. It can also be used to save database space on empty blobs.

Note: Yii 1.1.4 no longer converts an empty string unsigned int to a null - it stores it as "".



  • Yii 1.0 or above


  • Extract the release file under protected/extensions


This extension is a validator but doesn't do validation - it simply converts empty values to null.

An empty value is one that returns true when passed to the empty() php function.

Optional properties: - emptyValues: either a scalar value or an array of values which are considered empty values when compared to the attribute value - lowercaseCompare: a boolean to indicate if the emptyValues compare must be done case insensitively lowercase - trimValue: a boolean to indicate if the attribute value should be trimmed of whitespace before checking for empty value - ignoreZero: a boolean to indicate if a numeric attribute of zero should be ignored as an empty value, ie: 0 is not null

See the following code example:

public function rules()
  return array(
    array('name, description, supplierid', 'required'),
    array('supplierid, branchid', 'numerical', 'integerOnly'=>true),
    // Sets attributes to null if empty
    array('notes, branchid', 'ext.emptyNullValidator'),
    // Sets attributes to null if empty or equal to '0000-00-00'
    array('purchasedate', 'ext.emptyNullValidator','emptyValues'=>'0000-00-00'),
    // Sets attributes to null if trimmed value is empty or 
    // equal to 'n/a' or 'none' (case insensitive lower)
    array('comments', 'ext.emptyNullValidator','emptyValues'=>array('n/a','none'),
    // Sets attributes to null if empty but not equal to zero
    array('rating', 'ext.emptyNullValidator','ignoreZero'=>true),

Change Log

September 9, 2010

  • Initial release.

Total 2 comments

#2125 report it
Mike at 2010/11/15 04:31am
Small confusion on ignoreZero

I still was confused about the setting of ignoreZero. Maybe this comment would be more clear:

ignoreZero - if true, '0' and '0.0' will not get converted to null. Defaults to false.

Other than that: Nice job!

#2124 report it
Mike at 2010/11/15 04:06am
May be obsolete soon

Please also observe this issue. An upcoming release might fix this soon for numeric values:


Leave a comment

Please to leave your comment.

Create extension
  • Yii Version: 1.1
  • License: New BSD License
  • Developed by: Timbo
  • Category: Validation
  • Votes: +5
  • Downloaded: 573 times
  • Created on: Sep 9, 2010
  • Last updated: never