Forums

Prepopulate datepicker field with CB date

fribse 30 Apr, 2016
Hi there

My form tries to prepopulate as many fields as possible.
One I don't seem to get to work is a datepicker field:

$form->data['foedsel1'] = date_format( date_create_from_format( 'Y-m-d', $user->get('cb_birthday'), 'd-m-Y' ) );


The other way works as it should:

$user->set( 'cb_birthday', date_format( date_create_from_format( 'd-m-Y', $form->data['foedsel1'] ), 'Y-m-d' ) ) ;


But the debugger simply shows this field as empty in the load of the form.
Is that because the date format is wrong for the datepicker field, or maybe it can't be prepopulated?
GreyHead 30 Apr, 2016
HI fribse,

Is $user defined? I would add some debug code to see exactly what is happening.

Bob
fribse 30 Apr, 2016
Yes, and I can echo the 'raw' data correctly, so somehow I must've made the conversion wrong?
GreyHead 30 Apr, 2016
Hi fribse,

You have three nested functions there, I'd break that into three separate lines so that you can see if each one succeeds.

Bob
fribse 02 May, 2016
Answer
Of course you are right, so I broke it down, and now it works, the right conversion looks like this:

$form->data['foedsel1'] = date_format( date_create_from_format( 'Y-m-d' , $user->get('cb_birthday') ) , 'd-m-Y' ) ;

Though I do see this when activating it (with error reporting set to maximum of course):

Notice: Only variables should be assigned by reference in .../custom_code/custom_code.php(20) : eval()'d code on line 17
GreyHead 04 May, 2016
1 Likes
Hi Fribse,

The PHP Notice is irritating but harmless, check line 20 of your Custom Code, probably you have an =& there which can now be replaced by just =

Bob
This topic is locked and no more replies can be posted.