Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Manually modify initialValues #191

Open
georgeemr opened this issue Jun 16, 2023 · 1 comment
Open

Manually modify initialValues #191

georgeemr opened this issue Jun 16, 2023 · 1 comment
Labels
enhancement New feature or request

Comments

@georgeemr
Copy link

georgeemr commented Jun 16, 2023

Is there a way that I can modify the value of the initialValues when using the helper components?
I want to change 3 inputs when one input changes because I want to get from the server the values.

const initialValues = {
identification: "", // --> When input, i want to change firstname, lastname, and lastname2 with values from the server
firstName: "",
lastName1: "",
lastName2: ""
};

<div class="form-group">
      <label for="identification"
        >Número de identificación<span class="required">*</span></label
      >
      <!-- <input
        on:change={handleIdentificationInput}
        class="form-control"
      /> -->
      <Field name="identification" type="text" class="form-control" />
      <ErrorMessage class="form-error" name="identification" />
    </div>
    <div class="row">
      <div class="col-lg-12">
        <div class="form-group">
          <label for="firstName">Nombre<span class="required">*</span></label>
          <Field
            name="firstName"
            type="text"
            class="form-control"
            disabled={!nombreCustom}
          />
          <ErrorMessage class="form-error" name="firstName" />
        </div>
      </div>
      <div class="col-lg-6">
        <div class="form-group">
          <label for="lastName1">Apellido1<span class="required">*</span></label
          >
          <Field
            name="lastName1"
            type="text"
            class="form-control"
            disabled={!nombreCustom}
          />
          <ErrorMessage class="form-error" name="lastName1" />
        </div>
      </div>
      <div class="col-lg-6">
        <div class="form-group">
          <label for="lastName2">Apellido2<span class="required">*</span></label
          >
          <Field
            name="lastName2"
            type="text"
            class="form-control"
            disabled={!nombreCustom}
          />
          <ErrorMessage class="form-error" name="lastName2" />
        </div>
      </div>
    </div>

I'm unable to call a custom function and change the values directly because the Form don;t detect the change and because of that the form says the value is required.

@georgeemr georgeemr added the enhancement New feature or request label Jun 16, 2023
@taronaeo
Copy link

taronaeo commented Jul 7, 2023

@georgeemr createForm exposes a Svelte writable store named form. In your case, you can use a reactive block to fetch the data from the server when the form changes, and use .update() from the store to update your values.

E.g.,

$: {
  // fetch data from API
  form.update(data => ({ ...data, firstName: API_RESULT_FIRSTNAME, lastName: API_RESULT_LASTNAME, lastName1: API_RESULT_LASTNAME1 }))
}

Edit: specify which Svelte writable store is exposed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants