简体   繁体   中英

ReactJS: Using value from form - radio button

I have a form with some variables:

  const [formModel, setFormModel] = useState({
    realm: '',
    deletedDate: 'all',
  });

the deletedDate is a radio button that should have as value -> "all", "true", "false"

the form is:

<AvForm model={formModel} onSubmit={filterResults} onReset={handleFormReset}>
 <AvGroup>
                    <Label id="realm" for="realm-realm">
                      Realm Name
                    </Label>
                    <AvField
                      id="realm"
                      data-cy="realm"
                      type="text"
                      name="realm"
                      onChange={handleChange}
                      value={formModel.realm}
                    />
                  </AvGroup>
<FormGroup check>
                      <Label id="deletedDate" check>
                        <Input
                          id="deletedDate"
                          data-cy="deletedDate"
                          type="radio"
                          name="deletedDate"
                          value="all"
                          checked={formModel.deletedDate === 'all'}
                          onChange={handleChange}
                        />{' '}
                        All
                      </Label>
                      <br></br>
                      <Label id="deletedDate" check>
                        <Input
                          id="deletedDate"
                          data-cy="deletedDate"
                          type="radio"
                          name="deletedDate"
                          value="true"
                          checked={formModel.deletedDate === 'true'}
                          onChange={handleChange}
                        />{' '}
                        Yes
                      </Label>
                    </FormGroup>
                    <FormGroup check>
                      <Label id="deletedDate" check>
                        <Input
                          id="deletedDate"
                          data-cy="deletedDate"
                          type="radio"
                          name="deletedDate"
                          value="false"
                          checked={formModel.deletedDate === 'false'}
                          onChange={handleChange}
                        />{' '}
                        No
                      </Label>
                    </FormGroup>

now when I submit the form I call the filterResults:

const handleChange = e => {
    console.log('e ', e);
    console.log('deletedDate ', formModel.deletedDate);
    const { name, value } = e.target;
    console.log('name ', name, ' value ', value);
    setFormModel(prevState => ({
      ...prevState,
      [name]: value,
    }));
    console.log('deletedDate ', formModel.deletedDate);
  };

  const filterResults = (event, errors, values) => {
    console.log('values ', values, 'event ', event, ' errors ', errors);
    let entity = null;
    if (errors.length === 0) {
      entity = {
        ...values,
      };
    }
    console.log('entity is ', entity);
    if (entity) {

Now My problem is:

In the filterResults I have inserted a console log to see the values.. when I submit the form I can read only the value about the realm and nothing about the deletedDate and I don't understand why. How can I do to use in the filterResult the deletedDate?? What is wrong with my form? thank you

looks like you are using two different form which when you submit while using the props of the AV form you dont have access to other form values, i would recommend to store these values in the same form

 <AvForm model={formModel} onSubmit={filterResults} onReset={handleFormReset}>
 <AvGroup>
                    <Label id="realm" for="realm-realm">
                      Realm Name
                    </Label>
                    <AvField
                      id="realm"
                      data-cy="realm"
                      type="text"
                      name="realm"
                      onChange={handleChange}
                      value={formModel.realm}
                    />
                  </AvGroup>
<AvGroup>
                    <Label id="realm" for="realm-realm">
                      Radio Input
                    </Label>
                    <AvField
                      id="deletedDate"
                      data-cy="deletedDate"
                      type="type value this for uses for radio inputs"
                      name="deletedDate"
                      onChange={handleChange}
                      value={formModel.deletedDate}
                    />
                  </AvGroup>

, but if you want to make this work, I would suggest get your values from the state, here you are storing your values in the state but not doing anything with them

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM