From 6a69b350f7e8f24b62f5472b38360954313ba786 Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Mon, 4 Mar 2024 09:06:50 +0100 Subject: [PATCH] fix(address): nullify data when empty or invalid address is entered --- .../address_component.html.haml | 2 +- app/models/champs/address_champ.rb | 17 ++++++++++++++--- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/app/components/editable_champ/address_component/address_component.html.haml b/app/components/editable_champ/address_component/address_component.html.haml index bee71b52a..e1029f05a 100644 --- a/app/components/editable_champ/address_component/address_component.html.haml +++ b/app/components/editable_champ/address_component/address_component.html.haml @@ -1,3 +1,3 @@ = render Dsfr::ComboboxComponent.new form: @form, url: data_sources_data_source_adresse_path, selected: @champ.value, allows_custom_value: true, input_html_options: { name: :value, id: @champ.input_id, class: 'fr-select', describedby: @champ.describedby_id } do = @form.hidden_field :external_id, data: { value_slot: 'value' } - = @form.hidden_field :feature, value: '', data: { value_slot: 'data' } + = @form.hidden_field :feature, data: { value_slot: 'data' } diff --git a/app/models/champs/address_champ.rb b/app/models/champs/address_champ.rb index 3f49c3a7a..487f472e7 100644 --- a/app/models/champs/address_champ.rb +++ b/app/models/champs/address_champ.rb @@ -3,10 +3,21 @@ class Champs::AddressChamp < Champs::TextChamp data.present? end + def feature + data.to_json if full_address? + end + def feature=(value) - return if value.blank? - feature = JSON.parse(value) - self.data = APIGeoService.parse_ban_address(feature) + if value.blank? + self.data = nil + else + feature = JSON.parse(value) + if feature.key?('properties') + self.data = APIGeoService.parse_ban_address(feature) + else + self.data = feature + end + end rescue JSON::ParserError self.data = nil end