I'm very new to Rust programming. I am learning to use it and Yew framework for a web project, I am now stuck on how to make the webpage input validation work.
Here is my code for implementing an input.rs component that is used in a login form.
#[derive(Properties, PartialEq, Validate, Clone)]
pub struct InputProps {
pub label: String,
pub input_type: AttrValue,
pub name: String,
pub value: String,
pub onchange: Callback<Event>,
pub validate_function: Callback<String,bool>,
}
#[function_component(Input)]
pub(crate) fn input(props: &InputProps) -> Html {
let html_id = format!("input-{}", props.name.clone());
let validate_function = props.validate_function.clone();
let validate_function_callback = Callback::from(move |event: Event| {
let target = event.target().unwrap().dyn_into::<HtmlInputElement>().unwrap();
let value = target.value();
validate_function.emit(value)
});
html! {
<>
<label for={html_id.clone()}>{props.label.clone()}</label>
<input
id={html_id}
type={props.input_type.clone()}
name={props.name.clone()}
value={props.value.clone()}
onchange={props.onchange.clone()}
validate_function={validate_function_callback}
/>
</>
}
}
When I try to build it, it always gives me this error:
error[E0277]: the trait bound `yew::Callback<yew::Event, bool>: IntoPropValue<std::option::Option<implicit_clone::unsync::string::IString>>` is not satisfied
--> src\components\input.rs:54:36
|
54 | validate_function={validate_function_callback}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `IntoPropValue<std::option::Option<implicit_clone::unsync::string::IString>>` is not implemented for `yew::Callback<yew::Event, bool>`
|
= help: the following other types implement trait `IntoPropValue<T>`:
<bool as IntoPropValue<ChildrenRenderer<VNode>>>
<char as IntoPropValue<ChildrenRenderer<VNode>>>
<isize as IntoPropValue<ChildrenRenderer<VNode>>>
<i8 as IntoPropValue<ChildrenRenderer<VNode>>>
<i16 as IntoPropValue<ChildrenRenderer<VNode>>>
<i32 as IntoPropValue<ChildrenRenderer<VNode>>>
<i64 as IntoPropValue<ChildrenRenderer<VNode>>>
<i128 as IntoPropValue<ChildrenRenderer<VNode>>>
and 47 others
For more information about this error, try `rustc --explain E0277`.