One of the two required fields to be filled in accounts module

Hi,

I have a doubt regarding fields in the accounts module. When user enters all his details in the accounts form, either an email address or a phone number would be required always to be filled in. Even both the details will do. But he has to enter one field from both of them always.

I am trying to change the account.js and account.php file but in vain. Could someone please guide me on the same.

To change the required fields in a module you can go to Admin/Studio//Fields then click on the field you wish to make required, then select the ‘Required Field’ checkbox.

Thank you for the above reply…

I don’t want to make both the fields as required. Its like if the user enters any one of the two fields(email or phone) an account is created.

(E.g

  1. email address - abc@example.com and phone_number - blank - account created
  2. email address - blank and phone number -454554545 -account created
  3. both email address and phone entered -account created
  4. both empty - account not created(shows a popup as to enter email address or phone number)

Please reply

Read this article and you should get a hint on how to proceed.
https://developer.sugarcrm.com/2013/04/04/sugarcrm-create-a-custom-javascript-validation-before-saving-in-edit-view/

and also here:
https://phpbugs.wordpress.com/2010/01/22/sugarcrm-adding-javascript-validation-on-form-submit/

and here:
https://gunnicom.wordpress.com/2015/09/21/suitecrm-sugarcrm-6-5-add-custom-javascript-field-validation/

and here:
http://stackoverflow.com/questions/19184018/field-validations-in-sugarcrm

I wouldn’t modify Account.php nor Account.js if I were you.

Alternatively to the solution described in the article you could also use a logic hook I believe.

1 Like

Thank You alot…

I tried doing the below changes in the files mentioned…

created a file named “view.edit.php” in the custom folder

<? php public function preDisplay() { parent::preDisplay(); function customJavascriptValidation() { var value1 = document.getElementById('email1').value; var value2 = document.getElementById('phone_office').value; if( value1 != "" || value2 != "" ) { return true; } alert("You must enter a value"); return false; return check_form(“Edit View”); // This will call the default function that sugar use } } ?>

and also made changes in the “editviewdefs.php” file as mentioned …but I am getting a blank page.
after repair and rebuild on the accounts page…

code:

‘form’ =>
array (
‘buttons’ =>
array (
0 => array (
‘customCode’ => ‘’,
),
),
),

can you please help me out in this as I am new to suitecrm…
Thank you once again… :slight_smile:

I am not sure because I have never done it myself.

However there are two things I would investigate in your code:

  1. return check_form(“Edit View”) <- I would remove the spave between Edit and View
  2. return custom_function(‘EditView’); <- possibly you should use the same name of your custom function instead (customJavascriptValidation)

I repeat, I never tried it. I will test it.
In the mean time if you manage please post back.

Thankyou

I’m trying the above changes suggested by you. If I manage to get the desired output , I’ll post it here.

Thanks once again.

One more thing.

I am not sure, but looking carefully at your code you seem to be suing some backticks (or a different type of quotes) instead of standard quotes or double quotes. I would replace them with normal quotes or double quotes since backticks have serve a special purpose in php.

In particular I refer to: return check_form(“Edit View”)

I would remove the space and change the backticks to single or double quotes:
return check_form(‘EditView’)

Thank You for the reply…

Tried all the above mentioned modifications in the code…but no luck…
So kindly help me out in this…

Thank you once again…

  1. Which files did you edit?

  2. Can you post the portions of code you added?

  3. Can you post images of the outcome?

  4. Can you post errors appearing in your logs? (SuiteCRM, Apache/php, Browser Console)

I edited two files :
1)view.edit.php in custom/modules/accounts/views
2)editviewdefs.php in custom/modules/accounts/metadata

Portions of code

  1. view.edit.php

public function preDisplay() {
parent::preDisplay();

function EditView()

{

if (EditView.Accounts0emailAddress0.value == '' && EditView.phone_office.value == '') {
            alert('You have to enter at least one phone number.');
            return false;
        }
		
		else{
			return true;
		}

return check_form(“EditView”); // This will call the default function that sugar use
}
}

2)editviewdefs.php - it is added before widths

array (
‘buttons’ =>
array (
0 => array (
‘customCode’ => ‘<input type=“submit” name=“save” id=“save” onClick=“this.form.return_action.value=“DetailView”; this.form.action.value=“Save”; return custom_function(“EditView”);” value=“Save”>’,
),
),
),

Images of the outcome: It directly saves the accounts created and does not create an alert for not filling any one of the two fields…

Errors in the Suitecrm logs:
Nothing appears in the log as it saves the account.

Have you run a Quick Repair and Rebuild and have you cleared your browser cache after your modifications?

Can you verify that, when you are inside the EditView, the submit button field corresponds to your custom code?

I am not sure, but in your submit button you call a function called custom_function, while in the view.edit.php you have only defined a function called EditView.

Maybe I am wrong but I think that you should use the same name.

This is also not working…

:frowning:

Can anyone help?
I want to know evn if such thing is possible in suitecrm?

Please reply…

I am still exploring it. But I need a few days.

I noticed that one of the articles probably missed some information like the code has to be entered within a class that extends the edit view class and, before doing so it should require once the parent class.

I will try to test it and will let you know.

The following solution worked for me:

  1. Check if there is a file called custom/modules/Accounts/views/view.edit.php
  2. If the file doesn’t exist create it and paste the following code:

<?php
if (!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
require_once('modules/Accounts/views/view.edit.php');

class CustomAccountsViewEdit extends AccountsViewEdit
{
     public function __construct(){
        parent::__construct();
     }

    function display()
    {
        //call parent display method
        parent::display();


// This is javascript code that I save in a variable:
		$js = <<<EOT
<script type="text/javascript" language="JavaScript">
function customJavascriptValidation(thisview)
 {
   var returnvalue;
   var value1 = document.getElementById('Accounts0emailAddress0').value;
   var value2 = document.getElementById('phone_office').value;
   if( value1 != "" || value2 != "" )
    {
      return check_form('EditView'); // This will call the default function that sugar use
    }
   else
    {
      alert("You must enter a value for email or phone");
      return  false;
    }
}
</script>
EOT;

// now I output the javascript
        echo $js;

    }
}
?>

If the file already exists check if there is a function called display() and, after

    parent::display();

paste the following code:


// This is javascript code that I save in a variable:
		$js = <<<EOT
<script type="text/javascript" language="JavaScript">
function customJavascriptValidation(thisview)
 {
   var returnvalue;
   var value1 = document.getElementById('Accounts0emailAddress0').value;
   var value2 = document.getElementById('phone_office').value;
   if( value1 != "" || value2 != "" )
    {
      return check_form('EditView'); // This will call the default function that sugar use
    }
   else
    {
      alert("You must enter a value for email or phone");
      return  false;
    }
}
</script>
EOT;

// now I output the javascript
        echo $js;

If you cant find the display() function then paste inside the class the following code:

    function display()
    {
        //call parent display method
        parent::display();


// This is javascript code that I save in a variable:
		$js = <<<EOT
<script type="text/javascript" language="JavaScript">
function customJavascriptValidation(thisview)
 {
   var returnvalue;
   var value1 = document.getElementById('Accounts0emailAddress0').value;
   var value2 = document.getElementById('phone_office').value;
   if( value1 != "" || value2 != "" )
    {
      return check_form('EditView'); // This will call the default function that sugar use
    }
   else
    {
      alert("You must enter a value for email or phone");
      return  false;
    }
}
</script>
EOT;

// now I output the javascript
        echo $js;

    }
  1. check if there is a file called custom/modules/Accounts/metadata/editviewdefs.php
    If it doesnt exist, create it and paste the following inside:

<?php
include('modules/Accounts/metadata/editviewdefs.php');
$viewdefs ['Accounts']['EditView']['templateMeta']['form']['buttons'][0] =  array (
            'customCode' => "<input type='submit' name='save' id='save' onClick=\"this.form.return_action.value='DetailView'; return customJavascriptValidation('EditView');\" value='Save'>",
          );
?>

If it exists add at the end the following code:


$viewdefs ['Accounts']['EditView']['templateMeta']['form']['buttons'][0] =  array (
            'customCode' => "<input type='submit' name='save' id='save' onClick=\"this.form.return_action.value='DetailView'; return customJavascriptValidation('EditView');\" value='Save'>",
          );

  1. Go to Admin->Repair->Quick Repair and rebuild and then test

And give some feedback

Additionnaly you can add some labels so that the language is not hard coded.

1 Like

Yaaay… Thank You very much…
Its working… :slight_smile:

1 Like

Hello,

I tried to rewrite this to the Opportunities module and it did not work.
Should I do something different to make it work?

Versão 7.8.2

Sugar Versão 6.5.24 (Compilaçāo 509)

The code I provided was specific for the Accounts module. With little changes you can adapt it to Opportunities.

Please share what you have done and I will try to understand what needs to be corrected.

…l/custom/modules/Opportunities/views//view.edit.php



<?php
if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');

class OpportunitiesViewEdit extends ViewEdit
{
 	public function __construct()
 	{
 		parent::__construct();
 		$this->useForSubpanel = true;
 		$this->useModuleQuickCreateTemplate = true;
 	}

	function display()  // CUSTOMIZACAO PARA DESATIVAR CAMPO CONDICIONALMENTE - PAYÁ
	{  

        parent::display(); 

$js = <<<EOT


<script type="text/javascript" language="JavaScript">
function customJavascriptValidation(thisview)
 {
   var returnvalue;
   var value1 = document.getElementById('sales_stage').value;
   var value2 = document.getElementById('h2l_motivo_perder_c').value;
   if( value1 != "PERDIDO")
    {
      return check_form('EditView'); // This will call the default function that sugar use
    }
   else
    {
      alert("nao validado - teste - PAYA");
      return  false;
    }
}
</script>
EOT;

// now I output the javascript
        echo $js;



	} 
}
?>

…/custom/modules/Opportunities/metadata/editviewdefs.php



<?php
$viewdefs ['Opportunities'] = 
array (

...

);


$viewdefs ['Opportunities']['EditView']['templateMeta']['form']['buttons'][0] =  
array (
  'customCode' => "<input type='submit' name='save' id='save' onClick=\"this.form.return_action.value='DetailView'; return customJavascriptValidation ('EditView');\" value='Save'>",
);

?>

I have a blank page as a return!