Hide Elements using PHP

I would like to hide/show form/details elements depending on another field.

I found this piece of code on another thread. I tried to use this, however, it says I can't access protected property "access". I changed it to Public from Element.php and everything works fine. I would like to find out if there's a better way to achieve this. Thanks in advance


Hi,
I have found a solution to hide elements, perheps somebody helps:

Create a Form php code on action "onCanEditGroup"

Code:
$elem = $formModel->getElement('table___element');
$elem->access->view = false; //to hide the element on details view
$elem->access->use = false;  //to hide the element on edit view


Lars
 
This is my code on the OnCanEditGroup


Code:
$e = $formModel->getElementData('civicrm_contact___contact_sub_type');
switch($e){
    case "Care_Home":
    $elem[0] = $formModel->getElement('staff_with_driving_license_240');
    $elem[1] = $formModel->getElement('civicrm_value_staff_r_25___staff_ratio_daytime__219');
   
    //var_dump($elem);
    foreach ($elem as $i) {
       $i->access->view = false; 
    }
   
   
    //var_dump
         //$elem->access->view = false;
        //$elema->access->view = false;
//$elem->access->use = false;
    break;

}
 
Is there some reason you can't use element javascript to do this? That is one of the functions it was designed to do.
 
Is there some reason you can't use element javascript to do this? That is one of the functions it was designed to do.


Thanks for replying arcartier. The only reason is that when i use javascript it takes a little while to hide the element in the Front-End. Using PHP is perfect as it hides the elements before the form is rendered. That's the reason i prefer using PHP
 
Ah, I see. Yes this is a bit of a pain. You might try doing the hiding in javascript in the document.ready function, then you won't have to modify the element code.
 
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top