Unset data

mirceat

Member
Hello,

Please tell me which is the correct syntax to unset the data in a form (also for repeat groups). I've tried the following syntax's onBeforeStore/onBeforeProcess, can't make it work, the form still record the data.

$foo is getting the correct value as string, but the Unset trigger doesn't run. I need to unset data every time an element (yes/no, dbjoin, cdd etc) have the default value set to zero.

PHP:
$foo = $formModel->getElementData('mytable___myelement', true);
if ($foo[0] == '0') {
unset($_REQUEST['join'][2183]);
}

PHP:
$foo = $formModel->getElementData('mytable___myelement', true);
if ($foo[0] == '0') {
unset($this->_formData['join'][2183]);
}

PHP:
$foo = $formModel->getElementData('mytable___myelement', true);
if ($foo[0] == '0') {
unset($formModel->_formData['join'][2183]);
}

Thank you
 
I didn't try.
But following http://fabrikar.com/forums/index.php?wiki/php-form-plugin/#accessing-form-data

Maybe
Fabrik 3.1:
$formModel->data//onload: use full element names 'table___element' e.g. $formModel->data['table___element']='abc';
...
The way Fabrik handles repeat data changed between 3.0 and 3.2/1. In 3.0, repeat group data was stored in a 'join' array in formData, keyed by the groups ID. So $formModel->_formData['join'][123]['repeat_table___foo'] would be an array containing each repeated value. In 3.1/2, this got simplified, with the ['join'] array being remoed, so $formModel->formData['repeat_table___foo'] is the array of repeated values.
To quickly see the structure of your form's data set this as your code:

Fabrik 3.1:
echo "<pre>";print_r($formModel->data);exit;
or
echo "<pre>";print_r($formModel->formData);exit;
or
echo "<pre>";print_r($data);exit;
 
Ok, that will solve the way i get the value of the element (i will test for a repeat group), but how do i unset the data? The problem is that after $foo is getting the correct value , the unset is still not triggered. Using $_REQUEST is still ok or what else i should use?
 
Have a look with the different print_r() which data structure you have.

Then something like
unset($formModel->formData['repeat_table___foo']);
 
already tried, the data is still submitted :(

When i should process the script, onBeforeStore or onBeforeProcess?
 
Last edited:
...can't make it work :( Hugh, can you please give me the full syntax to unset a join with and without a repeat group?
this code doesn't work for me onBeforeStore for a repeat group.

PHP:
$foo = $formModel->formData['contracte_abonamente___optiuni'];

if ($foo == '0') {
unset($formModel->formData['contracte_abonamente_691_repeat___categorie_optiune']);
}

Thank you
 
Can you be more specific about what you are trying to do? Are you trying to remove an entire repeated group, or just unset specific element data within a repeat?

Are you trying to remove all repeats? Or a specific repeat?

-- hugh
 
I need to unset/remove any group (with or without repeat option enabled) that are not visible/available to user.

My form consists of a series of groups that are shows/hidden depending by user selections. I need to stop recording any values for the groups that are not used on user interaction. Now i have a lot of database tables full of empty rows and once per month i run a cron task to clean the empty data.
 
Ah, OK.

That won't be entirely trivial, because you'd have to remove all the element data individually, and modify the hidden variable which contains the count for each group.

I think a better approach might be to use the canEditGroup hook in a form plugin.

Code:
// make sure this is form submission, as this hook is also called during rendering
if (JFactory::getDocument()->input->get('task', '') == 'form.process') {
   // case for which group ID
   switch ($groupModel->getId()) {
      case 123:
         // return false if some condition
         if ($formModel->getElementData('mytable___myelement', true, '') === '0') {
            return false;
         }
      case 456:
         // return false for some other condition
         if ($formModel->getElementData('mytable___myotherelement', true, '') === '0') {
            return false;
         }
      default:
         return true;
   }
   return true;
}
return true;

That should prevent the group(s) from being saved.

-- hugh
 
Using your code, I get the message " Fatal error: Call to a member function get() on null in C:\inetpub\mysite\plugins\fabrik_form\php\php.php(485) : eval()'d code on line 2 ".

Using getApplication instead of getDocument solve the error message but the data is still recorded.

J 3.7.5, Fabrik Github (updated last week)
 
using var_dump($formModel->formData);exit; and place it before if (if (JFactory::getDocument()->input->get('task', '') == 'form.process') {) return NULL .

Place it after return "Fatal error: Call to a member function get() on null in C:\inetpub\partenerivodafone.ro\plugins\fabrik_form\php\php.php(485) : eval()'d code on line 3"

the php plugin is running "While groups are rendered on the form (onCanEditGroup)"

this is the complete code:

PHP:
if (JFactory::getDocument()->input->get('task', '') == 'form.process') {
   //get group ID
   switch ($groupModel->getId()) {
      case 640:
         //terminale
         if ($formModel->getElementData('contracte_telefoane___terminal', true, '') === '0') {
            return false;
         }
      case 680:
         //optiuni suplimentare
         if ($formModel->getElementData('contracte_campuri_680_repeat___optiune_suplimentara', true, '') === '0') {
            return false;
         }
      case 719:
         //accesorii flota
         if ($formModel->getElementData('contracte_campuri_719_repeat___accesorii_echipament', true, '') === '0') {
            return false;
         }
      default:
         return true;
 }
   return true;
}
return true;

btw, dumping $formModel->getElementData('contracte_campuri_719_repeat___accesorii_echipament', true, '') will return Array.

I can get a value if i set the group position to 0 like: $formModel->getElementData('contracte_campuri_719_repeat___accesorii_echipament', true, '', 0) . All the groups from this form are set to repeat.
 
Last edited:
Yes, getApplication solve the error message but i had to change the task to

if (JFactory::getApplication()->input->get('task', '') == 'process') {

and the element to

Update: to avoid situation when the first occurrence of this element has been removed, it's better to use foreach?

PHP:
 $foos = $formModel->getElementData('mytable___myelement', true);
        foreach ($foos as $foo) {
        if (empty($foo)) {
            return false;
         }
}

to make it work.

Thank you for help.
 
Last edited:
So if any of contracte_campuri_680_repeat___optiune_suplimentara are set to 0, you need to remove all of group 680?

-- hugh
 
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top