sp-dev-fx-controls-react icon indicating copy to clipboard operation
sp-dev-fx-controls-react copied to clipboard

[Bug][DynamicForm] OnBeforeSubmit does not fire when using a mandatory field in the FieldOverrides

Open Benatiabacemdev opened this issue 9 months ago • 2 comments

Category

[ ] Enhancement

[X] Bug

[ ] Question

Version

Please specify what version of the library you are using: [3.20.0]

Expected / Desired Behavior / Question

In DynamicForm, when I use fieldoverrides with a required field, onBeforeSubmit does not fire and the form is not submitted. By default the field was red (error) and when I type something it turns black and without an error message, but when I click submit nothing happens. if I update the list field as non-required, the dynamic form works fine.

Observed Behavior

In DynamicForm, when I use fieldoverrides with a required field, onBeforeSubmit does not fire and the form is not submitted.

Steps to Reproduce

By default the field was red (error) and when I type something it turns black and without an error message, but when I click submit nothing happens. if I update the list field as non-required, the dynamic form works fine.

Thanks!

Benatiabacemdev avatar Feb 11 '25 12:02 Benatiabacemdev

Thank you for submitting your first issue to this project.

github-actions[bot] avatar Feb 11 '25 12:02 github-actions[bot]

Same issue here!

BlazeOfFeathers avatar Mar 15 '25 15:03 BlazeOfFeathers

Same here. The problem seems to be that for fieldOverrides, the forms field-onChange handler is not triggered, leading to the fields newValue not being set in the DynamicForm.

This did work correctly in version 3.15.0 though...

martinlingstuyl avatar Apr 08 '25 12:04 martinlingstuyl

Ok, so the point is you need to let the DynamicForm know that a field has changed.

So if your override contains a TextField control for example, you'll need to implement an event handler, like onChange or onBlur. In the event you call the onChanged function in the fieldProperties, like so:

private renderField = (
    fieldProperties: IDynamicFieldProps
  ): React.ReactElement<IDynamicFieldProps> => {
    return (
      <>
        <div className={styles.fieldWrapper}>
          <TextField
            label={strings.SiteName}
            defaultValue={fieldProperties.value}
            onChange={(ev, newValue) =>
              fieldProperties.onChanged(fieldProperties.columnInternalName, newValue, true);
            }
            required={fieldProperties.required}
            disabled={fieldProperties.disabled}
            placeholder={fieldProperties.placeholder}
            autoComplete="off"
          />
          <text className={styles.fieldDescription}>
            {fieldProperties.description}
          </text>
        </div>
      </>
    );
  };

in version 3.15.0 you could use fieldValues.newValue = newValue in the event. But now you need to call the onChanged function.

martinlingstuyl avatar Apr 08 '25 14:04 martinlingstuyl

Tried approach from @martinlingstuyl - does not seem to work at all: onBeforeSubmit does not fire. Using SPFx 1.21.1 and 3.21.0 of @pnp/spfx-controls-react. Disabling required for the fields seems to have no effect either. Stopped working post upgrade from SPFx 1.17.14 and 3.14 correspondingly.

sugar-dr avatar Jun 19 '25 20:06 sugar-dr

Could you share something of the code? If required is not the issue here it would seem to be something else entirely.

martinlingstuyl avatar Jun 20 '25 04:06 martinlingstuyl

Sure thing: function renderSiteNameField(fieldProperties: IDynamicFieldProps): React.ReactElement<IDynamicFieldProps> { ... return <> <div className={styles.fieldWrapper}> <TextField label={"Site Name"} //{fieldProperties.fieldTitle} onRenderLabel={(p:ITextFieldProps, dr:IRenderFunction<ITextFieldProps>) => labelRenderer(p, dr, "TextField")} defaultValue={fieldProperties.defaultValue} onChange={(ev, newValue:string) => onChangeSiteNameField(fieldProperties, newValue)} //required={true}//{fieldProperties.required} //disabled={fieldProperties.disabled} placeholder={fieldProperties.placeholder} autoComplete="off" />....

function onChangeSiteNameField(fieldProperties: IDynamicFieldProps, newValue: string): void { ...

//fieldProperties.newValue = newValue;
fieldProperties.onChanged?.(fieldProperties.columnInternalName,newValue,true);

Please note: list column (Site Name) was made NOT required in the list too

sugar-dr avatar Jun 20 '25 11:06 sugar-dr

Are you getting any exceptions in the console?

And is the . in fieldProperties.onChanged?.(fieldProperties.columnInternalName,newValue,true); a typo?

I'd expect fieldProperties.onChanged(fieldProperties.columnInternalName,newValue,true);

martinlingstuyl avatar Jun 20 '25 11:06 martinlingstuyl