2024-04-04 12:20:03 -07:00
< ? php
namespace App\Http\Requests\Traits ;
2024-04-04 15:41:10 -07:00
use App\Models\AssetModel ;
2024-04-04 12:20:03 -07:00
trait MayContainCustomFields
{
2024-04-04 15:41:10 -07:00
// this gets called automatically on a form request
2024-04-04 12:20:03 -07:00
public function withValidator ( $validator )
{
$validator -> after ( function ( $validator ) {
2024-04-04 15:41:10 -07:00
$request_fields = $this -> collect () -> keys () -> filter ( function ( $attributes ) {
2024-04-04 12:20:03 -07:00
return str_starts_with ( $attributes , '_snipeit_' );
});
2024-04-04 15:41:10 -07:00
if ( count ( $request_fields ) > 0 ) {
2024-04-04 12:20:03 -07:00
if ( $this -> method () == 'POST' ) {
2024-04-04 15:41:10 -07:00
$request_fields -> diff ( AssetModel :: find ( $this -> model_id ) -> fieldset -> fields -> pluck ( 'db_column' ))
-> each ( function ( $request_field_name ) use ( $request_fields , $validator ) {
// i could probably add some more conditions here to determine whether or not the column exists but just not on this asset model
// and then return a more helpful error message
$validator -> errors () -> add ( $request_field_name , 'This field does not seem to exist (at least on this asset), please double check your custom field names.' );
});
2024-04-04 12:20:03 -07:00
} elseif ( $this -> method () == 'PUT' || $this -> method () == 'PATCH' ) {
2024-04-04 15:41:10 -07:00
// need to know about other pr before I can go down this route
2024-04-04 12:20:03 -07:00
}
}
});
}
}