Welcome to the nuBuilder Forums!

Register and log in to access exclusive forums and content available only to registered users.

Default values

Questions related to using nuBuilder Forte.
marcvander
Posts: 101
Joined: Mon Mar 26, 2018 5:57 pm

Default values

Unread post by marcvander »

Hey,

I'm trying to set a default value for an object on an edit form. I found this topic: https://forums.nubuilder.cloud/viewtopic. ... lue#p17268

The code suggested is this one:

Code: Select all

if (nuFormType() == 'edit') {

  $('#your_field').val("hello").change();

  nuHasNotBeenEdited();
});
But I would like to set a default value based on who is the current logged in user adding a new entry. In my database, I have a table called employee. In this table, there is a foreign key employee_user pointing to zzzzsys_user_id of table zzzzsys_user. So I can assign an employee to a user. In my employee table I have two fields employee_fr (tinyint) and employee_ch (tinyint). Both can either be 0 or 1. Based on if the value is 0 or 1 for the current logged in user, I would like to enter a default value on an edit form. How can I do that ? (in nuBuilder v3, I could do it easily because there was an SQL entry for default values on any object, so I could query the right info. I guess for v4, I have to have a php procedure running in the back ?)
Config:
nuBuilder v4 on a dedicated LAMP server:
-Ubuntu 14.04.5 LTS
-Apache 2.4.7
-MySQL 14.14 Distrib 5.7.22
-PHP 5.5.9-1ubuntu4.23
toms
Posts: 785
Joined: Sun Oct 14, 2018 11:25 am

Re: Default values

Unread post by toms »

Hi,

Something like this? (untested)

In the Before Edit (PHP) Event:

Code: Select all

$s = "SELECT * FROM .... ";
$t = nuRunQuery($s);
$ch = db_fetch_object($t)->employee_ch;
$fr = db_fetch_object($t)->employee_fr;

$j = "

function employee_fr(){
   return '$fr';    
}

function employee_ch(){
   return '$ch';    
}

";

And then the JS in your form:

Code: Select all

if (nuFormType() == 'edit') {
  
  if (employee_ch() == "1") {
	$('#your_field').val("hello").change();
  }
  
  if (employee_fr) == "1") {
	$('#your_field').val("hello").change();
  }

  nuHasNotBeenEdited();
});
marcvander
Posts: 101
Joined: Mon Mar 26, 2018 5:57 pm

Re: Default values

Unread post by marcvander »

Toms, thanks for the reply. There was a typo here:

