CGBetterForms $forms object in udt Topic is solved

Have a question or a suggestion about a 3rd party addon module or plugin?
Let us know here.
Post Reply
rooon
Forum Members
Forum Members
Posts: 93
Joined: Mon Dec 05, 2011 4:40 pm
Location: Voorburg, Nederland

CGBetterForms $forms object in udt

Post 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
User avatar
velden
Dev Team Member
Dev Team Member
Posts: 3304
Joined: Mon Nov 28, 2011 9:29 am
Location: The Netherlands

Re: CGBetterForms $forms object in udt

Post 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)
rooon
Forum Members
Forum Members
Posts: 93
Joined: Mon Dec 05, 2011 4:40 pm
Location: Voorburg, Nederland

[SOLVED] use CGBetterForms response in UDT

Post 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>";
User avatar
velden
Dev Team Member
Dev Team Member
Posts: 3304
Joined: Mon Nov 28, 2011 9:29 am
Location: The Netherlands

Re: CGBetterForms $forms object in udt

Post 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.
rooon
Forum Members
Forum Members
Posts: 93
Joined: Mon Dec 05, 2011 4:40 pm
Location: Voorburg, Nederland

[SOLVED] CGBetterForms $forms object in udt

Post 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.

Code: Select all

$response = $params['response'];
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>";
Post Reply

Return to “Modules/Add-Ons”