tpr Posted December 14, 2015 Posted December 14, 2015 I'm thinking about creating a new module to store form submissions quickly using serialized arrays. The idea is not to create separate fields for each submitted input but only one field. On the backend, the values would displayed in a human readable format, and the field itself won't be editable. Is there any existing solution for this? And, in general, any thoughts on this idea? 1
LostKobrakai Posted December 14, 2015 Posted December 14, 2015 That's essentially what FormBuilder and the pro fields Multiplier and Textareas are doing. Just the serialization method does vary.
tpr Posted December 14, 2015 Author Posted December 14, 2015 Unfortunately I don't have access to them. Even if they are doing the same, they display the submitted values on the backend like real fields, aren't they? My idea is to display values as a list (key-value), and make them read-only. I'm using a 3rd party form solution and this would simplify saving submissions. I was creating fields on the fly so far but that's very error-prone, plus I feel it's too much for the purpose. What I would need is to store submissions as simply as possible, and make submissions readable in the backend too.
LostKobrakai Posted December 14, 2015 Posted December 14, 2015 Maybe have a look into the FieldtypeYaml module. It's saving data in a textual representation and has multiple optional ways to access those data. As aside: There's no inherently "real" field when we're talking about UI. It's just a matter of having an inputfield and some save method to make a UI field real. E.g. all the module settings' forms have their values stored as json blobs and the forms still look the same as others. If you only need a readonly display it's just less work (no save method) and different markup.
tpr Posted December 14, 2015 Author Posted December 14, 2015 Thanks, I'll check it. I guess I'll create such a module (or at least try) because I would use it my projects.
kongondo Posted December 14, 2015 Posted December 14, 2015 (edited) Yes, MenuBuilder too ...(but uses JSON) IIRC, somebody already did this? i.e. there's a fieldtype around here somewhere... Edit 1: Yeah, what @LostKobrakai said... Edit 2: @tpr: Moving this topic to plugin development. The current board (ProcessWire Support Forums → Community Support → Modules/Plugins) is for supporting released modules Edited December 17, 2015 by kongondo
tpr Posted December 14, 2015 Author Posted December 14, 2015 @kongondo Thanks, I guess I focused only on the Modules/Plugins part of the title and ignored the rest
tpr Posted December 17, 2015 Author Posted December 17, 2015 Gave this thing a go and it seems I will take a simpler approach: using a textarea and a InputfieldTextarea::render hook to display the array in a HTML list format in the admin. Works like a charm, only the admin display should be tweaked a bit to make it nicer. I've also changed the storage type from serialized array to json. Of course field value has to be json_encoded before save but that's a minor issue.
LostKobrakai Posted December 17, 2015 Posted December 17, 2015 I've also changed the storage type from serialized array to json. Of course field value has to be json_encoded before save but that's a minor issue. You could hook FieldtypeTextarea::sanitizeValue for that. If it's not a string with [ or { in the beginning, then encode it. 1
tpr Posted December 17, 2015 Author Posted December 17, 2015 Is sanitizeValue hookable? It doesn't seem to work. Note that I'm using hooks in ready.php (but have tried init.php too).
tpr Posted December 17, 2015 Author Posted December 17, 2015 After some trial and error, "Pages::saveReady" did the trick to ensure input value is json encoded. Now getting those dirty form submissions to the admin is a breeze.
tpr Posted December 17, 2015 Author Posted December 17, 2015 Just a note: overwriting the render value with custom html output (getting rid of the input field) is not a good idea. The field will have no value in the admin page (only the custom html, no form input) and hitting Save will null out the original value. This could be possibly eliminated keeping the original input for the field and hiding them, and appending the custom html. As for now, I made the field Locked so the field will use the renderValue to display its contents. Doing so hitting Save will not change the original value. Here's how the current admin page looks like. The "Submission details" is one textarea with json contents, using a renderValue hook to make nicer: 1
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now