From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) by lore.proxmox.com (Postfix) with ESMTPS id 753891FF26D for ; Thu, 9 Oct 2025 15:31:58 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 019F7312; Thu, 9 Oct 2025 15:32:05 +0200 (CEST) From: Hannes Laimer To: yew-devel@lists.proxmox.com Date: Thu, 9 Oct 2025 15:31:19 +0200 Message-ID: <20251009133120.425255-3-h.laimer@proxmox.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20251009133120.425255-1-h.laimer@proxmox.com> References: <20251009133120.425255-1-h.laimer@proxmox.com> MIME-Version: 1.0 X-Bm-Milter-Handled: 55990f41-d878-4baa-be0a-ee34c49e34d2 X-Bm-Transport-Timestamp: 1760016659150 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.043 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment RCVD_IN_VALIDITY_CERTIFIED_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to Validity was blocked. See https://knowledge.validity.com/hc/en-us/articles/20961730681243 for more information. RCVD_IN_VALIDITY_RPBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to Validity was blocked. See https://knowledge.validity.com/hc/en-us/articles/20961730681243 for more information. RCVD_IN_VALIDITY_SAFE_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to Validity was blocked. See https://knowledge.validity.com/hc/en-us/articles/20961730681243 for more information. SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Subject: [yew-devel] [PATCH proxmox-yew-comp v3 1/2] schema_validation: use Schema to populate field properties of number, check- and combobox X-BeenThere: yew-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Yew framework devel list at Proxmox List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Yew framework devel list at Proxmox Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: yew-devel-bounces@lists.proxmox.com Sender: "yew-devel" The idea is to have things like enum/selection items, defaults or property string validations only specified in one place. Signed-off-by: Hannes Laimer --- src/schema_validation.rs | 99 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 98 insertions(+), 1 deletion(-) diff --git a/src/schema_validation.rs b/src/schema_validation.rs index af28705..fbdf96d 100644 --- a/src/schema_validation.rs +++ b/src/schema_validation.rs @@ -2,7 +2,13 @@ use std::cell::RefCell; use std::collections::HashMap; use proxmox_schema::Schema; -use pwt::widget::form::{InputType, ValidateFn}; +use pwt::{ + props::FieldBuilder, + state::Store, + widget::form::{InputType, NumberTypeInfo, ValidateFn}, +}; +use serde_json::Value; +use yew::AttrValue; pub trait SchemaValidation { fn schema(mut self, schema: &'static Schema) -> Self @@ -33,12 +39,24 @@ impl SchemaValidation for pwt::widget::form::Field { self.max = s.maximum.map(|v| v as f64); self.step = Some(1.0); self.input_type = InputType::Number; + if let Some(value) = s.default { + self.set_placeholder(value.to_string()); + } } Schema::Number(s) => { self.min = s.minimum; self.max = s.maximum; self.step = Some(1.0); self.input_type = InputType::Number; + if let Some(value) = s.default { + self.set_placeholder(value.to_string()); + } + } + Schema::String(s) => { + self.set_tip(s.description.to_string()); + if let Some(default) = s.default { + self.set_placeholder(default.to_string()); + } } _ => {} } @@ -60,3 +78,82 @@ impl SchemaValidation for pwt::widget::form::Field { self.set_validate(validate); } } + +impl SchemaValidation for pwt::widget::form::Checkbox { + fn set_schema(&mut self, schema: &'static Schema) { + if let Schema::Boolean(s) = schema { + if let Some(value) = s.default { + self.set_default(value); + } + } + } +} + +impl SchemaValidation for pwt::widget::form::Combobox { + fn set_schema(&mut self, schema: &'static Schema) { + if let Schema::String(s) = schema { + if let Some(proxmox_schema::ApiStringFormat::Enum(e)) = s.format { + let items: Vec = e + .iter() + .map(|entry| entry.value.to_string().into()) + .collect(); + self.set_items(items.into()); + } + if let Some(value) = s.default { + self.set_placeholder(value); + } + } + self.set_validate(move |(value, _store): &(String, Store)| { + schema.parse_simple_value(value)?; + Ok(()) + }); + } +} + +// Generic implementation for Number +impl SchemaValidation for pwt::widget::form::Number +where + T: NumberTypeInfo, +{ + fn set_schema(&mut self, schema: &'static Schema) { + match schema { + Schema::Integer(s) => { + if let Some(minimum) = s.minimum { + if let Ok(min_value) = T::value_to_number(&Value::Number(minimum.into())) { + self.min = Some(min_value); + } + } + if let Some(maximum) = s.maximum { + if let Ok(max_value) = T::value_to_number(&Value::Number(maximum.into())) { + self.max = Some(max_value); + } + } + if let Some(default) = s.default { + self.set_placeholder(default.to_string()); + } + } + Schema::Number(s) => { + if let Some(minimum) = s.minimum { + if let Ok(min_value) = T::value_to_number(&Value::from(minimum)) { + self.min = Some(min_value); + } + } + if let Some(maximum) = s.maximum { + if let Ok(max_value) = T::value_to_number(&Value::from(maximum)) { + self.max = Some(max_value); + } + } + if let Some(default) = s.default { + self.set_placeholder(default.to_string()); + } + } + _ => {} + } + + self.set_validate(move |value: &T| { + let value_str = value.to_string(); + schema.parse_simple_value(&value_str)?; + Ok(()) + }); + } +} -- 2.47.3 _______________________________________________ yew-devel mailing list yew-devel@lists.proxmox.com https://lists.proxmox.com/cgi-bin/mailman/listinfo/yew-devel