Empty string in Model

Hi, I have a question about some generic method for preventing inserting empty string into database for field another then TEXT e.g. Data.

Please imageine we have got form with two fileds: name - varchar dob - date

Now in controller I'm using assign method to insert that values to Model object.

If I don't fill dob field then to that field in model will be assigned "" so trying save to database will casue error. "" isn't correct date representation.

Another case is I have got correct date, object is saved into DB. Now I wanted to updated that object and clear date field. And here similar "" will be assigned to date field and error will be returned on Update query.

I was thinking about php arrayfilter to remove empty strings, null values , etc. but in that case I will be unable to clear value in text field.

What solution do You use.


Just use validation and Phalcon\Validation\Validator\Date

I think validator will check if date field is in correct format, so if it is proper date or null. But from web form if use would like to remove date then he will erase that input and that input will be provided as empty string so validator will recognize that this is incorect value, but from user point of view it is correct (user wanted to remove date value).

edited 9d ago

No, it will check for not null too. You can also use allowEmpty and allow only null value(at least in latest phalcon) when disallow empty string.

Also you can also just use beforeValidationOnSave and if it's empty string then change value to null.