Tell us what you are trying to do?
https://toolset.com/forums/topic/cred-assign-value-of-another-field-if-field-is-empty/
I accidently marked this topic as solved - although it wasn't solved - as one of my beta tester pointed out later.
My fault.
Here is the code again:
add_action('cred_before_save_data', 'func_assign_field_value',10,1);
function func_assign_field_value($form_data)
{
// if a specific form
$forms = array( 5629 , 10440 , 10362 );
if (in_array($form_data['id'], $forms))
{
// if value of wpcf-rask-bis-tag field is empty
if (empty($_POST['wpcf-rask-bis-tag'])) {
// make wpcf-rask-bis-tag field use the same value as the wpcf-rask-von-tag field
$_POST['wpcf-rask-bis-tag'] = $_POST['wpcf-rask-von-tag'];
}
}
}
Through the code I try to check if the ['wpcf-rask-von-tag'] field is empty - and if "empty" is "true", then ['wpcf-rask-von-tag'] should have the same value like the ['wpcf-rask-von-tag'] field. The ['wpcf-rask-von-tag'] field and the ['wpcf-rask-von-tag'] field are datepicker fields. My impression is that it doesn't work, because somehow the datepickerfied does never throw back empty - even if the user doesn't set a date yet. Is there a way to check if there is a date set in a datepicker field?
Is there any documentation that you are following?
There was a documentation, but they used it for a single line field.
Is there a similar example that we can see?
no
What is the link to your site?
hidden link
Hi,
Thanks for writing back and I'm sorry I missed the case of the date type fields.
For the data type fields, the form's submitted data is sent as an array and not a single value and therefore the code to check for an empty value will need to be slightly modified:
add_action('cred_before_save_data', 'func_assign_field_value',10,1);
function func_assign_field_value($form_data)
{
// if a specific form
$forms = array( 5629 , 10440 , 10362 );
if (in_array($form_data['id'], $forms))
{
// if value of wpcf-rask-bis-tag field is empty
if (empty($_POST['wpcf-rask-bis-tag']['datepicker'])) {
// make wpcf-rask-bis-tag field use the same value as the wpcf-rask-von-tag field
$_POST['wpcf-rask-bis-tag'] = $_POST['wpcf-rask-von-tag'];
}
}
}
This should do the trick.
regards,
Waqar
Many thanks for the explanation, Waqar - I appreciate to learn and improve and also many thanks for the code improvement. It did the trick. But it was my fault, I should have shared earlier, that it was a date field. I didn't mention. The issue is resolved now.