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

Clean up empty HTML and help text in form inputs #700

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

chiragchhatrala
Copy link
Collaborator

@chiragchhatrala chiragchhatrala commented Feb 13, 2025

Summary by CodeRabbit

  • New Features
    • Enhanced form submissions by automatically clearing empty or non-informative fields.
    • Improved rich text input behavior to normalize empty content, ensuring smoother and more consistent data entry.

Copy link
Contributor

coderabbitai bot commented Feb 13, 2025

Walkthrough

The pull request updates both backend and frontend components to sanitize input data. In the backend, the UserFormRequest class now includes a prepareForValidation() method, which iterates through a properties array and sets empty help fields to null. In the frontend, a watcher in the RichTextAreaInput.client.vue component monitors a bound value, setting it to null if the HTML-stripped content is empty.

Changes

File(s) Change Summary
api/.../UserFormRequest.php Added prepareForValidation() to iterate over properties and set empty help fields to null
client/.../RichTextAreaInput.client.vue Introduced a watcher for compVal to clear the value if the HTML-stripped content is empty

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant RequestHandler as UserFormRequest
    participant Validator

    Client->>RequestHandler: Submit Request
    RequestHandler->>RequestHandler: prepareForValidation()
    RequestHandler->>Validator: Forward sanitized data
Loading
sequenceDiagram
    participant User
    participant Component as RichTextAreaInput
    participant Watcher

    User->>Component: Input HTML content
    Component->>Watcher: Watch compVal change
    Watcher->>Component: Strip HTML & check if empty
    alt Content is empty
        Component->>Component: Set compVal to null
    else Content present
        Component->>Component: Retain current value
    end
    Component->>User: Update display
Loading

Poem

I'm a little rabbit, hopping in code so light,
Skipping through functions in the day and night.
I watch and clean each field with a joyful heart,
Resetting empties so our data can smartly start.
With a twitch of my nose and a playful skip,
I celebrate these changes with a carrot in my grip!
🥕🐇


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🧹 Nitpick comments (2)
client/components/forms/RichTextAreaInput.client.vue (1)

101-101: Consider using DOMParser for more reliable HTML parsing.

The current regex pattern /<[^>]*>/g might miss some edge cases. Using DOMParser would be more reliable for stripping HTML.

Apply this diff to improve HTML parsing:

-  if (val && val.replace(/<[^>]*>/g, '').trim() === '') {
+  if (val) {
+    const doc = new DOMParser().parseFromString(val, 'text/html')
+    if (doc.body.textContent.trim() === '') {
api/app/Http/Requests/UserFormRequest.php (1)

23-23: Consider using DOMDocument for more reliable HTML parsing.

Similar to the frontend change, using strip_tags() might miss some edge cases. Consider using DOMDocument for more reliable HTML parsing.

Apply this diff to improve HTML parsing:

- if (isset($property['help']) && strip_tags($property['help']) === '') {
+ if (isset($property['help']) && is_string($property['help'])) {
+     $dom = new \DOMDocument();
+     $dom->loadHTML($property['help'], LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
+     if (trim($dom->textContent) === '') {
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 29ef44d and dab8b22.

📒 Files selected for processing (2)
  • api/app/Http/Requests/UserFormRequest.php (1 hunks)
  • client/components/forms/RichTextAreaInput.client.vue (1 hunks)

Comment on lines +100 to +104
watch(compVal, (val) => {
if (val && val.replace(/<[^>]*>/g, '').trim() === '') {
compVal.value = null
}
}, { immediate: true })
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Emit update event instead of directly modifying the value.

The watcher directly modifies compVal.value which breaks the one-way data flow pattern in Vue. Instead, emit an update event to let the parent component handle the change.

Apply this diff to fix the implementation:

 watch(compVal, (val) => {
   if (val && val.replace(/<[^>]*>/g, '').trim() === '') {
-    compVal.value = null
+    emit('update:modelValue', null)
   }
 }, { immediate: true })
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
watch(compVal, (val) => {
if (val && val.replace(/<[^>]*>/g, '').trim() === '') {
compVal.value = null
}
}, { immediate: true })
watch(compVal, (val) => {
if (val && val.replace(/<[^>]*>/g, '').trim() === '') {
emit('update:modelValue', null)
}
}, { immediate: true })

Comment on lines +22 to +26
foreach ($data['properties'] as &$property) {
if (isset($property['help']) && strip_tags($property['help']) === '') { // Remove help if it's empty
$property['help'] = null;
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Add type checking and avoid reference modification.

The current implementation has two issues:

  1. No type checking for property['help']
  2. Using references (&$property) for array modification which can lead to unexpected behavior

Apply this diff to improve the implementation:

-    foreach ($data['properties'] as &$property) {
-        if (isset($property['help']) && strip_tags($property['help']) === '') { // Remove help if it's empty
-            $property['help'] = null;
-        }
+    $data['properties'] = array_map(function ($property) {
+        if (isset($property['help']) && is_string($property['help']) && strip_tags($property['help']) === '') {
+            $property['help'] = null;
         }
-    }
+        return $property;
+    }, $data['properties']);
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
foreach ($data['properties'] as &$property) {
if (isset($property['help']) && strip_tags($property['help']) === '') { // Remove help if it's empty
$property['help'] = null;
}
}
$data['properties'] = array_map(function ($property) {
if (isset($property['help']) && is_string($property['help']) && strip_tags($property['help']) === '') {
$property['help'] = null;
}
return $property;
}, $data['properties']);

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.

1 participant