Image rendering and linking fields in form
Hi!
How to render image (field of some resource) represented as []byte in go struct and varbinary in DB (I use MySQL) ? What type of field in Meta should I use (media_library ?) ? Should I use Valuer ?
How can I link 2 fields in form (e.g. new resource form) so when I select some value in the first field available values in the second field are changing correspodingly? E.g. I have 2 fields, country and city. Firstly I select country (e.g. Russia). After I have selected Russia avaiable values in the field city must be only russian cities and not all cities available in DB table cities.
@ftomza
- You can check out how images works in qor-example
- You need to use
Valuerin your resource, check out docs or qor-example/config/admin.go
Hi @alexmironof!
Thank you for answer. I have already checked both qor-example and docs and still it doesnt' clear enough for me. I also made some experiments.
- I can generate image (QR code) as []byte and then store it in DB as varbinary. Should I convert it into MediaBox, but how ?
- I've tried to use Valuer but Valuer is called once on form rendering to generate some options (e.g. not all cities but only some of them). I need other thing: I need dynamically change list of options in the second field (cities) each time user select some new option in the first field (countries).
@ftomza Oh I see, Qor Media uses OSS to store files on file system or s3, so you shoud manually handle rendering images from database.. Maybe you should try to convert bytes to base64? It is most simple solution
@alexmironof thank for your hint with image rendering: I don't use Qor Media and OSS but I wrote own template to manually handle QR rendering: admin.go:
profile.Meta(&admin.Meta{Name: "QR", Type: "readonly_img",
Valuer: func(val interface{}, context *qor.Context) (interface{}) {
var profile, _ = val.(*models.Profile)
return base64.StdEncoding.EncodeToString(profile.QR)
}})
own template app/views/qor/metas/form/readonly_img.tmpl containing:
<div class="qor-field__edit">
<img style='display:block; width:200px;height:200px;'
src='data:image/png;base64,{{.Value}}' />
</div>
It works!
@alexmironof I would really appreciate if you can give advice regarding 2nd point about linking 2 fields in form.