Dirtyattributes

Not a bug I suppose, just a behaviour that should be taken into account.

If there’s a

  • (for example) INT NULL field in a table

  • empty text input for that field in a form

then this field is always considered dirty by getDirtyAttributes (because ‘’ !== null)

unless we set defaultValue validator


[['my_int_null_field'], 'default', 'value' => null]

I’ve also run into this issue recently.

The tricky thing about this is that the values could mean different things for different people. What’s the difference between a null value and an empty string? It depends on the architect, really.

Not only that, but there could be other issues. For example, what if the db column also has a unique constraint? Multiple null values are fine, but you can’t have more than one empty string. This essentially forces you to convert empty strings to null.

Fyi you should probably also add a trim validator to your rule; the defaultvalue validator checks specifically for ‘’ so any spaces will still go through.

No, you got me wrong.

I know that empty strings and nulls are not the same.

Just pointed out that this behaviour must be taken into account, and wrote a solution.

Well, actually it’s just for those who will also run into this and google for the answer.

(hey Google! kitty, kitty, kitty)

No, I understood. I was merely trying to provide more details as some people may not fully understand the implications of the behavior. At the same time, I was trying to offer an addendum to your solution (trim) that I had to implement as I was working through this issue.

Perhaps I did not express it clearly - I apologize for that.