Check Empty Fields - Orange 5.3

Asked

Viewed 2,166 times

2

I have an entire field and a date field that are not mandatory and are programmed to accept NULL, but when I send the form data without filling it in my controller receives the empty value instead of ignoring the field, which generates the error:

Incorrect integer value: '' for column vendedor_id

how can I fix this?

Migration:

 $table->integer('vendedor_id')->unsigned()->nullable();
 $table->date('data_venda')->nullable();

Controller:

$new = $this->repository->create($request->all());

View:

<div id="vendedor_id" class="form-group col-sm-4 opt">
    <label for="vendedor_id">Vendedor:</label>
    <select name="vendedor_id" class="form-control select2">
        <option value=""></option>
        <option value="1">User 1</option>
        <option value="2">User 2</option>
    </select>
</div>
<div id="data_venda" class="form-group col-sm-4">
    <label for="data_venda">Data:</label>
    <input type="date" name="data_venda" class="form-control">
</div>

In my request I have some validations for the required fields, but it does not include anything about these 2 fields. Ex.:

public function rules()
{
    return [
        'cnpj' => 'numeric|required|cnpj|unique:empresas',
        'razao_social' => 'required',
    ];
}
  • Provide your validation? and also where are you doing this code? I believe it all comes down to validation!!!

  • You need to post the code snippet to your question to make it clearer. It could be two different problems (or more). We need more details.

  • Make a Submit without sending the required values. Debug with function dd before the Insert.

  • Is this form being sent via Ajax? I’ve had problems like this when using FormData.

  • "vendedor_id" => "" "data_sale" => ""

  • Yes, it’s via Ajax @Wallacemaxters

  • Raylan anyway this error occurs because you are trying to insert a string into a BD field (column) that is reset to integers (integer)

Show 2 more comments

2 answers

5

If no value is chosen on that select he’s sending "" ai mora the problem, need to be checked before sending to that layer repository:

$data = $request->except('vendedor_id');
$data['vendedor_id'] = $request->input('vendedor_id', "") == "" 
                      ? null
                      : (int)$request->input('vendedor_id')
$new = $this->repository->create($data);
  • That solves the problem, but it’s a bit of a scam. Maybe the problem is something else, but the AP didn’t describe it right.

  • Exactly, I could go even further, and put if(!is_numeric($request->input('vendedor_id')) { .... }, the error is generated because the column is integer and Raylan is sending a string

  • So the @Wallacemaxters problem is how it’s doing (maybe because I don’t know the real intention), and not my answer representing a gambiarra. Gambiarra validate a field that may be empty or else the number 1 or 2 with a single if I don’t see how.

  • @Virgilionovic I did a test here. It really looks like Laravel "gave that mole". When the value of "select" is empty, it sends an empty string.

  • This way it worked, but the ideal would be even if the Variable sent a Null or merely ignored the field

  • @Wallacemaxters, not Laravel, is php even, an empty input is received as empty string

  • 1

    @Raylansoares is the following, when you send a select and have an option in it between double quotes "", the Laravel understands that it can record a empty at the table, but when I force a NULL in the field he understands that is to record null that’s how he works! currently

  • @Miguel yes, but Laravel "went soft" not to treat it right. I think it’s good to give them the suggestion to improve it in the next version. Avoids bracing work.

  • Ha yes, you’re right @Wallacemaxters , it’s true

  • Got it @Virgilionovic, thank you very much

  • @Raylansoares this code can be further improved, but, it explains to you why the problem, it is good to always pass what is the problem that happens.

  • Both answers were very enlightening, I did some tests here and I will use the Setter option. Thanks @Virgilionovic

Show 7 more comments

5


I will pass some solutions below that the framework itself offers:

Model Mutators

It is possible to define a behavior for the definition of a certain attribute of a Model in Laravel.

For example, for you to not have to define at all times verification conditions of a field in your controller, you can create a setter specific to these fields in your Model.

Behold:

  public function setVendedorIdAttribute($valor) {
      $this->attributes['vendedor_id'] = ctype_digit($valor) ? $valor : NULL;
  }

So every time an empty value was passed, it would be inserted as NULL at the bank.

Validation

According to documentation from Laravel 5.3, it is possible, from this version, to define a validation nullable for a given field.

Array_filter

Another solution would be to use array_filter that removes the empty values of a array.

$data = array_filter($request->all());
$new = $this->repository->create($data);

Array_map

If you just want to convert the empty values to NULL, you can also use array_map as a solution:

$data = array_map($request->all(), function ($value) {
  return $value ?: NULL;
});
  • I thought to give also this solution would be very good, the problem is that we do not know what the respository->create makes! if it’s fill will work

  • It worked that way too! Of the 2 ways in fact

  • 1

    I think I will use this array_filter even, everything is much cleaner and works perfectly

  • @Raylansoares always prefer solutions that "spend less".

  • The array_filter may be good in create but, in Edit it is harmful, and if I want to set as NULL? @Raylansoares

  • 1

    @Virgilionovic if you don’t want to set the value as NULL, values will simply not be in the array. Then that field would not be updated. The array_filter removes empty values from array, he does not define to Null. When a field is nullable, only the fact of being omitted makes it already set to NULL (but in the case of create, when it is Insert the default keeps what you already have).

  • @Wallacemaxters I write create number 1, and in edit I select empty wanting to change the value to null if it is not in the array will not have update !!!

  • 1

    Got it, so if in the update I want to return the value to Null will not work right

  • @Raylansoares one, it’s true. So in this case, you could convert too... Just a minute.

  • @Raylansoares with array_filter no ... just wanted to make it clear that I thought about create and Edit, and Edit would not be valid. The option would be setter ( I take great care of it ) or check in the controller

  • @Virgilionovic as he is using the Repository, perhaps it would be even more profitable he overwrite the method create and update, But you gotta see how that bullshit works, 'cause I never moved it

  • @Wallacemaxters yes, but as I said we don’t have that code in hand, and maybe it can be rewritten or maybe not everything will depend on, it’s a particular case

  • Both answers were very enlightening, I did some tests here and I will use the Setter option. Thanks @Wallacemaxters

Show 8 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.