Added error message when name field is empty

This commit is contained in:
xeovalyte 2024-03-12 14:43:41 +01:00
parent 3fc7420245
commit dcf535aa77
No known key found for this signature in database
4 changed files with 21 additions and 6 deletions

View File

@ -110,3 +110,8 @@ form {
width: 400px; width: 400px;
} }
.error {
color: red;
text-align: center;
font-size: 1.2em;
}

View File

@ -1,5 +1,5 @@
use leptos::*;
use application::App; use application::App;
use leptos::*;
fn main() { fn main() {
// set up logging // set up logging

View File

@ -19,13 +19,16 @@ pub fn Add() -> impl IntoView {
let (name, set_name) = create_signal(String::from("")); let (name, set_name) = create_signal(String::from(""));
let (group, set_group) = create_signal(String::from("A1")); let (group, set_group) = create_signal(String::from("A1"));
let (error, set_error) = create_signal(String::from(""));
let on_submit = move |ev: leptos::ev::SubmitEvent| { let on_submit = move |ev: leptos::ev::SubmitEvent| {
ev.prevent_default(); ev.prevent_default();
set_error.set(String::from(""));
websocket.add_person(name.get(), group.get()); match websocket.add_person(name.get(), group.get()) {
Ok(_) => set_name.set(String::from("")),
set_name.set(String::from("")) Err(err) => set_error.set(err),
}
}; };
view! { view! {
<h1>"Deelnemer toevoegen"</h1> <h1>"Deelnemer toevoegen"</h1>
@ -80,5 +83,8 @@ pub fn Add() -> impl IntoView {
/> />
<input type="submit" value="Submit" /> <input type="submit" value="Submit" />
</form> </form>
<p class="error">
{ error }
</p>
} }
} }

View File

@ -102,7 +102,11 @@ impl SurrealContext {
self.send(&json!(request).to_string()); self.send(&json!(request).to_string());
} }
pub fn add_person(&self, name: String, group: String) { pub fn add_person(&self, name: String, group: String) -> Result<(), String> {
if name.is_empty() {
return Err(String::from("Name cannot be empty"));
}
let request = SurrealRequest { let request = SurrealRequest {
id: 1, id: 1,
method: String::from("create"), method: String::from("create"),
@ -115,7 +119,7 @@ impl SurrealContext {
], ],
}; };
self.send(&json!(request).to_string()) Ok(self.send(&json!(request).to_string()))
} }
} }