Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setValue setting attribute to undefined react hook form

Im using react-hook-form in my project i want to assign one field to be pre filled on some condition but set value not working properly although it validation properly but not setting the value here is my form field

<div className="item-input hal-input hal-margin-bottom-0 item-input-field not-empty-state">
  <select
    disabled={
      activeStep === 2 || activeStep === 3 || dataForDropdown?.length == 1
    }
    {...register("issuerAccount", {
      required: true,
    })}
    onChange={(e) => setNickname(e)}
    className="hal-input-select account-selector "
    data-type="account"
  >
    <option value="">- Select -</option>
    {dataForDropdown?.map((x, i) => (
      <option value={x.id} selected={dataForDropdown?.length == 1}>
        {x.id}
      </option>
    ))}
  </select>
  {errors.issuerAccount && (
    <span className="hal-validation_errors creditorAccount">
      {literals("newSubscription.field.requiredText")}
    </span>
  )}
</div>;

here is data

useEffect(() => {
  if (issuer)
    setDataForDropdown(
      issuer.accounts.map((x) => ({
        id: x.accountNumber,
        text: x.branchName,
        selected: true,
      }))
    );

  if (issuer && issuer.accounts.length == 1) {
    console.log(issuer.accounts[0].accountNumber);
    setValue("issuerAccount", issuer.accounts[0].accountNumber);
  }
}, [issuer]);

i want to set issuerAccount field but I get the undefined on form submission

like image 553
Asad Avatar asked Jun 13 '26 18:06

Asad


1 Answers

so the problems was I had disabled the select element thats why it was saving undefined on form object.

like image 108
Asad Avatar answered Jun 17 '26 01:06

Asad



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!