-
-
Notifications
You must be signed in to change notification settings - Fork 135
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
Comments
I suggest to make a fork and fix, and actually some changes were added recently, maybe this is not an issue anymore |
The problem still persists after updating to 0.9.7. I believe the issue is that the 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. |
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. |
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
And with that you will have your validations in place for ckeditor assets. |
@khhizr007 answer's is a good one. |
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
, andCkeditor::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)
The text was updated successfully, but these errors were encountered: