Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

skihansen's avatar

Laravel 5 forms - How To Ignore Blank Text Fields in FormRequest (validation)

I have the following form

{!! Form::text('name', null) !!}
{!! Form::text('number', null) !!}
{!! Form::text('description', null) !!}

name is required. number and description are optional. number is an integer field; description is text.

When I input a valid name and number field, everything works, BUT, a blank value gets inserted into description, rather than leaving it NULL.

When I leave number blank, I get an "invalid input syntax for type integer" error. I have tried this:

{!! Form::number('number', null) !!}

But I get the error "Method number does not exist." (Has this changed from Laravel 4 to Laravel 5?)

So my main question is this - What is the best practice for treating empty form text fields as NULL, and ignoring them when sending the form input to a FormRequest?

0 likes
4 replies
michaeldyrynda's avatar
Level 41

It doesn't look like illuminate/html supports the number method in 5.0. You can use:

{!! Form::input('number', 'number', null) !!}

As for null values, the reason you're inserting an empty string rather than NULL into your database is that because null and '' are not equivalent. You'll need to explicitly insert null for any empty fields that you need - either via model attribute or checking in your controller method. The option you choose will depend on how many places you're actually setting the values. More than once and you'll want to do it in the model.

class MyModel extends Model {

    protected $fillable = [ 'name', 'number', 'description', ];
     
    public function setNameAttribute($name)
    {
        $this->attributes['name'] = trim($name) !== '' ? $name : null;
    }

    public function setNumberAttribute($number)
    {
        $this->attributes['number'] = trim($number) !== '' ? $number : null;
    }

    public function setDescriptionAttribute($description)
    {
        $this->attributes['description'] = trim($description) !== '' ? $description : null;
    }

}

Now you can just drop your request parameters directly into your model:

class MyController extends Controller {

    public function store(Request $request)
    {
        return MyModel::create($request->all());
    }

}

As an aside, if you want Form::number, this is provided by the (maintained) laravelcollective/html package.

2 likes
skihansen's avatar

Thank you! I now recall at one time reading about Accessors and Mutators, but I had to refresh my memory: http://laravel.com/docs/5.0/eloquent#accessors-and-mutators

Here's what I did. If anyone has a better solution, please post.

MyBaseModel.php

abstract class MyBaseModel extends Model {
    public function nullIfBlank($field)
    {
        return trim($field) !== '' ? $field : null;
    }
}

MyModel.php

class MyModel extends MyBaseModel {
    protected $fillable = [ 'name', 'number', 'description', ];
     
    public function setNameAttribute($name)
    {
        $this->attributes['name'] = $this->nullIfBlank($name);
    }

    public function setNumberAttribute($number)
    {
        $this->attributes['number'] = $this->nullIfBlank($number);
    }

    public function setDescriptionAttribute($description)
    {
        $this->attributes['description'] = $this->nullIfBlank($description);
    }
}
4 likes
arianmaps's avatar

I use a trait to convert to null or zero when needed:

<?php

namespace App;

trait ModelHelpers
{
    /**
     * Returns null if value is an empty string, the value otherwise.
     *
     * @param $value
     * @return mixed
     */
    public function emptyToNull($value)
    {
        return $value !== '' ? $value : null;
    }

    /**
     * Returns zero if value is an empty string, the value otherwise.
     *
     * @param $value
     * @return mixed
     */
    public function emptyToZero($value)
    {
        return $value !== '' ? $value : 0;
    }
}

And on the model:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Tip extends Model
{
    use ModelHelpers;

    /**
     * Sets special_instructions to null if no input is provided
     *
     * @param $value
     */
    public function setSpecialInstructionsAttribute($value)
    {
        $this->attributes['special_instructions'] = $this->emptyToNull($value);
    }

   /**
     * Sets tip_amount to 0 if no input is provided
     *
     * @param $value
     */
    public function setTipAmountAttribute($value)
    {
        $this->attributes['tip_amount'] = $this->emptyToZero($value);
    }
}

Please or to participate in this conversation.