Skip Navigation

[Resolved] Re-open: CRED: assign value of another field, if field is empty

This support ticket is created 4 years, 4 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Our next available supporter will start replying to tickets in about 2.68 hours from now. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 2 replies, has 2 voices.

Last updated by Lara 4 years, 4 months ago.

Assisted by: Waqar.

Author
Posts
#1709935

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

#1710193

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

#1710433

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.