I have a checkbox that only exists in my post values when it's checked. When not checked, the value doesn't exist in Input::all() resulting in a InvalidArgumentException when mapping the input to the command. I toughed to be smart and gave the parameter a default value of null, this way the command executes when the checkbox is not checked, but when it's checked the value stays null. After reading theCommanderTraid::mapInputToCommand method I realised that it's coded in this way.
Though, I'm curious what people think of an approach where, when you don't always have the input available, you can give a default vale of null and make a check inside mapInputToCommand to see if the default value of the parameter is different to the value of the input from the same key. And if that's the case, assign the input value instead of the default value.
I have a checkbox that only exists in my post values when it's checked. When not checked, the value doesn't exist in
Input::all()
resulting in aInvalidArgumentException
when mapping the input to the command. I toughed to be smart and gave the parameter a default value ofnull
, this way the command executes when the checkbox is not checked, but when it's checked the value staysnull
. After reading theCommanderTraid::mapInputToCommand
method I realised that it's coded in this way.Though, I'm curious what people think of an approach where, when you don't always have the input available, you can give a default vale of
null
and make a check insidemapInputToCommand
to see if the default value of the parameter is different to the value of the input from the same key. And if that's the case, assign the input value instead of the default value.