Skip to content

"Error: Item failed validation" on Asset import with custom fields and zero numeric value #17433

@Yvejen

Description

@Yvejen

I'm currently trying to import our existing list of devices into snipeit. After creating some custom fields and running the importer I noticed that for some
reason some of the assets failed validation while others passed for seemingly no reason.

After reducing my test case I got it down to the following CSV, where the first item fails validation and the second one gets imported successfully

Asset Tag,Category,NumberField,Location,Company,Model Name,Item Name
10,mycategory,0,Room1,mycompany,PCModel,Test1
11,mycategory,1,Room1,mycompany,PCModel,Test2

NumberField is a custom Field that is marked as required for PCModel

How to reproduce

  1. Create a custom field of numeric type (NumberField)
  2. Add it to a fieldset and mark it as 'required'
  3. Create a corresponding Asset Model using that fieldset and try to import the CSV using php artisan snipeit:import ...

The first item will fail validation

SnipeIT Version

v8.1.18 build 18876

PHP Version

8.3.6

After poking around a bit in the import code I noticed the following in app/Importer/AssetImporter.php

if ($customFieldValue) {
if ($customField->field_encrypted == 1) {
$this->item['custom_fields'][$customField->db_column_name()] = Crypt::encrypt($customFieldValue);
$this->log('Custom Field '.$customField->name.': '.Crypt::encrypt($customFieldValue));
} else {
$this->item['custom_fields'][$customField->db_column_name()] = $customFieldValue;
$this->log('Custom Field '.$customField->name.': '.$customFieldValue);
}
} else {
// Clear out previous data.
$this->item['custom_fields'][$customField->db_column_name()] = null;
}

It seems the conditional might interpret zero numeric values as false and just null them, which then causes the Asset to fail validation later on?
I'm not familiar enough with the codebase to know if this would be the place to fix this, but changing the conditional to use an
!is_null($customFieldValue) check allowed all my items to import successfully.

Metadata

Metadata

Labels

❓ not sure if bugThis issue has not been confirmed as a bug yet

Type

Projects

Status

Done

Relationships

None yet

Development

No branches or pull requests

Issue actions