Keystone-classic: Keystone Beta 4.0.0-beta.5 - required being ignored for type: Types.File and image preview not working

Created on 3 Mar 2017  路  10Comments  路  Source: keystonejs/keystone-classic

The admin is ignoring that a file is required and also it's image preview:

image: {
    type: Types.File,
    storage: storage,
    filename: function (item, file) {
        return item.id + '.' + file.extension
    },
    format: function (item, file) {
        return '<img src="' + file.href + '" style = "max-width: 120px">'
    },
    allowedTypes: ['jpg', 'png', 'jpg', 'gif'],
    required: true,
    initial: true,
},

image

bug help wanted

Most helpful comment

+1 this is still an issue and is terribly frustrating

All 10 comments

I have the same problem with the image preview. The Admin UI is ignoring the format function provided to the model.

For the following code:

fieldName: {
    type: Types.File,
    option1: "foo",
    option2: "bar",
}

Types.File is a constructor which gets constructed with the options here.

The logic for FileType exists in this file, you can see some logic related to formatting in there as well.

I'm just passing through this issue quickly, but hopefully this gives you some context that you might be able to figure out what is happening on your own.

Any updates on this?

Any luck on this?

How do you do it now?

@JedWatson I'm looking at the validation half of this issue and have come across your comment in validateRequiredInput:

// TODO: We need to also get the `files` argument, so we can check for
// uploaded files. without it, this will return false negatives so we
// can't actually validate required input at the moment.

I'm trying to work out something that will do at least half a job, but just wanted to check my understanding of the way the data is stored. By my understanding...

The filename of the file is stored in the database. This can be coupled with the root of the app and the path option in the storage object to get an absolute path to the file.

For a required field, we can therefore:

  1. Check that value is not undefined, null or ''
  2. Check that value.filename is not undefined, null or ''
  3. If value is a string, then it is either an upload or delete. If upload, then pass validation as we're uploading a file. If delete, then fail the validation as it would leave the field empty.
  4. Check that the file exists

To my mind this should cover all eventualities and result in accurate validation.

Am I missing anything obvious here? I'd rather not spend too much time digging only to learn lessons you've already learned.

any update please? I have problems with Types.File preview format too :(

+1 this is still an issue and is terribly frustrating

+1 Any update on this?

@GreenMonkeySaveEarth I resorted to writing custom logic on my API endpoints which takes the files and saves them to the public folder.

When the upload finishes and I create my model, I pass the public URL of the pictures to two fields with 'Types.Url' field type, which at least renders as a clickable link in the Admin UI:

Admin UI Rendering

Let me know if you want to have a look at the API code.

Was this page helpful?
0 / 5 - 0 ratings