I have a date column that usually takes values as dd.MM.yyyy
. Its validation rule in the model\'s rules()
is this:
array(\'start, e
Assigning a CDbExpression
to the field will (and should) never pass validation; the validator allows null
but it definitely cannot allow an arbitrary CDbExpression
as the value of the field; this should not be surprising.
If you wanted to write null
to the database then do it simply with $user->start = null
-- there is no reason to involve CDbExpression
at all here.
Another approach you could use in case you did need to use CDbExpression
would be to tell save not to validate the record and do it manually, as in:
$attributes = $user->attributeNames();
if (empty($csv_data)) {
$user->start = new CDbExpression('NULL');
$attributes = array_diff($attributes, array('start')); // don't validate this
} else {
$user->start = $csv_data;
}
if ($user->validate($attributes)) { // validate only attributes we want here
$user->save(false); // no validation at all here
}
in model rules()
:
array('start, end', 'date', 'format' => 'dd.MM.yyyy'),
array('start, end', 'default', 'setOnEmpty' => true, 'value' => null),
also,
if (empty($csv_data)) {
$user->start = null;
} ...
should do the trick too.
The trivial fix for this is not to set the value at all during creation:
if (!empty($csv_data)) {
$user->start = $csv_data;
}
This way, the date will not be set and thus appear as empty, which also passes validation.