Form: input fields. Get the values of input fields with the same names - type: Option<Vec<String>>

Html + javascript: an arbitrary number of (input name="duplicate") are created on the page.
How to arrange the enumeration of input fields..
On golang it looks like this..

func psForm(w http.ResponseWriter, r *http.Request) ([]time.Time, error) {

	err := r.ParseForm()
	if err != nil {
		fmt.Fprintf(w, "Error ParseForm..! : %+v\n", err)
	}

	ps := r.Form["list"]

	on_off := make([]string, len(ps))
	for k, v := range ps {
		on_off[k] = v
	}

	list := make([]time.Time, 0, len(ps))
	for k, v := range ps {
		if on_off[k] != "" {
			ss := Converter(w, v)
			list = append(list, ss)
		}
	}
	return list, err
}

rust is something like that..

use axum_extra::extract::Form;
pub async fn post_creat(
    State(pool): State<PgPool>,
    TypedHeader(cookie): TypedHeader<Cookie>,
    Extension(templates): Extension<Templates>,
    Form(form): Form<FormSch>,
) -> impl IntoResponse {

    let token = auth::views::request_token(TypedHeader(cookie)).await;

    println!("form..{:?}", form);

    let s_val = form.st_hour.as_deref().unwrap_or("err..");
    let e_val = form.en_hour.as_deref().unwrap_or("err..");
    let start: Option<NaiveDateTime> = if form.st_hour.is_some() {
        Some(
            NaiveDateTime::parse_from_str(s_val, "%Y-%m-%dT%H:%M").unwrap()
        )
    } else {
        None
    };
    let end: Option<NaiveDateTime> = if form.en_hour.is_some() {
        Some(
            NaiveDateTime::parse_from_str(e_val, "%Y-%m-%dT%H:%M").unwrap()
        )
    } else {
        None
    };

    let l_val = form.list.as_deref().unwrap();
    let mut hours = Some(Vec::new());
    if form.list.is_some() {
        for i in l_val {
            if !i.is_empty() {
                hours.as_mut().expect("REASON").push(NaiveDateTime::parse_from_str(i, "%Y-%m-%dT%H:%M").unwrap())
            }
        }
    } else {
        hours = None
    }

    let result = sqlx::query(
        "INSERT INTO schedule (user_id, title, description, st_hour, en_hour, hours, created_at) VALUES ($1,$2,$3,$4,$5,$6,$7)"
        )
        .bind(token.clone().unwrap().claims.id)
        .bind(form.title.clone())
        .bind(form.description.clone())
        .bind(start)
        .bind(end)
        .bind(&hours)
        .bind(Utc::now())
        .execute(&pool)
        .await;
    match result {
        Ok(result) => result,
        Err(err) => {
            let mut context = Context::new();
            context.insert("err_token", &err.to_string());
            return Err(Html(templates.render("creat", &context).unwrap()));
        }
    };
    Ok(Redirect::to("/schedule/all").into_response())
}

Can you give an example input and the corresponding output?

input

    function addFn() {
        const divEle = document.getElementById("addinput");
        const wrapper = document.createElement("div");
        const iFeild = document.createElement("input");
        iFeild.setAttribute("type", "datetime-local");
        iFeild.setAttribute("name", "list");
        iFeild.setAttribute("placeholder", "Enter value");
        iFeild.classList.add("form-control");
        iFeild.classList.add("mt-2");
        wrapper.appendChild(iFeild);
        divEle.appendChild(wrapper);
    }

output
let list = ??? as axum::extract::Form
let parse_form:Vec = vec![list];

That's not input and output, those are two pieces of code.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.