-->

Adding Custom Field to Edit View - SugarCRM

2019-05-23 20:59发布

问题:

I am trying to modify the behavior of a custom field (a checkbox) in the EditView of a SugarCRM (SugarPro 6.1.2) instance. Specifically, I would like a div to be exposed whenever someone clicks on the checkbox. The field appears in the Edit view as expected and I have added some javascript that executes onclick - this works fine too. The problem is that, when the record is saved, the value of the checkbox is not saved. Here are specifics on how I went about adding the field and associating the javascript:

  1. I created the field as a checkbox in Studio. It's called wire_payment_c
  2. I added the field to the DetailView and EditView layouts.
  3. I ran Quick Repair & Rebuild
  4. I edited ./custom/modules/MODULE/metadata/editviewdefs.php as follows:

      1 => 
    array (
      0 => '',
      1 => 
      array (
        'name' => 'wire_payment_c',
        'label' => 'LBL_WIRE_PAYMENT',
     'customCode' => '<input type="hidden" value="0" name="wire_payment_c">    
     <input id="wire_payment_c" type="checkbox" tabindex="107" title="" value="{$fields.wire_payment_c.value}" name="wire_payment_c" onclick="showMe(\'wire_message\', this)"> 
     <div id="wire_message" style="display:none">Please obtain wire payment information.</div>' ,
      ),
    ),
    

And just below the panels definition, I added the following also:

'includes'=> array(
     array('file'=>'custom/modules/MODULE/wire_pmt.js' ),
 ),
  1. I created the javascript containing showMe() at ./custom/modules/MODULE/wire_pmt.js.
  2. I ran a Quick Repair & Rebuild again

I've been able to verify that the checkbox value is saved and retained in the database exactly as expected if I take out the customCode line in editviewdefs.php. Further, I can verify that removing the onclick directive and simply leaving the input tag in the value assigned to customCode is enough to reproduce the problem. Upshot seems to be that this issue has nothing to do with the javascript - it's something about how I am re-rendering the input tag. But the code I have in place for the input tag looks exactly like what I see when I comment out customCode altogether and view the form through FireBug.

Is there something else that one has to do in a situation like this to get Sugar to save the value of custom fields to the database and then display the saved value to the Detail and Edit Views on load? Happy to read docs on this process, but have been unsuccessful in finding any that seem to apply to this specific sort of task.

Thank you very much!

回答1:

In investigating your issue, I found that the problem is how you are determining if the checkbox is checked. This is affecting the way the form is being submitted.

Currently you are using: value="{$fields.wire_payment_c.value}"

The attribute value should by default be 1. The determining factor in whether the checkbox is true or false is specified with the checked attribute.

Your customCode attribute should look more like this:

'customCode' => '<input type="hidden" value="0" name="wire_payment_c">{if $fields.wire_payment_c.value == "1"}{assign var="isChecked" value="CHECKED"}{else}{assign var="isChecked" value=""}{/if}<input type="checkbox" id="wire_payment_c" name="wire_payment_c" value="1" title="" tabindex="107" onclick="showMe(\'wire_message\', this)" {$isChecked}><div id="wire_message" style="display:none">Please obtain wire payment information.</div>',

To see how we handle this in the core product, you can take a look at include/SugarFields/Fields/Bool/EditView.tpl.

Kind regards, Jerry Clark

Developer Support Engineer



标签: sugarcrm