diff --git a/src/locales/en.json b/src/locales/en.json index af96761..35e0fe8 100644 --- a/src/locales/en.json +++ b/src/locales/en.json @@ -24,14 +24,22 @@ "not_manager_of_this_schema": "You cannot manage this ticket." }, "rules": { - "required": "%{field} field is required.", - "text_too_long": "%{field} field has too many characters.", - "text_too_many_lines": "%{field} field has too many lines.", - "not_valid_choice": "%{field} field has an incorrect choice.", - "not_upload_file": "%{field} field must upload a file.", - "not_upload_image": "%{field} field must upload an image.", - "unknown": "Unknown value for %{field} field.", - "too_many_choice": "%{field} field has too many choices." + "required": "%{field} is required.", + "text_too_long": "%{field} has too many characters.", + "text_too_many_lines": "%{field} has too many lines.", + "not_valid_choice": "%{field} has an incorrect choice.", + "not_upload_file": "%{field} must upload a file.", + "file_too_large": "%{field} file is too large.", + "invalid_file_type": "%{field} has an incorrect file type.", + "not_upload_image": "%{field} must upload an image.", + "image_too_large": "%{field} image is too large.", + "image_width_too_large": "%{field} image width is too large.", + "image_height_too_large": "%{field} image height is too large.", + "image_width_too_small": "%{field} image width is too small.", + "image_height_too_small": "%{field} image height is too small.", + "invalid_image_type": "%{field} has an incorrect image type.", + "unknown": "%{field} has an unknown value.", + "too_many_choice": "%{field} has too many choices." } } } diff --git a/src/locales/zh.json b/src/locales/zh.json index 0e39849..7b0e597 100644 --- a/src/locales/zh.json +++ b/src/locales/zh.json @@ -19,7 +19,7 @@ "error": { "not_join_to_this_ticket": "你沒有參與這個工單。", "not_assign_to_this_flow": "你沒有被指定參與這個流程。", - "not_assign_to_this_schema": "你沒有不能參與這個流程。", + "not_assign_to_this_schema": "你沒有能參與這個流程。", "not_probably_user_of_this_schema": "你不能建立這張工單。", "not_manager_of_this_schema": "你不能管理這張工單。" }, @@ -29,7 +29,15 @@ "text_too_many_lines": "%{field} 欄位行數太多。", "not_valid_choice": "%{field} 欄位選項不正確。", "not_upload_file": "%{field} 欄位必須上傳檔案。", + "file_too_large": "%{field} 欄位檔案太大。", + "invalid_file_type": "%{field} 欄位檔案類型不正確。", "not_upload_image": "%{field} 欄位必須上傳圖片。", + "image_too_large": "%{field} 欄位圖片太大。", + "image_width_too_large": "%{field} 欄位圖片寬度太大。", + "image_height_too_large": "%{field} 欄位圖片高度太大。", + "image_width_too_small": "%{field} 欄位圖片寬度太小。", + "image_height_too_small": "%{field} 欄位圖片高度太小。", + "invalid_image_type": "%{field} 欄位圖片類型不正確。", "unknown": "%{field} 欄位未知的值。", "too_many_choice": "%{field} 欄位選項太多。" } diff --git a/src/modules/ticket/api.rs b/src/modules/ticket/api.rs index 7ea4723..6d737b1 100644 --- a/src/modules/ticket/api.rs +++ b/src/modules/ticket/api.rs @@ -491,7 +491,7 @@ async fn upload_file_in_form_field<'a>( if form.field.is_file_define() { return form - .upload_file(&mut conn, data_folder, file) + .upload_file(&mut conn, data_folder, &i18n, file) .await .map(|mut f: TicketFormFile| { f.path = String::new(); @@ -502,7 +502,7 @@ async fn upload_file_in_form_field<'a>( if form.field.is_image_define() { return form - .upload_image(&mut conn, data_folder, file) + .upload_image(&mut conn, data_folder, &i18n, file) .await .map(|mut f| { f.path = String::new(); diff --git a/src/modules/ticket/forms/mod.rs b/src/modules/ticket/forms/mod.rs index 4c137f4..9c312a0 100644 --- a/src/modules/ticket/forms/mod.rs +++ b/src/modules/ticket/forms/mod.rs @@ -56,14 +56,47 @@ impl FormSchema { &self, conn: &mut crate::DbConn, i18n: &I18n<'a>, - data: &serde_json::Map, + data: &serde_json::Map ) -> Result, serde_json::Map> { let FormSchema { fields, .. } = self; let mut is_error = false; let mut result = serde_json::Map::new(); let mut errors = serde_json::Map::new(); + + let mut last_falsy_if: Option = None; for field in fields.iter() { + if let Some(ref falsy_if_key) = last_falsy_if { + if let TicketSchemaFormField { + define: FormFieldDefine::IfEnd { key }, + .. + } = field + { + if key == falsy_if_key { + last_falsy_if = None; + } + } + continue; + } + if let TicketSchemaFormField { + define: FormFieldDefine::IfEqual { key, from, value }, + .. + } = field.clone() + { + let condition_result = match from { + FormFieldDefault::Static(from_value) => value.contains(&from_value), + FormFieldDefault::Dynamic { + value: from_value, .. + } => match from_value { + Some(from_value) => value.contains(&from_value), + None => false, + }, + }; + if !condition_result { + last_falsy_if = Some(key); + } + continue; + } let user_value = match data.get::(&field.key) { Some(value) => value, None => { @@ -174,10 +207,11 @@ pub struct PartFormSchema { } impl PartFormSchema { - pub async fn upload_image( + pub async fn upload_image<'a>( &self, conn: &mut crate::DbConn, data_folder: &State, + i18n: &I18n<'a>, mut temp_file: TempFile<'_>, ) -> Result { let PartFormSchema { field, .. } = self; @@ -208,7 +242,7 @@ impl PartFormSchema { } = field.define { if file_size > max_size { - return Err(format!("Image {} is too large", field.key)); + return Err(i18n.tf("ticket.rules.image_too_large", &[("field", field.key.clone())])); } let file_content = get_file_content().await?; @@ -219,39 +253,36 @@ impl PartFormSchema { if let Some(min_width) = min_width { if width < min_width { - return Err(format!("Image {} width is too small", field.key)); + return Err(i18n.tf("ticket.rules.image_width_too_small", &[("field", field.key.clone())])); } } if let Some(max_width) = max_width { if width > max_width { - return Err(format!("Image {} width is too large", field.key)); + return Err(i18n.tf("ticket.rules.image_width_too_large", &[("field", field.key.clone())])); } } if let Some(min_height) = min_height { if height < min_height { - return Err(format!("Image {} height is too small", field.key)); + return Err(i18n.tf("ticket.rules.image_height_too_small", &[("field", field.key.clone())])); } } if let Some(max_height) = max_height { if height > max_height { - return Err(format!("Image {} height is too large", field.key)); + return Err(i18n.tf("ticket.rules.image_height_too_large", &[("field", field.key.clone())])); } } match mime { Some(mime) => { if !mimes.contains(&mime) { - return Err(format!( - "Image {} is not in the correct format", - field.key - )); + return Err(i18n.tf("ticket.rules.invalid_image_type", &[("field", field.key.clone())])); } Ok((mime, (width, height), file_size)) } - _ => return Err(format!("Image {} is not in the correct format", field.key)), + _ => return Err(i18n.tf("ticket.rules.invalid_image_type", &[("field", field.key.clone())])), } }; @@ -311,10 +342,11 @@ impl PartFormSchema { Err(format!("Field {} is not an image", field.key)) } - pub async fn upload_file( + pub async fn upload_file<'a>( &self, conn: &mut crate::DbConn, data_folder: &State, + i18n: &I18n<'a>, mut temp_file: TempFile<'_>, ) -> Result { let PartFormSchema { field, .. } = self; @@ -341,7 +373,7 @@ impl PartFormSchema { } = field.define { if file_size > max_size { - return Err(format!("File {} is too large", field.key)); + return Err(i18n.tf("ticket.rules.file_too_large", &[("field", field.key.clone())])); } let file_content = get_file_content().await?; @@ -352,19 +384,16 @@ impl PartFormSchema { match mime { Some(mime) => { if !mimes.contains(&mime) { - return Err(format!( - "File {} is not in the correct format", - field.key - )); + return Err(i18n.tf("ticket.rules.invalid_file_type", &[("field", field.key.clone())])); } mime } _ => { - return Err(format!("File {} is not in the correct format", field.key)) + return Err(i18n.tf("ticket.rules.invalid_file_type", &[("field", field.key.clone())])) } } } - _ => return Err(format!("File {} is not in the correct format", field.key)), + _ => return Err(i18n.tf("ticket.rules.invalid_file_type", &[("field", field.key.clone())])), }; let file_extension = &temp_file