mercredi 4 août 2021

Validation issues with HTML tags

The database saves the HTML tags from the input form and I would like to strip them to do correct validation. I tested the below method in IRB and it seems to work fine. However I cannot figure out how to use this method to do my validation.

Here's the code from my model:

class Task < ActiveRecord::Base
  validates strip_tag(:text), length: {minimum: 3}, uniqueness: true
  
  def strip_tag(record)
    record.split(/\<.*?\>/).map(&:strip).reject(&:empty?).join(' ').gsub(/\s,/,',')
  end
end

The issue I am facing is that am checking for uniqueness and the length to be greater than 3, but because of the HTML tags it's easy to create duplicates for example.

Example of a duplication after remove tags:

"<p><span style=\"color: #1d3d70; font-family: -apple-system, system-ui, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; font-size: 16px; background-color: #ffffff;\">Testing 8449</span></p>" 

and this one:

"<p>Testing 8449</p>" 

Both should have the same values after removing the HTML tags and I want to prevent this duplicates for example.

Aucun commentaire:

Enregistrer un commentaire