diff --git a/src/app/item/create.rs b/src/app/item/create.rs index 4c56f57..6baeed7 100644 --- a/src/app/item/create.rs +++ b/src/app/item/create.rs @@ -5,12 +5,13 @@ use askama_axum::{IntoResponse, Response}; use axum::extract::State; use axum::{async_trait, debug_handler, Form}; use serde::Deserialize; +use tracing::info; use crate::app::routes::AppState; use crate::db::display_unit::DbDisplayUnit; use crate::db; -use crate::util::extract::form_helpers::form_checkbox_is_checked; use crate::util::extract::htmx_form_data::{HtmxFormData, HtmxFormDataError}; use crate::util::extract::validated_form::ValidatedForm; +use crate::util::extract::form_helpers::deserialize_form_checkbox; #[derive(Template, Debug)] #[template(path = "item/item-create-form.html")] @@ -45,9 +46,10 @@ pub struct CreateItemFormData { name: String, display_unit: String, reorder_point: f64, - allow_fractional_units: Option, pims_id: Option, vetcove_id: Option, + #[serde(default, deserialize_with = "deserialize_form_checkbox")] + allow_fractional_units: bool, } #[async_trait] @@ -58,8 +60,6 @@ impl HtmxFormData for CreateItemFormData { let mut base = Self::base_template(&state).await?; let display_units = &base.display_units; - let allow_fractional_units = form_checkbox_is_checked(&self.allow_fractional_units); - let name_error = if self.name.is_empty() { "Please provide a name" } else { @@ -79,7 +79,7 @@ impl HtmxFormData for CreateItemFormData { || self.reorder_point.is_sign_negative() { "Provide a positive number" - } else if !(allow_fractional_units || self.reorder_point.fract() == 0.0) { + } else if !(self.allow_fractional_units || self.reorder_point.fract() == 0.0) { "Fractional units not allowed" } else { "" @@ -126,7 +126,7 @@ impl HtmxFormData for CreateItemFormData { base.pims_id_error = pims_id_error; base.vetcove_id_value = self.vetcove_id.as_deref().unwrap_or_default().to_string(); base.vetcove_id_error = vetcove_id_error; - base.allow_fractional_units_value = allow_fractional_units; + base.allow_fractional_units_value = self.allow_fractional_units; return Err(HtmxFormDataError::ValidationError(base)); } @@ -163,10 +163,8 @@ pub async fn create_item_form_post( form_data: ValidatedForm, ) -> Result { - let allow_fractional_units = form_checkbox_is_checked(&form_data.allow_fractional_units); - let _new_id = db::inventory_item::add_inventory_item(&state.db, &form_data.name, form_data.reorder_point, - allow_fractional_units, &form_data.display_unit, + form_data.allow_fractional_units, &form_data.display_unit, &form_data.pims_id, &form_data.vetcove_id, ).await?; diff --git a/src/util/extract/form_helpers.rs b/src/util/extract/form_helpers.rs index e1a2f18..177ab51 100644 --- a/src/util/extract/form_helpers.rs +++ b/src/util/extract/form_helpers.rs @@ -1,3 +1,8 @@ -pub fn form_checkbox_is_checked(val: &Option) -> bool { - val.as_ref().map(|val| val == "on").unwrap_or(false) +use serde::{Deserialize, Deserializer}; + +pub fn deserialize_form_checkbox<'de, D>(deserializer: D) -> Result +where D: Deserializer<'de> { + let buf = String::deserialize(deserializer)?; + + Ok(&buf == "on") } \ No newline at end of file