From bb73f3346ecbf799a1428448cd0cf7d480dd86c7 Mon Sep 17 00:00:00 2001 From: Manuel Gugger Date: Fri, 3 Feb 2023 18:06:14 +0100 Subject: [PATCH] add validation doc --- docs/content/docs/adding-crud-models.md | 2 ++ docs/content/docs/validation.md | 35 +++++++++++++++++++++++++ examples/basic/entity/comment.rs | 1 + 3 files changed, 38 insertions(+) create mode 100644 docs/content/docs/validation.md diff --git a/docs/content/docs/adding-crud-models.md b/docs/content/docs/adding-crud-models.md index c745b77..1e9cbdf 100644 --- a/docs/content/docs/adding-crud-models.md +++ b/docs/content/docs/adding-crud-models.md @@ -23,6 +23,8 @@ pub struct Model { pub id: i32, pub comment: String } + +impl ActixAdminModelValidationTrait for Entity {} ``` ## Derive Implementations diff --git a/docs/content/docs/validation.md b/docs/content/docs/validation.md new file mode 100644 index 0000000..9b3706c --- /dev/null +++ b/docs/content/docs/validation.md @@ -0,0 +1,35 @@ +--- +title: "Validation" +date: 2023-01-17T11:44:56+01:00 +draft: false +weight: 5 +--- + +# Validation + +Models can be validated before writing to the database. In order to validate, the ValidationTrait needs to be implemented as in the following example. + +```rust +#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Deserialize, Serialize, DeriveActixAdmin, DeriveActixAdminModel, DeriveActixAdminViewModel)] +#[sea_orm(table_name = "comment")] +pub struct Model { + #[sea_orm(primary_key)] + #[serde(skip_deserializing)] + #[actix_admin(primary_key)] + pub id: i32, + + pub my_decimal: Decimal +} + +impl ActixAdminModelValidationTrait for Entity { + fn validate(model: &ActiveModel) -> HashMap { + let mut errors = HashMap::new(); + + if model.my_decimal.clone().unwrap() < Decimal::from(100 as i16) { + errors.insert("my_decimal".to_string(), "Must be larger than 100".to_string()); + } + + errors + } +} +``` \ No newline at end of file diff --git a/examples/basic/entity/comment.rs b/examples/basic/entity/comment.rs index a38535e..0a97799 100644 --- a/examples/basic/entity/comment.rs +++ b/examples/basic/entity/comment.rs @@ -46,6 +46,7 @@ impl ActixAdminModelValidationTrait for Entity { if model.my_decimal.clone().unwrap() < Decimal::from(100 as i16) { errors.insert("my_decimal".to_string(), "Must be larger than 100".to_string()); } + errors } } \ No newline at end of file