revalidation icon indicating copy to clipboard operation
revalidation copied to clipboard

clear form

Open muhaimincs opened this issue 6 years ago • 4 comments

How do I clear the form once form values submitted? FYI, I use stateless component as container to the form. Please advice

muhaimincs avatar Dec 03 '17 16:12 muhaimincs

The onSubmit function accepts a callback. You could use the updateState(form) in the callback to reset the form. You could als reset the form via props. I can build a minimum example tomorrow if this would help.

busypeoples avatar Dec 03 '17 23:12 busypeoples

when I trigger updateState it turn on the error but does not clear the form

here is my enhance form

const validationRules = {
  substore_uuid: [[isNotEmpty, 'Sila buat pilihan stor']],
  quantity: [
    [isNotEmpty, 'Sila masukkan kuantiti'],
    [isInteger, 'Sila nombor bulat sahaja'],
  ],
};

const DistributeForm = ({onSubmit}) => {
  const initialState = {
    substore_uuid: '',
    quantity: '',
  };
  if (disabled) return null;
  return (
      <Form
        initialState={initialState}
        rules={validationRules}
        validateSingle={false}
        validateOnChange
        onSubmit={onSubmit}
      />
  );
};

and here is the actual form bind with revalidation

const StockForm = ({
  onSubmit: submitCb,
  max,
  disabled,
  revalidation: {form, valid, errors = {}, onSubmit, updateValueAndValidate},
}) => (
  <form
    onSubmit={e => {
      e.preventDefault();
      onSubmit(() => (valid ? submitCb(form) : null));
    }}>
    <div className="card-body">
      <div
        className={`form-group required ${errorClass(errors.substore_uuid)}`}>
        <span>Substor</span>
        <CommonSubstore
          name="substore_uuid"
          onChange={updateValueAndValidate}
        />
        <ErrorLabel err={errors.substore_uuid} />
      </div>
      <div className={`form-group required ${errorClass(errors.quantity)}`}>
        <span>Kuantiti Agihan</span>
        <input
          name="quantity"
          type="number"
          className="form-control"
          required
          max={max}
          onChange={updateValueAndValidate}
        />
      </div>
      {errors.quantity.map(err => <ErrorLabel key={err} err={err} />)}
    </div>
    <div className="card-footer">
      <button disabled={!valid || disabled} className="btn btn-success">
        Agih
      </button>
    </div>
  </form>
);

Right now I didn't clear the form yet but if I do, I'd do it in my enhance form

muhaimincs avatar Dec 04 '17 01:12 muhaimincs

I'll give you feedback later on today.

busypeoples avatar Dec 04 '17 09:12 busypeoples

@muhaimincs I'm trying to rebuild your example, to get it running, I'll need more information. Could you build a demo on https://codesandbox.io/ i.e.? We should have this solved.

busypeoples avatar Dec 16 '17 17:12 busypeoples