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

fix: add form validation for survey's colors properties #28450

Merged
merged 13 commits into from
Feb 11, 2025

Conversation

lucasheriques
Copy link
Contributor

@lucasheriques lucasheriques commented Feb 7, 2025

Problem

adds form validation to CSS colors inside Surveys' form to prevent rendering issues

Changes

CleanShot 2025-02-07 at 15 18 34@2x

👉 Stay up-to-date with PostHog coding conventions for a smoother review.

Does this work well for both Cloud and self-hosted?

How did you test this code?

Tested if validation worked as expected locally

@lucasheriques lucasheriques self-assigned this Feb 7, 2025
@lucasheriques lucasheriques requested a review from a team February 7, 2025 18:21
@lucasheriques lucasheriques marked this pull request as ready for review February 7, 2025 18:22
Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

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

PR Summary

Added form validation for CSS color properties in survey forms to prevent rendering issues, with visual error feedback for invalid color inputs.

  • Added validateColor function in surveyLogic.tsx using CSS.supports() to validate color values before rendering
  • Created reusable LemonFieldError component in LemonField.tsx for consistent error display across forms
  • Added .ignore-error-border class in LemonInput.scss to allow selective opt-out of error border styling
  • Implemented validation for all color inputs (background, border, buttons) in SurveyCustomization.tsx with error messages

4 file(s) reviewed, 2 comment(s)
Edit PR Review Bot Settings | Greptile

@posthog-bot
Copy link
Contributor

📸 UI snapshots have been updated

2 snapshot changes in total. 0 added, 2 modified, 0 deleted:

  • chromium: 0 added, 2 modified, 0 deleted (diff for shard 2)
  • webkit: 0 added, 0 modified, 0 deleted

Triggered by this commit.

👉 Review this PR's diff of snapshots.

Copy link
Contributor

github-actions bot commented Feb 7, 2025

Size Change: +5 B (0%)

Total Size: 1.18 MB

ℹ️ View Unchanged
Filename Size Change
frontend/dist/toolbar.js 1.18 MB +5 B (0%)

compressed-size-action

@marandaneto
Copy link
Member

@lucasheriques does this validation also work for the settings page?
https://us.posthog.com/project/{surveyId}/surveys?tab=settings since you can also change settings in the org level

Comment on lines -140 to -190
{tab === SurveysTabs.Settings && (
<>
<div className="flex items-center gap-2 mb-2">
<LemonField.Pure className="mt-2" label="Appearance">
<span>These settings apply to new surveys in this organization.</span>
</LemonField.Pure>

<div className="flex-1" />
{globalSurveyAppearanceConfigAvailable && (
<LemonButton
type="primary"
onClick={() => {
updateCurrentTeam({
survey_config: {
...currentTeam?.survey_config,
appearance: {
...currentTeam?.survey_config?.appearance,
...editableSurveyConfig,
},
},
})
}}
>
Save settings
</LemonButton>
)}
</div>
<LemonDivider />
<div className="flex gap-2 mb-2 align-top">
<Customization
key="survey-settings-customization"
appearance={editableSurveyConfig}
hasBranchingLogic={false}
customizeRatingButtons={true}
customizePlaceholderText={true}
onAppearanceChange={(appearance) => {
setEditableSurveyConfig({
...editableSurveyConfig,
...appearance,
})
setTemplatedSurvey({
...templatedSurvey,
...{ appearance: appearance },
})
}}
/>
<div className="flex-1" />
<div className="mt-10 mr-5 survey-view">
{globalSurveyAppearanceConfigAvailable && (
<SurveyAppearancePreview survey={templatedSurvey} previewPageIndex={0} />
)}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

no functionality changes here, just moved this logic to its own component

Comment on lines 36 to 64
const updateSurveySettings = (): void => {
const errors = {
backgroundColor: validateColor(editableSurveyConfig.backgroundColor, 'background color'),
borderColor: validateColor(editableSurveyConfig.borderColor, 'border color'),
ratingButtonActiveColor: validateColor(
editableSurveyConfig.ratingButtonActiveColor,
'rating button active color'
),
ratingButtonColor: validateColor(editableSurveyConfig.ratingButtonColor, 'rating button color'),
submitButtonColor: validateColor(editableSurveyConfig.submitButtonColor, 'button color'),
submitButtonTextColor: validateColor(editableSurveyConfig.submitButtonTextColor, 'button text color'),
}

// Check if there are any validation errors
const hasErrors = Object.values(errors).some((error) => error !== undefined)
setValidationErrors(errors)

if (hasErrors) {
return
}

// If no errors, proceed with the update
updateCurrentTeam({
survey_config: {
...currentTeam?.survey_config,
appearance: editableSurveyConfig,
},
})
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this is the only logic changes for the update settings call

@lucasheriques
Copy link
Contributor Author

@marandaneto now validating on the settings page too! took a while as had to make changes to the code so it worked everywhere

@posthog-bot
Copy link
Contributor

📸 UI snapshots have been updated

18 snapshot changes in total. 0 added, 18 modified, 0 deleted:

Triggered by this commit.

👉 Review this PR's diff of snapshots.

@@ -31,7 +31,7 @@
background-color: inherit;
}

.Field--error &,
.Field--error &:not(.ignore-error-border),
Copy link
Member

Choose a reason for hiding this comment

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

could this cause issues since its a shared component across the app?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't think so, as this class is not used anywhere else

Copy link
Member

Choose a reason for hiding this comment

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

I see a lot of usages https://github.com/search?q=repo%3APostHog%2Fposthog+%3CLemonInput+&type=code
just double check its alright.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yeah I meant for this class specifically ignore-error-border. And the CSS rule will still be applied normally, just to not fields that have it

return undefined
}
// Test if the color value is valid using CSS.supports
const isValidColor = CSS.supports('color', color)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
const isValidColor = CSS.supports('color', color)
let isValidColor = CSS.supports('color', color)
if (!isValidColor) isValidColor = CSS.supports('color', `#${color}`)

I think it's worth trying to add a # and try again if it fails, either here or elsewhere.
Attention that the given color will be mutated in this case, the caller will need to use the new value if it turns out to the a missing # issue

Copy link
Contributor Author

Choose a reason for hiding this comment

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

created a new function to sanitize the survey's appearance, this way it'll work even without the #

Copy link
Member

@marandaneto marandaneto left a comment

Choose a reason for hiding this comment

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

left a few comments/suggestions but otherwise LGTM

@lucasheriques lucasheriques enabled auto-merge (squash) February 11, 2025 17:51
@lucasheriques lucasheriques merged commit ce16d9f into master Feb 11, 2025
103 checks passed
@lucasheriques lucasheriques deleted the fix/add-form-validation-for-colors branch February 11, 2025 17:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants