GreyhoundIT's avatar

If form feild is blank insert a 0

Looking for somehelp. Locally on sqlite my app works but ater deploying to heroku on a postgres database I was getting some invalid input syntax for integer: "" errors.

I have set the feilds to nullable in the migration eg. $table->integer('g1_handicap')->unsigned()->nullable();

In my form i am submitting I have 8 of these fields that are integer's . I was expecting if they were left blank then that would be ok. However postgres says not.

So as a temporary fix I have set them all to similar to this {{ Form::number('g1_handicap', 0, array('class' => 'form-control', 'step' =>'any')) }}

However Im guessing that the users will see the 0 in the filed and try deleting it for the optional ones.

How would I go about saying if the field is blank then set it to zero after they have clicked submit. so to the user its blank but before it gets to the database its turned into a 0

Is there a neat way of doing this jQuery or Request or Middleware?

This is my store function public function store(TeamCreateRequest $request) { $input = $request->all(); $fixture = Fixture::find($input['fixture_id']); TeamSheet::create($input); Session::forget('fixtureId'); Session::flash('success', "Team successfully submitted"); return redirect()->action( 'UserZoneController@show', ['id' => $fixture->zone_id]); }

The form has 30 fields

Thanks in advance

0 likes
2 replies
Jaytee's avatar

Why not just add defaults to your migrations?

$table->integer('my_integer')->unsigned()->default(0);
// if nothing is entered by the user, then it automatically assigns a default value of 0

Please or to participate in this conversation.