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

ENH Improved validation #85

Merged

Conversation

emteknetnz
Copy link
Member

@emteknetnz emteknetnz commented Sep 26, 2023

Issue #17

Requires silverstripe/silverstripe-framework#11116 to be merged first before CI works

@emteknetnz emteknetnz force-pushed the pulls/4/support-validation branch 2 times, most recently from d08c4bd to de0bb19 Compare September 26, 2023 02:31
@emteknetnz emteknetnz changed the title ENH Validation NEW Improved validation Sep 26, 2023
@emteknetnz emteknetnz changed the title NEW Improved validation ENH Improved validation Sep 26, 2023
@emteknetnz emteknetnz force-pushed the pulls/4/support-validation branch 6 times, most recently from 326fcd0 to 0aa57cc Compare September 26, 2023 06:07
@emteknetnz emteknetnz force-pushed the pulls/4/support-validation branch 2 times, most recently from 9a87ead to f78ecb4 Compare September 28, 2023 02:05
@emteknetnz emteknetnz changed the base branch from 3 to 4 September 28, 2023 06:08
@purplespider
Copy link

purplespider commented Oct 10, 2023

For the ExternalLink field validation etc, can I suggest considering using https://github.com/burnbright/silverstripe-externalurlfield? (I do feel that module should really be part of the core, as it would help keep external links and their fields consistent between different modules).

src/Models/Link.php Outdated Show resolved Hide resolved
@emteknetnz emteknetnz force-pushed the pulls/4/support-validation branch 9 times, most recently from 7752fc7 to 10eca8d Compare January 30, 2024 05:08
@emteknetnz emteknetnz marked this pull request as ready for review January 30, 2024 05:09
lang/en.yml Outdated Show resolved Hide resolved
src/Models/EmailLink.php Show resolved Hide resolved
src/Models/ExternalLink.php Show resolved Hide resolved
Copy link
Member

@GuySartorelli GuySartorelli left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, will merge after framework PR.

README.md Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
@GuySartorelli
Copy link
Member

Works great on a page.

When I try in an inline-elemental block, the first time I try to create a link and trigger validation (i.e. an invalid link), the modal just closes and reopens.
Submitting again triggers validation and gives the message correctly, so it's just the first time submitting the modal form with an invalid link.

@emteknetnz
Copy link
Member Author

emteknetnz commented Feb 5, 2024

When I try in an inline-elemental block, the first time I try to create a link and trigger validation (i.e. an invalid link), the modal just closes and reopens. Submitting again triggers validation and gives the message correctly, so it's just the first time submitting the modal form with an invalid link.

I've spent half a day debugging this. This is seems like an extremely difficult issue to fix. Core issue is that the ElementList.js (possibly higher up) is re-rendering on validation failure, causing the nested LinkField and nested FormBuilderModal to themselves re-render.

LinkFields on Page don't have this issue because they're not nested inside a react component higher up that keeps re-rendering

This can be validated by putting in the following into (silverstripe/silverstripe-elemental) ElementEditor.js and putting breakpoints in and causing the validation to fail - componentDidUpdate() will trigger on the 1st validation failure. It will not trigger on the 2nd validation failure.

  componentDidMount() {
    var x = 1;
  }

  componentDidUpdate() {
    var x = 1;
  }

What's causing the re-render seems like its in the "magical blackbox" territory of JS Injector which does a bunch of stuff in registerTransforms.js and possibly redux-form. I don't really know. I've pretty confident that if we're able to get a fix for this it won't be in linkfield, it'll likely be in elemental though possibly admin instead

There's an outside chance there's some cross-over with silverstripe/silverstripe-admin#1662,

Debugging in entwine.js on root.render(<ElementEditorComponent {...props} />); this does NOT trigger a breakpoint the same way as other things do, so ElementEditor.js is the top-most bit

@emteknetnz
Copy link
Member Author

emteknetnz commented Feb 5, 2024

Update: Max traced it to this line - making it so that Injector isn't used fixes the issue and instead we just use <LinkModal> as per normal fixes issue

I've leave this code as is in this PR and look to fix or remove on this card

@GuySartorelli
Copy link
Member

GuySartorelli commented Feb 5, 2024

For my money, passing the injected modal as a prop or memoising it seems likely to resolve this.

@emteknetnz
Copy link
Member Author

emteknetnz commented Feb 5, 2024

I had a quick try with

// https://react.dev/reference/react/memo
import React, { memo } from 'react';

const LinkModal = memo(loadComponent(`LinkModal.${handlerName}`));

Didn't work

Passing in a prop (haven't tested) is kinda ugly because <LinkModalContainer> can come from either LinkField.js or LinkPicker.js

Passing as a prop from LinkPicker.js to LinkModalContainer.js didn't seem to work either

@GuySartorelli
Copy link
Member

Max came up with a solution. I've simplied it a lot, the only part that's really needed is setting the loadComponent result as state like so:

import React, { useState } from 'react';

// ...
const [ LinkModal, setLinkModal ] = useState(null);

// ...
if (LinkModal === null) {
  setLinkModal(() => loadComponent(`LinkModal.${handlerName}`));
}

You can wrap setting the state in a call to useEffect as well if you like (using [typeKey] as the second argument) - but in my (admittedly limited) test that didn't seem necessary.

@emteknetnz emteknetnz force-pushed the pulls/4/support-validation branch 3 times, most recently from 54ee80e to 493a294 Compare February 6, 2024 21:01
Copy link
Member

@GuySartorelli GuySartorelli left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Works great locally, just a tiny docs regression

README.md Outdated Show resolved Hide resolved
Copy link
Member

@GuySartorelli GuySartorelli left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@GuySartorelli GuySartorelli merged commit 4e9713c into silverstripe:4 Feb 6, 2024
10 checks passed
@GuySartorelli GuySartorelli deleted the pulls/4/support-validation branch February 6, 2024 21:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants