Page 1 of 1
CGBetterForms $forms object in udt
Posted: Sat Oct 24, 2020 3:29 pm
by rooon
Hi all,
I am using the Smarty validation template in CGBetterForms.
For this template I'm using an UDT with object $forms as param.
With a print_r I can see the $forms is in my UDT.
But how to get the field names and field values from this object?
There are (deep in the $form) more than 1 arrays like this, I cannot figure out how to get them.
Code: Select all
[smarty] => Smarty_CMS Object (
[params] => Array ( ..... )
)
All ideas are welcome.
Thanks, Ronald
Re: CGBetterForms $forms object in udt
Posted: Sat Oct 24, 2020 6:19 pm
by velden
If your $forms parameter contains a FormResponse object you can use something like:
$forms->get_field_value('<field name>');
$forms->list_fields(); (untested)
[SOLVED] use CGBetterForms response in UDT
Posted: Sat Oct 24, 2020 10:57 pm
by rooon
Thanks velden. A little bit testing with your suggestion did the job.
The result code is
CGBetterForms : Validate via Smarty Template
Code: Select all
{strip}{$fields = $response->list_fields()}
{$fld = ""}{$val = ""}{$sep = "]!["}
{foreach $fields as $field}
{$fld = $fld|cat:$field|cat:$sep}
{$val = $val|cat:$response->get_field_value("{$field}")|cat:$sep}
{/foreach}
{$fld = $fld|substr:0:-3}
{$val = $val|substr:0:-3}
{udt_betterforms_validation fields=$fld values=$val}{/strip}
UDT with name : udt_betterforms_validation
Code: Select all
$fields = get_parameter_value($params,'fields');
$values = get_parameter_value($params,'values');
$f = explode("]![", $fields);
$v = explode("]![", $values);
$fld = array_combine($f, $v);
// test
echo "<pre>"; print_r ($fld); echo "</pre>";
Re: CGBetterForms $forms object in udt
Posted: Sun Oct 25, 2020 7:48 am
by velden
I would pass the response object to the udt and move all logic to it too.
That's how I did it anyway.
[SOLVED] CGBetterForms $forms object in udt
Posted: Sun Oct 25, 2020 1:24 pm
by rooon
velden wrote: ↑Sun Oct 25, 2020 7:48 am
I would pass the response object to the udt and move all logic to it too.
That's how I did it anyway.
I tried that at first but I received a message "Warning: trim() expects parameter 1 to be string, object given". Today I did found out there is a difference in using the get_parameter_value(...) , then passing an object is
not allowed
Code: Select all
$response = get_parameter_value($params,'response');
and using the $params[...] , then passing an object is allowed.
By using the $params['param_name'] all is much easier and cleaner, as you said
CGBetterForms : Validate via Smarty Template
Code: Select all
{udt_betterforms_validation response=$response}
udt name : udt_betterforms_validation
Code: Select all
$response = $params['response'];
$fields = $response->list_fields();
$arr = array();
foreach ($fields as $field) {
$arr[$field] = $response->get_field_value($field);
}
echo "<pre>"; print_r($arr); echo "</pre>";