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

Validations not working on Ckeditor gem models? #145

Open
nkirby-nxt opened this issue Feb 24, 2022 · 5 comments
Open

Validations not working on Ckeditor gem models? #145

nkirby-nxt opened this issue Feb 24, 2022 · 5 comments
Labels
bug Something isn't working help wanted Extra attention is needed

Comments

@nkirby-nxt
Copy link

Hello! I am unsure if this should mentioned here or under the ckeditor gem...

I have noticed that adding validations for file content types and sizes for Ckeditor::Picture, Ckeditor::Asset, and Ckeditor::AttachmentFile models do not seem to work.

Adding:
validates :storage_data, size: { less_than: 5.megabytes }, content_type: /\Aimage\/.*\z/
or
validates :storage_data, size: { less_than: 5.megabytes }, content_type: ['image/png', 'image/jpg', 'image/jpeg']
in any of the above listed models do not restrict a large image or a pdf from being uploaded. The types seem to be registered with Marcel::EXTENSIONS

ckeditor gem (5.1.1)
active_storage_validations gem (0.9.6)

@igorkasyanchuk
Copy link
Owner

I suggest to make a fork and fix, and actually some changes were added recently, maybe this is not an issue anymore

@igorkasyanchuk igorkasyanchuk added the help wanted Extra attention is needed label Mar 5, 2022
@nkirby-nxt
Copy link
Author

The problem still persists after updating to 0.9.7. I believe the issue is that the ActiveStorageValidations module is loaded well before the Ckeditor modules in the ancestor chain on the Ckeditor models stated above. The validations run before the Ckeditor before_save callback.

I could be wrong, but I believe this is what is allowing a newly created Ckeditor::Picture to have the incorrect attributes and side-step the validations.

@khhizr07
Copy link

Hi any updates on this? Currently I am trying to move from paperclip to active storage and I am facing the same issue. I have tried different ways to use the validations on the attachment but it is not working.

@Mth0158 Mth0158 added the bug Something isn't working label Jan 5, 2024
@khhizr007
Copy link

khhizr007 commented Mar 27, 2024

If any one still facing this problem, I created a work around for this. You can use validations by making a custom validator for ckeditor. The code for the validator would look something like this

class CkeditorAssetValidator < ActiveModel::Validator
  MAX_PICTURE_SIZE = 3.megabytes
  MAX_FILE_SIZE = 5.megabytes

  def validate(record)
    return if valid_size?(record)

    record.errors.add(:storage_data, 'File size exceeds the maximum allowed size.')
    raise ActiveRecord::RecordInvalid
  end

  private

    def valid_size?(record)
      max_attachment_size = record.type == 'Ckeditor::Picture' ? MAX_PICTURE_SIZE : MAX_FILE_SIZE
      record.data.present? && record.data.size <= max_attachment_size
    end
end

Now inside you asset.rb class of ckeditor, you can add this line

validates_with CkeditorAssetValidator

And with that you will have your validations in place for ckeditor assets.
And since we already have validations that work for extensions we don't need to include them here as well.

@Mth0158
Copy link
Collaborator

Mth0158 commented Apr 1, 2024

@khhizr007 answer's is a good one.
I wish I had time to solve this issue, but I am quite busy these days, feel free to drop a PR on the matter. I'll take time to review it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

5 participants