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.
May be obsolete soon
Please also observe this issue. An upcoming release might fix this soon for numeric values:
http://code.google.com/p/yii/issues/detail?id=1750
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!
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.