I’m having trouble getting data from my simple custom function in which I want to input form data. the
$form = $event[‘form’];
is getting invalid json
I’m having trouble getting data from my simple custom function in which I want to input form data. the
$form = $event[‘form’];
is getting invalid json
i managed to get the data i want using this
$data = json_decode( json_encode($form["data"]), true);
foreach($data as $val){
if(!is_array($val)){
$x=0;
$myfields[$x]=$val;
}
}
@yehudac, Every time when code looks clunky, a little voice in the back of your head should say, “Hm I can’t imagine this is really meant to be”
In the case of Grav, one could then lookup the API of Data in the docs and find for example:
public toArray() : array
Convert object into an array.
or
public get( string $name , mixed $default=null , string $separator=null ) : mixed Value.
Get value by using dot notation for nested arrays/objects.
Or use code IntelliSense which will reveal:
To summarise the options:
public function onFormProcessed(Event $event) {
/** @var Form */
$form = $event['form'];
// Using Data::toArray()
$data = $form->data->toArray();
// Using Data::get($name, $default = null, $separator = null)
$message = $form->data->get('message', 'some default value');
$nestedFieldname = $form->data->get('a.nested.fieldname', 'some default value');
// Using associative array access
$name = $form->data['name'];
}