Default Date Value

bilal.abdeen 12 Apr, 2016
I want a "date" input element to show today's date as a default value. The user should still be able to use the Chrono's datepciker to change the value of the date. I have read few topics and a FAQ about this.

Finally, I got it to show the default value (today's date) correctly. However, the value that gets inserted in the database is wrong (day and month swapped.) For example, for the default value of today (12/04/2016), the value that gets inserted is (2016-12-04 00:00:00).

Furthermore, if the value is changed by the user, a completely wrong value is inserted. For example, if the user selects (23/08/2011), the value that gets inserted is (1970-01-01 10:00:00).

I have a feeling the problem is related to how the ChronoForm changes the date format.

Following are more details...
1. date element (payment_received_on) format: d/m/Y

2. Custom Code - just before "HTML (Render Form)":
if ( !isset($form->data['payment_received_on']) || !$form->data['payment_received_on'] ) {
$form->data['payment_received_on'] = date('d/m/Y'); // I have also tried date(Y-m-d H:i:s') and date(Y-m-d');
}

3. database format: DATETIME // I have also tried DATE
By the way, without setting the default value (DATETIME) works very smoothly.

Thank you very much for your effort and time to help...
Bilal
GreyHead 12 Apr, 2016
Hi Bilal,

To save in the database as DATE or DATETIME you need to convert the sate to yyyy-mm-dd format. You can do that in a Custom Code action, similarly you'd need to covert it back to read and re-display.

Bob
bilal.abdeen 13 Apr, 2016
Bob,

Yes, this is what I thought. Please, see below my code. What am I doing wrong? Moreover, attached is a screen shot of my setup.

Custom Code before rendering:
<?php
// Set Default date value for payment_received_on
if ( !isset($form->data['payment_received_on']) || !$form->data['payment_received_on'] ) {
$form->data['payment_received_on'] = date('d/m/Y');
}
?>

Custom Code at the beginning of Submit:
<?php
// Change date format
$form->data[payment_received_on] = date ("Y-m-d H:i:s", strtotime($form->data[payment_received_on]) );
?>

Thanks,
Bilal
GreyHead 13 Apr, 2016
Hi bilal,

The code looks OK to me by looking at it.

Add a debugger action or a line of PHP to echo out the values so that you can see exactly what is happening.

Bob
bilal.abdeen 13 Apr, 2016
Thank you so much for your effort and time... 😀
I would really appreciate looking at my other question too (http://www.chronoengine.com/forums/posts/f5/t101713/dynamic-options-for-dropdown-but-not-linked-to-an-event2.html)
-----------
Here you are....
I didn't change the date field before submitting the form. I left the default set value without touching it.
Please, refer to the screen shot in the previous message to see where this custom codes are in the setup.

1.1 Debugging Code - 1 (Custom Code 31):
echo "<p>inside (On Load) - AFTER rendering</p>";
echo "<p> payment_received_on: {$form->data[payment_received_on]} </p>" ;

1.1 Debugging Result - 1:
inside (On Load) - AFTER rendering
payment_received_on: 13/04/2016

--------------
2.1 Debugging Code - 2 (Custom Code 3):
echo "<p>inside (On Submit) - 1st thing: </p>";
echo "<p> payment_received_on: {$form->data[payment_received_on]} </p>" ;

2.2 Debugging Result - 2:
inside (On Submit) - 1st thing:
payment_received_on: 13/04/2016

----------------
3.1 Debugging Code - 3 - (Custom Code 26):
echo "<p>Event Switcher: Success</p>";
echo "<p>Custom Code 26 (just before db Save): </p>";
echo "<p> payment_received_on: {$form->data[payment_received_on]} </p>" ;

3.2 Debugging Result - 3:
Event Switcher: Success
Custom Code 26 (just before db Save):
payment_received_on: 1970-01-01 10:00:00
------------------
Debugger Output:

Array
(
[chronoform] => RegFormForAdmin
[event] => submit
[firstname] => Bilal
[lastname] => Abdeen
[email] => bilal.abdeen.n1@yahoo.com
[username] => bilal.abdeen.n1@yahoo.com
[mobile_phone] => 0406335228
[phone_no] => 0406335228
[address] => 7/3 West Terrace
[suburb] => Bankstown
[post_code] => 2200
[spoken_languages] => Array
(
[0] => Acehnese
[1] => Amharic
)

[ethnic_background] => Array
(
[0] => Aboriginal
[1] => Afghan
[2] => Albanian
)

[english_level] => None
[marital_status] => Married
[date_of_birth] => 20/01/2010 // Correct date - No default value was assigned
....
[transportation_method_used_to_come_to_iw] => Array
(
[0] => Bus
[1] => Walking
)

[i_give_my_consent_to_iwwa_to_use_my_phot] => No
[payment_flag] => Yes
[payment_amount] => 22
[membership_period] => 3
[payment_received_on] => 1970-01-01 10:00:00 // Wrong date - default value was assigned
[payment_received_by] => 505
[button177] => Register
[fullname] => Bilal Abdeen
[name] => Bilal Abdeen
....
[_PLUGINS_] => Array
(
[joomla_registration] => Array
(
[*isRoot] =>
[id] => 1885
[name] => Bilal Abdeen
[username] => bilal.abdeen.n1@yahoo.com
[email] => bilal.abdeen.n1@yahoo.com
[password] => $2y$10$oP4/jjwaifHXcyLM4aLZuOXE94R2c5NlGETuhetHFAva50ITuNdVC
[password_clear] => vRsGLe1P
[block] => 0
[sendEmail] => 0
[registerDate] => 2016-04-13 09:44:17
[lastvisitDate] => 2016-04-13 06:38:50
[activation] =>
[groups] => Array
(
[0] => 15
)

[lastResetTime] => 2016-03-18 06:10:12
[resetCount] => 1
[requireReset] => 0
[*_params] => Joomla\Registry\Registry Object
(
[data:protected] => stdClass Object
(
[admin_style] =>
[admin_language] =>
[language] =>
[editor] =>
[helpsite] =>
[timezone] =>
)

[separator] => .
)

[*_authGroups] => Array
(
[0] => 1
[1] => 2
[4] => 3
[5] => 4
[6] => 14
)

[*_authLevels] => Array
(
[0] => 1
[1] => 1
[2] => 2
[3] => 3
[4] => 4
)

[*_authActions] =>
[*_errorMsg] =>
[*userHelper] => JUserWrapperHelper Object
(
)

[*_errors] => Array
(
)

[aid] => 0
[otpKey] =>
[otep] =>
[cookieLogin] =>
[chronoform] => RegFormForAdmin
[event] => submit
[firstname] => Bilal
[lastname] => Abdeen
[mobile_phone] => 0406335228
[phone_no] => 0406335228
[address] => 7/3 West Terrace
[suburb] => Bankstown
[post_code] => 2200
[spoken_languages] => Array
(
[0] => Acehnese
[1] => Amharic
)

[ethnic_background] => Array
(
[0] => Aboriginal
[1] => Afghan
[2] => Albanian
)

[english_level] => None
[marital_status] => Married
[date_of_birth] => 20/01/2010 // Correct date - No default value was assigned
....
[transportation_method_used_to_come_to_iw] => Array
(
[0] => Bus
[1] => Walking
)

[i_give_my_consent_to_iwwa_to_use_my_phot] => No
[payment_flag] => Yes
[payment_amount] => 22
[membership_period] => 3
[payment_received_on] => 1970-01-01 10:00:00 // Wrong date
[payment_received_by] => 505
[button177] => Register
[fullname] => Bilal Abdeen
[password2] => vRsGLe1P
[usertype] => deprecated
)

)

[UserModel] => Array
(
[id] => 1885
[email] => bilal.abdeen.n1@yahoo.com
)

[PaymentModel] => Array
(
[payer_id] => 1885
[collector_id] => 505
[payment_amount] => 22
[payment_date] => 1970-01-01 10:00:00 // Wrong date - default value was assigned
[payment_type] => Membership
[membership_period] => 3
[payment_id] => 1391
)

)
------------
Array
(
)
-------------
Errors Array
(
)
--------------
Debug Info
Array
(
[27] => Array
(
[DB Read] => Array
(
[Queries] => Array
(
[0] => SELECT `UserModel`.`id` AS `UserModel.id`, `UserModel`.`email` AS `UserModel.email` FROM `iwwaj_users` AS `UserModel` WHERE `UserModel`.`email` = 'bilal.abdeen.n1@yahoo.com'
)

)

)

[25] => Array
(
[DB Save] => Array
(
[Queries] => Array
(
[0] => INSERT INTO `iwwaj_iwwa_payment` (`payer_id`, `collector_id`, `payment_amount`, `payment_date`, `payment_type`, `membership_period`) values ('1885', '505', '22', '1970-01-01 10:00:00', 'Membership', '3');
)

)

)

)
GreyHead 13 Apr, 2016
Hi bilal,

I found this StackOverFlow answer http://stackoverflow.com/a/15676380 and further down the page is a note saying that strtotime expects US format dates when the divider is / and UK format when the divider is -

Bob
bilal.abdeen 13 Apr, 2016
Bob,
You are the best... 😀
Thank you so much.

I am looking forward to receive your genius solution for my other quetion.

For the benefit of others who might be reading this. The following worked.
// Change date format at the beginning of the (On Submit) event - (Custom Code 3 in my screen shot of the Setup steps):
$date_array = explode('/',$form->data[payment_received_on]);
$form->data[payment_received_on] = $date_array[2].'-'.$date_array[1].'-'.$date_array[0];
bilal.abdeen 14 Apr, 2016
Bob,

It seems that there is an issue when the form fails and the user is asked to resubmit the form. Following are the details.

A. The form is displayed:
A.1 Custom Code 31 - (On Load) - AFTER rendering
payment_received_on: 15/04/2016
membership_period: Array ( [0] => Array ( [value] => 2014 [key] => 2014 ) [1] => Array ( [value] => 2015 [key] => 2015 ) [2] => Array ( [value] => 2016 [key] => 2016 ) [3] => Array ( [value] => 2017 [key] => 2017 ) [4] => Array ( [value] => 2018 [key] => 2018 ) )

B. The user fills in the form. However, the submitted username is already in use. So, the form fails submission, and it is redisplayed for the user to change the username before resubmission. I think my Custom Code, which was executed at the beginning of the "On Submit" event "interfers" with ChronoForm internal processing code.
B.2 Custom Code 3 - (On Submit) - 1st thing:
date before change: 15/04/2016
// My Code to change dm/Y to Y-m-d is here
date AFTER change: 2016-04-15
membership_period: 2014

B.1 Custom Code 31 - (On Load) - AFTER rendering
payment_received_on: 2016-04-15
membership_period: 2014
// Moreover, the list of options for the dropdown membership_period disappeared from the form, The default date value "looks" fine.

C. The user fixes the value of the username, and resubmits the form, without touching any other element. No value is inserted into the database for any of these 2 elements (membership_period, payment_received_on).
C.2 Custom Code 3 - (On Submit) - 1st thing:

date before change: 2016-04-15
date AFTER change: --2016-04-15 // I am not sure where (--) comes from! It might indicate a "duplicate" step in ChronoForm internal processing code, possibly caused by resubmitting the form.
membership_period:

C.3 Debugging Output:
Array
(
[chronoform] => RegFormForAdmin
[event] => submit
[firstname] => Bilal
[lastname] => Abdeen
[email] => bilal.abdeen.9@yahoo.com
[username] => bilal.abdeen.9@yahoo.com
......
[_PLUGINS_] => Array
(
.....
[PaymentModel] => Array
(
[payer_id] => 1897
[collector_id] => 1412
[payment_amount] => 99
[payment_date] => --2016-04-15
[payment_type] => Membership
[membership_period] =>
[payment_id] => 1412
)

)
----------
Array
(
)
--------
Errors
Array
(
)
--------
Debug Info:
.....
[DB Save] => Array
(
[Queries] => Array
(
[0] => INSERT INTO `iwwaj_iwwa_payment` (`payer_id`, `collector_id`, `payment_amount`, `payment_date`, `payment_type`, `membership_period`) values ('1897', '1412', '99', '--2016-04-15', 'Membership', '');
)

)
......
----------------
D. The user fixes the value of the username "AND SELECTS A DIFFERENT VALUE FOR PAYMENT_RECEIVED_ON", and resubmits the form. The dropdown element membership_period does not have any values to select from. Consequently, NO value is inserted into the database for it.
As mentioned, the user selects a new value for the date element (payment_received_on), and the selected value is correctly inserted into the db.
D.2 Custom Code 3 - (On Submit) - 1st thing:

date before change: 13/04/2015
date AFTER change: 2015-04-13
membership_period:

D.3 Debugging:
[PaymentModel] => Array
(
[payer_id] => 1898
[collector_id] => 502
[payment_amount] => 1
[payment_date] => 2015-04-13
[payment_type] => Membership
[membership_period] =>
[payment_id] => 1413
)
-------
[Queries] => Array
(
[0] => INSERT INTO `iwwaj_iwwa_payment` (`payer_id`, `collector_id`, `payment_amount`, `payment_date`, `payment_type`, `membership_period`) values ('1898', '502', '1', '2015-04-13', 'Membership', '');
)

)

Thanks a lot...
GreyHead 15 Apr, 2016
Hi Bilal,

I don't think it will affect the ChonoForms code but the logic might be faulty.

Have you tried moving the Custom Code after the Joomla Registration?

Bob
bilal.abdeen 15 Apr, 2016
Bob,

Which custom code do you mean?
1. To the best of my knowledge, the custom code for preparing the default values has to be before rendering. Otherwise the elements won't show. To double-check, I have tried moving it after the Joomla Registration. This did not help.
2. (membership_period) does not have any other custom code.
3. Although I cannot see how this can help, I have tried moving the custom code for changing the date format of (payment_received_on) after the Joomla Registration. This did not help.

If you meant some other Custom Code, please clarify.

To further analyse this, I suggest that we focus on fixing {membership_period), which is an array, first. When we get it right, we can look into (payment_received_on), which is a date field. Fiddling with date fields can be really tricky (at least to me.)

Looking at (B.1 Custom Code 31 - (On Load) - AFTER rendering) in my previous message, I noticed that the membership_period array became a single value. This is obviously correct, because this is the value, which was selected and submitted, and should be inserted into the database. However, because the form submission failed, we need to assign the complete array to this form element, for it to display all the options and having the "selected" option, designated as "selected."

In other words, when the user tried to submit the form, the form element changed
from: $form-data[membership_period] = Array ( [0] => Array ( [value] => 2014 [key] => 2014 ) [1] => Array ( [value] => 2015 [key] => 2015 ) [2] => Array ( [value] => 2016 [key] => 2016 ) [3] => Array ( [value] => 2017 [key] => 2017 ) [4] => Array ( [value] => 2018 [key] => 2018 ) )
to: $form-data[membership_period] = 2014

Then, "I think" ChronoForms at this point has no choice but to show the element as a simple text having the value 2014. Does this make sense?

If you agree with that, the question would be "Would it be possible to assign an array to the form element, when submission fails?" I tried to copy the default-setting code, which I was using just before rendering, into a Custom Code element inside (Joomla Registration - On Fail). However, that did not work.

If I was writing this in php, it would like the following. The user-entered value is saved in a session variable, which is checked before rendering the html. I am not sure how ChronoForms does it.

<?php
$period = date('Y'); // 2016
$periods[0] = $period - 2 ; // 2014
$periods[1] = $period - 1 ; // 2015
$periods[2] = $period ; // 2016
$periods[3] = $period + 1 ;
$periods[4] = $period + 2 ;

$membership_select_element_add = "<select name='membership_period' required >";
$membership_select_element_add .= "<option value='' " . ( (!$form_processed) ? 'selected' : '' ) . ">Please select a value</option>";
$membership_select_element_add .= "<option value='{$periods[0]}' " . ( ($form_processed && !$form_data_2['form_ok'] && $form_data_2['posted_form_data']['membership_period'] == $periods[0] ) ? 'selected' : '' ) . ">{$periods[0]}</option>";
$membership_select_element_add .= "<option value='{$periods[1]}' " . ( ($form_processed && !$form_data_2['form_ok'] && $form_data_2['posted_form_data']['membership_period'] == $periods[1] ) ? 'selected' : '' ) . ">{$periods[1]}</option>";
$membership_select_element_add .= "<option value='{$periods[2]}' " . ( ($form_processed && !$form_data_2['form_ok'] && $form_data_2['posted_form_data']['membership_period'] == $periods[2] ) ? 'selected' : '' ) . ">{$periods[2]}</option>";
$membership_select_element_add .= "<option value='{$periods[3]}' " . ( ($form_processed && !$form_data_2['form_ok'] && $form_data_2['posted_form_data']['membership_period'] == $periods[3] ) ? 'selected' : '' ) . ">{$periods[3]}</option>";
$membership_select_element_add .= "<option value='{$periods[4]}' " . ( ($form_processed && !$form_data_2['form_ok'] && $form_data_2['posted_form_data']['membership_period'] == $periods[4] ) ? 'selected' : '' ) . ">{$periods[4]}</option>";
$membership_select_element_add .= "</select>";

echo $membership_select_element_add;
?>

Thank you for your effort, time and patience... 😀
GreyHead 15 Apr, 2016
Hi Bilal,

I can't easily check the code as I'm now using my iPod Touch screen :-(

If the Joomla Registration action fails for any reason you should have an Event Loop action in the pink On Fail box that will reload the form OnLoad event and display the form. At that point you want the data in the correct format to be reloaded e.g. as an array.

If you already have code in the On Load event that may need to check the format before making any changes.

Bob
bilal.abdeen 16 Apr, 2016
Bob,

Thank you for the information. I have added the following code to a Custom Code inside "Joomla Registration - On Fail", and before (Event Loop). This worked fine for displaying the default value for the date element. However, I am not sure how to make the user-selected option for (membership_period) as selected.

<?php
// date default value 
if ( !isset($form->data['payment_received_on']) || !$form->data['payment_received_on'] ) {
  $form->data['payment_received_on'] = date('d/m/Y'); 
}
elseif ($form->data['payment_received_on']) {
   $date_array = explode('-',$form->data[payment_received_on]);
   $form->data[payment_received_on] = $date_array[2].'/'.$date_array[1].'/'.$date_array[0]; 
}

// options for membership_period  
$period = date('Y'); 		// 2016 
$periods[0] = $period - 2 ; // 2014 
$periods[1] = $period - 1 ; // 2015 
$periods[2] = $period ; 	// 2016
$periods[3] = $period + 1 ; 
$periods[4] = $period + 2 ; 

$membership_period = array();
$membership_period[0][key] = $membership_period[0][value] = $periods[0];
$membership_period[1][key] = $membership_period[1][value] = $periods[1];
$membership_period[2][key] = $membership_period[2][value] = $periods[2];
$membership_period[3][key] = $membership_period[3][value] = $periods[3];
$membership_period[4][key] = $membership_period[4][value] = $periods[4];

if ( !isset($form->data[membership_period]) || !$form->data[membership_period] ) {
  $form->data[membership_period] = array(); 
  $form->data[membership_period] = $membership_period;
}
elseif (isset($form->data[membership_period])) {
  $membership_period_old = $form->data[membership_period]; 
  $form->data[membership_period] = array(); 
  $form->data[membership_period] = $membership_period;
  // How to make ($membership_period_old) the selected value?
}
?>
This topic is locked and no more replies can be posted.