Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Radix Primitives How to submit value of Select component in Form component?

Tags:

radix-ui

I'm trying create a form using Radix Primities and submit the data to Next.js 13 server action like this,

<Form.Root action={create}>
  <Form.Field name="gender">
    <Form.Label>Gender</Form.Label>
    <Form.Control type="select" asChild>
      <select name="gender">
        <option value="m">Male</option>
        <option value="f">Female</option>
      </select>
    </Form.Control>
  </Form.Field>
  <Form.Submit>OK</Form.Submit>
</Form.Root>

the above is working correctly and I can receive the form data in my server action successfully,

export const create = (formData: FormData) => {
  // log the data in server action
  console.log(formData.get("gender"));
};

but I also want the accessibility and feactures from Select component so I thought I can insert the Select component like this,

<Form.Root action={create}>
  <Form.Field name="gender">
    <Form.Label>Gender</Form.Label>
    <Form.Control type="select" asChild>
      <Select.Root>
        ...
      </Select.Root>
    </Form.Control>
  </Form.Field>
  <Form.Submit>OK</Form.Submit>
</Form.Root>

but the server action is not able to get the value of gender. May I know how to use Select inside of Form component?

like image 840
user3357926 Avatar asked Oct 20 '25 00:10

user3357926


1 Answers

Add name attribute to your select:

<Select.Root name="gender">
  ...
</Select.Root>
like image 187
Ahmed Abdelbaset Avatar answered Oct 27 '25 05:10

Ahmed Abdelbaset



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!