How make à validator, setFormatter, getFormatter for json member ?
How make à validator, setFormatter, getFormatter for json member ?

Hello,
i have à table width à json Field.
struct of table is : id_datahead,id_context_model,id_datahead_origin, json
I use json because the data structure is moving according to a model.
I linked the different elements to the datatable and the editor
exemple of data
id_datahead=1
id_context_model=2
id_datahead_origin=1
json={"prop_6":"1","prop_4":"2","prop_1":"","prop_5":"dsd","prop_3":"dsd","prop_7":"7"}
Json for columns and fields :
{"columns":[{"data":null,"defaultContent":"","className":"select-checkbox"},{"data":"json.prop_1","title":"1Name"},{"data":"json.prop_3","title":"3Adresse1"},{"data":"json.prop_4","title":"4Adresse2"},{"data":"json.prop_5","title":"5test1"},{"data":"json.prop_6","title":"6x1","type":"checkbox","separator":"|","ipOpts":{"":1}},{"data":"json.prop_7","title":"7x2"}],
"fields":[{"name":"id_datahead","type":"hidden"},{"name":"id_datahead_origin","type":"hidden"},{"name":"id_context_model","type":"hidden"},{"name":"json.prop_1"},{"name":"json.prop_3"},{"name":"json.prop_4"},{"name":"json.prop_5"},{"name":"json.prop_6","type":"checkbox","separator":"|","ipOpts":{"":1}},{"name":"json.prop_7"}]}
and the php
```
<?php
include( "../editor/lib/DataTables.php" );
use
DataTables\Editor,
DataTables\Editor\Field,
DataTables\Editor\Format,
DataTables\Editor\Mjoin,
DataTables\Editor\Options,
DataTables\Editor\Upload,
DataTables\Editor\Validate,
DataTables\Editor\ValidateOptions;
include '../entete.php';
Editor::inst( $db, 'datahead', 'id_datahead' )
->fields(Field::inst( 'datahead.id_datahead')->set(false)
,Field::inst( 'datahead.id_datahead_origin')->set(false)
,Field::inst( 'datahead.id_context_Model')->set(false)
,Field::inst( 'json' )
->getFormatter(function($val,$data){
return json_decode($val);
})
->setFormatter(function($val,$data){
return json_encode($data["json"]);
}))
->debug( true )
->process( $_POST )
->json();
It works perfectly,
but I have validation checks and formatters to do on each element of the json and I don't know how to do it in the php.
thank for your help.
Edited by Colin - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.
This question has an accepted answers - jump to answer
Answers
Hi,
In PHP you can use
json_decode
andjson_last_error
to validate JSON. Combine that with a custom validator and I think you should have what you need - e.g.:Allan
Thank you Allan, but it is not possible to have a simple type writing ?
'''
Field :: inst ('json.prop_1')
-> validator (Validate :: minNum (16));
-> setFormatter (....
-> getFormatter (...
''''
I see otherwise how to do it manually in a global validator, but it is shame to do without its functionality.
Ah I see - sorry, no. It isn't possible to validate individual fields inside a JSON string like that. You could use the validators directly inside a custom validator, but you'd be as well just doing something like:
Allan
Thanks Allan. That is what I thought. Could this possibility of having validators for json fields be added in a future version?
It's not something that we've thought of before to be honest - I think this is the first request we've had for it. For myself, I've tended to use JSON schema validation with a custom validator if doing validation against JSON.
Certainly open to adding new features, but I think we'd need a bit more feedback from others to look at implementing this.
Regards,
Allan
Thanks Allan.