Code: Select all

  if (employee_fr) == "1") {
I changed it to

Code: Select all

  if (employee_fr() == "1") {
Here is the error I get in console when I go on the form:
Capture d’écran 2018-06-29 à 19.06.36.png
Maybe the problem comes from the fact that you declare the variable $j in the PHP in which you have the two JS functions, but you don't use this variable anywhere else. So the functions are unknown (it's just a guess, i'm not a pro in neither PHP nor JS).
You do not have the required permissions to view the files attached to this post.
Config:
nuBuilder v4 on a dedicated LAMP server:
-Ubuntu 14.04.5 LTS
-Apache 2.4.7
-MySQL 14.14 Distrib 5.7.22
-PHP 5.5.9-1ubuntu4.23
toms
Posts: 785
Joined: Sun Oct 14, 2018 11:25 am

Re: Default values

Unread post by toms »

There's a bracket too much:

Code: Select all

if (nuFormType() == 'edit') {
  
  if (employee_ch() == "1") {
   $('#your_field').val("hello").change();
  }
  
  if (employee_fr) == "1") {
   $('#your_field').val("hello").change();
  }

  nuHasNotBeenEdited();
};
admin
Site Admin
Posts: 2814
Joined: Mon Jun 15, 2009 2:23 am
Been thanked: 25 times

Re: Default values

Unread post by admin »

.
marcvander
Posts: 101
Joined: Mon Mar 26, 2018 5:57 pm

Re: Default values

Unread post by marcvander »

Toms, thanks. Indeed there was a bracket too much. Though now i have this error:
Capture d’écran 2018-07-01 à 12.47.17.png
To solve it I tried to add a

Code: Select all

nuJavascriptCallback($j);
at the end, but I still have the same error. My code now looks like this:

PHP Before Edit:

Code: Select all

$select = "SELECT * FROM employe WHERE employe_user=#USER_ID#";
$run = nuRunQuery($select);
$ch = db_fetch_object($run)->employe_ch;
$fr = db_fetch_object($run)->employe_fr;

$j = "

function employe_fr(){
   return '$fr';    
}

function employe_ch(){
   return '$ch';    
}

";

nuJavascriptCallback($j);
The JS on the form to put default values:

Code: Select all

if (nuIsNewRecord()) {
  
  if (employe_ch() == "1") {
   $('#contact_ch').val("1").change();
  }
  
  if (employe_fr() == "1") {
   $('#contact_fr').val("1").change();
  }

  nuHasNotBeenEdited();
};
You do not have the required permissions to view the files attached to this post.
Config:
nuBuilder v4 on a dedicated LAMP server:
-Ubuntu 14.04.5 LTS
-Apache 2.4.7
-MySQL 14.14 Distrib 5.7.22
-PHP 5.5.9-1ubuntu4.23
toms
Posts: 785
Joined: Sun Oct 14, 2018 11:25 am

Re: Default values

Unread post by toms »

#USER_ID# must be enclosed in single quotation marks

Code: Select all

$select = "SELECT * FROM employe WHERE employe_user= '#USER_ID#' ";
marcvander
Posts: 101
Joined: Mon Mar 26, 2018 5:57 pm

Re: Default values

Unread post by marcvander »

Toms, indeed, thanks. But I still have the same error: employe_ch is not defined. The system somehow cannot find the functions employe_ch and employe_fr that we created in the variable $j. Was it right to add the nuJavascriptCallback function at the end of the PHP ? Is there anything else that we have to add ?
Config:
nuBuilder v4 on a dedicated LAMP server:
-Ubuntu 14.04.5 LTS
-Apache 2.4.7
-MySQL 14.14 Distrib 5.7.22
-PHP 5.5.9-1ubuntu4.23
toms
Posts: 785
Joined: Sun Oct 14, 2018 11:25 am

Re: Default values

Unread post by toms »

marcvander wrote:Was it right to add the nuJavascriptCallback function at the end of the PHP ? Is there anything else that we have to add ?
nuJavascriptCallback() is used together with nuRunPHPHidden(). You need to use nuAddJavascript($j); And call db_fetch_object() just once.

Code: Select all

$run = nuRunQuery($select);

$select = "SELECT * FROM employe WHERE employe_user= '#USER_ID#' ";
$run = nuRunQuery($select);
$o       = db_fetch_object($run);
$ch = $o->employe_ch;
$ch = $o->employe_fr;

$j = "

function employe_fr(){
   return '$fr';    
}

function employe_ch(){
   return '$ch';    
}

";

nuAddJavascript($j);
marcvander
Posts: 101
Joined: Mon Mar 26, 2018 5:57 pm

Re: Default values

Unread post by marcvander »

Toms, thanks, nuAddJavascript($j) made it work :)

Now I have an issue to populate my form. I'm trying to populate two checkboxes:
Capture d’écran 2018-07-01 à 20.42.25.png
The JS code is:

Code: Select all

if (nuIsNewRecord()) {
  
  if (employe_ch() == 1) {
   $('#contact_ch').val(1).change();
  }
  
  if (employe_fr() == 1) {
   $('#contact_fr').val(1).change();
  }

  nuHasNotBeenEdited();
};
But the checkboxes do not populate. I used the console to test my JS code, here is the result:
Capture d’écran 2018-07-01 à 20.41.36.png
So it shows that the conditions work and should populate. So the issue is on this line I guess:

Code: Select all

$('#contact_ch').val(1).change();
I also tried:

Code: Select all

$('#contact_ch').val("1").change();
And:

Code: Select all

$('#contact_ch').val('1').change();
Maybe to populate a checkbox, .val().change() doesn't work?
You do not have the required permissions to view the files attached to this post.
Config:
nuBuilder v4 on a dedicated LAMP server:
-Ubuntu 14.04.5 LTS
-Apache 2.4.7
-MySQL 14.14 Distrib 5.7.22
-PHP 5.5.9-1ubuntu4.23
Locked