[yew-devel] [PATCH proxmox-yew-comp v3 1/2] schema_validation: use Schema to populate field properties of number, check- and combobox

Hannes Laimer h.laimer at proxmox.com
Thu Oct 9 15:31:19 CEST 2025


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 <h.laimer at proxmox.com>
---
 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<AttrValue> = 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<AttrValue>)| {
+            schema.parse_simple_value(value)?;
+            Ok(())
+        });
+    }
+}
+
+// Generic implementation for Number<T>
+impl<T> SchemaValidation for pwt::widget::form::Number<T>
+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





More information about the yew-devel mailing list