1
<input name="foo[]" ... >

I've used these before, but I'm wondering what it is called and results in various Google results only call it an "array" but I want to validate multiple fields in Express API which have dynamically added from the frontend side.
Frontend HTML:

<form>
    <div id="persons">
        <div data-col="1">
            <title>Person 1:</title>
            <label>First name</label>
            <input name="first_name[]">

            <label>Last name</label>
            <input name="last_name[]">

            <label>Email</label>
            <input name="email[]">
        </div>
        <div data-col="2">

            <title>Person 2:</title>
            <label>First name</label>
            <input name="first_name[]">

            <label>Last name</label>
            <input name="last_name[]">

            <label>Email</label>
            <input name="email[]">
        </div>
    </div>

    <button type="button">Add new person</button>
    <button type="submit">Submit</button>
</form>

In Express API, I am using express-validator to validate my fields. I want to validate request object on Express API where all fields must have equal number of elements on each array (first_name,last_name,email, etc..), and also each array have a different type of datatype like email should be validated as Email and other should be as String.
Request Object on Backend:

{
    "first_name[]": [
        "foo1",
        "foo2",
    ],
    "last_name[]": [
        "foo1",
        "foo2",
    ],
    "email[]": [
        "foo1",
        "foo2",
    ]
}

0 Answers0