Skip to content

How to convert chrono::naivedatetime from str and to naivedatetime using Rocket Forms? #2606

Answered by ksaadDE
ksaadDE asked this question in Questions
Discussion options

You must be logged in to vote

I solved my question three days ago. I just forgot to post it. The code above is basically the solution

#[rocket::async_trait]
impl<'r> FromFormField<'r> for NaiveDateTimeForm {
    fn from_value(field: ValueField<'r>) -> form::Result<'r, Self> {
        println!("{}", field.value);
        match NaiveDateTime::parse_from_str(field.value, "%Y-%m-%dT%H:%M") {
            Ok(date)=> Ok(NaiveDateTimeForm(date)),
            Err(e) => panic!("{}", e),
//             Err(e) =>Err(e),
        }
    }

    async fn from_data(_field: DataField<'r, '_>) -> form::Result<'r, Self> {
        unimplemented!()
    }
}

However, what I did not try yet is loading data from the DB into the Form. I only use…

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by ksaadDE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
1 participant