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

Don't hardcode Form widget validation for Entry #4250

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from

Conversation

Jacalz
Copy link
Member

@Jacalz Jacalz commented Sep 14, 2023

Description:

This will likely fix #4147

Checklist:

  • Tests included.
  • Lint and formatter run with no errors.
  • Tests all pass.

Where applicable:

  • Public APIs match existing style and have Since: line.

@Jacalz

This comment was marked as outdated.

@coveralls
Copy link

coveralls commented Sep 14, 2023

Coverage Status

coverage: 64.559% (+0.009%) from 64.55%
when pulling 1905597 on Jacalz:form-should-not-hardcode-entry-for-validation
into 8458aff on fyne-io:develop.

@Jacalz Jacalz marked this pull request as ready for review November 8, 2023 21:57
@Jacalz
Copy link
Member Author

Jacalz commented Nov 8, 2023

The APIs here are very rough around the edges but I would like some opinions on it to try to shape it into something more fit for landing in the codebase. This should solve the problems we had with extended widgets.

@Jacalz
Copy link
Member Author

Jacalz commented Nov 8, 2023

Hmm. This does still not seem to fix the issue mentioned above. However, it no longer relates to there being an extended entry any more so that's a win. It only replicates inside the form dialog but not the form widget on its own. I suspect that there is a bug in ow and when he form updates the validation status to the parent.

@Jacalz Jacalz changed the title Introduce new API so Form doesn't have to hardcode validation for Entry Don't hardcode Form widget validation for Entry Nov 8, 2023
@Jacalz Jacalz marked this pull request as draft November 8, 2023 22:19
Copy link
Member

@andydotxyz andydotxyz left a comment

Choose a reason for hiding this comment

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

I think the logic in naming ShouldDisplayValidation is backwards...

Also this introduces a new type with 4 methods for what looks like a single new function - can you share why?
Lastly, can this not be done with an anonymous interface instead of defining new APIs? If the fix was to resolve extended Entries we don't need to expose any new type as it is a case of Entry implementing a known function only...

@Jacalz
Copy link
Member Author

Jacalz commented Nov 8, 2023

I think the logic in naming ShouldDisplayValidation is backwards...

Hehe yeah indeed. Nice catch 🙈

Also this introduces a new type with 4 methods for what looks like a single new function - can you share why?

I'm entirely aware that this API is very rough around the edges. I just basically added a new method incrementally for each place where we were using internal API details from Entry. It definitely needs to be cleaned up, like #4250 (comment) and potentially more. However, I doubt that all four methods can be added as only one. I'm more than happy to receive suggestions, or work together with someone, to try to find a cleaner solution :)

I will try to clean up the API and slim it down now that it actually works like before minus the Entry specific code. However, it would be nice with some thoughts about better solutions to the internal API usage that gets removed here.

Lastly, can this not be done with an anonymous interface instead of defining new APIs? If the fix was to resolve extended Entries we don't need to expose any new type as it is a case of Entry implementing a known function only...

It can yes, but it avoids parts of what I'm trying to achieve here. The idea was never to work on this just to fix the issue. I was just hoping to fix it at the same time by more generally making the Form not hardcode validation to Entry. Basically, anyone should be able to create a widget that has enough context to display useful validation messages in a Form.

This also doesn't even fix the issue linked above. It just happens to make the extended entry work the same as the not extended one 😅

@Jacalz
Copy link
Member Author

Jacalz commented Nov 18, 2023

I have looked at this some more now and I don't quite know how to simplify it any more. I can't think of a way of reworking Form to not require the methods that I have added.

@Jacalz Jacalz marked this pull request as ready for review November 18, 2023 11:29
@Jacalz Jacalz force-pushed the form-should-not-hardcode-entry-for-validation branch from 6f401ff to 1905597 Compare January 7, 2024 16:15
@andydotxyz
Copy link
Member

Sorry I let this sit for a while, I will take a closer look and see why each of these methods is needed and try to think of simplifications myself.

Copy link
Member

@andydotxyz andydotxyz left a comment

Choose a reason for hiding this comment

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

Thanks for this, good to support extendible types etc for validation.
However I think the new interface exposes, or requires, to much - see my comments inline. I think we can leave it at HasValidator and ShouldDisplayValidation - these are clearly extra metadata about validation so fits within the interface idea.

@@ -47,6 +63,14 @@ func (e *Entry) SetValidationError(err error) {
}
}

// ShouldDisplayValidation returns true if the entry has not been interacted with
Copy link
Member

Choose a reason for hiding this comment

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

The dirty check seems to be backwards here - if it is not dirty it should not display validation?


// SetOnFocusChanged is intended for parent widgets or containers to hook into the change of focus.
// The function might be overwritten by a parent that cares about child validation (e.g. widget.Form).
SetOnFocusChanged(func(bool))
Copy link
Member

Choose a reason for hiding this comment

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

This feels like exposing internal details - I can see why it was needed but I don't think it's the cleanest way.
Can't we wrap the widgets if they are already Focusable and intercept the FocusGained and FocusLost?

SetOnFocusChanged(func(bool))

// SetValidationError manually updates the validation status until the next input change.
SetValidationError(error)
Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure about this either - it means that validation moves from a action to a state.
I would look carefully at where it is used now - I think that the new methods added should remove the necessity to set an initial state, especially if we have interception of focus events.

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.

None yet

3 participants