Posts

Showing posts with the label default-value

Laravel 5.6 Validation on empty fields

Image
Clash Royale CLAN TAG #URR8PPP Laravel 5.6 Validation on empty fields Somehow I feel like this should be a common question, but I can't seem to find a definite answer on that one. The problem is quite simple: On validating a form, I would like to exclude the empty non-required fields from the resulting array - and this in order to use the default value set at database level. Since Laravel is using the ConvertEmptyStringsToNull middleware by default (and I am not so keen on changing that), it means that my empty fields will be converted to 'null' and sent to my database (hence not getting their default value, and actually breaking the query since those fields are not nullable at database level). $userData = $request->validate([ 'email' => 'required|string|email|max:255|unique:users', 'number_of_whatever' => //if this field is empty, I want it stripped out the $userData array - or automatically default to the database ...