emptynullvalidator Validator to set empty values to null

  1. Documentation
  2. Change Log

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

Documentation

Requirements
  • Yii 1.0 or above
Installation
  • Extract the release file under protected/extensions
Usage

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'),
          'trimValue'=>true,'lowercaseCompare'=>true),
    // Sets attributes to null if empty but not equal to zero
    array('rating', 'ext.emptyNullValidator','ignoreZero'=>true),
    ....,
    );
}

Change Log

September 9, 2010
  • Initial release.
5 0
3 followers
575 downloads
Yii Version: 1.1
License: BSD-2-Clause
Category: Validation
Tags:
Developed by: Timbo
Created on: Sep 9, 2010
Last updated: 14 years ago

Downloads

show all