Skip Navigation

[Resolved] How to check for a Custom User Field value when inputting a CPT Post Form

This support ticket is created 3 years, 6 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.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 25 replies, has 2 voices.

Last updated by Christian Cox 3 years, 6 months ago.

Assisted by: Christian Cox.

Author
Posts
#2095083

Thank you Christian.
So just to confirm, once again I used selected to use the Client 'test10@akcreation.co.uk', filled out the Reference, and it posted successfully (when it should not have). And I also added the extra bit of code above to my functions.php file.
This time I will just paste in the code from Toolset (that is in my error_log.txt file) and leave out all of the stuff related to my page builder / theme etc. Please see the text below which was copied after running the latest test:

[21-Jun-2021 22:20:15 UTC] validating form: 1696
[21-Jun-2021 22:20:15 UTC] Array
(
[@client-reference.parent] => Array
(
[value] =>
[name] => @client-reference.parent
[type] => select
[repetitive] =>
[validation] => Array
(
[required] => Array
(
[message] => This field is required
[active] => 1
)

)

)

[organisation] => Array
(
[value] => Array
(
[0] => 3
)

[name] => organisation
[type] => taxonomyhierarchical
[repetitive] =>
)

[contract-type] => Array
(
[value] => Array
(
[0] => 4
)

[name] => contract-type
[type] => taxonomyhierarchical
[repetitive] =>
)

[post_title] => Array
(
[value] => ORC-908715-2021-06-21
[name] => post_title
[type] => textfield
[repetitive] => 0
[validation] => Array
(
[required] => Array
(
[active] => 1
[value] => 1
[message] => This field is required
)

)

)

[wpcf-contract-name] => Array
(
[value] => dbwjkl
[name] => wpcf-contract-name
[type] => textfield
[repetitive] => 0
[plugin_type] => types
[validation] => Array
(
[required] => Array
(
[active] => 1
[value] => true
[message] => This field is required. Please enter the contract name.
)

)

)

[wpcf-contract-value] => Array
(
[value] => 250000
[name] => wpcf-contract-value
[type] => numeric
[repetitive] => 0
[plugin_type] => types
[validation] => Array
(
[required] => Array
(
[active] => 1
[value] => true
[message] => This field is required. Please enter the contract value.
)

[number] => Array
(
[active] => 1
[message] => Please enter numeric data only (no letters or special characters can be entered here).
)

)

)

[wpcf-referee-name] => Array
(
[value] => John Smith
[name] => wpcf-referee-name
[type] => textfield
[repetitive] => 0
[plugin_type] => types
[validation] => Array
(
[required] => Array
(
[active] => 1
[value] => true
[message] => This field is required. Please enter the referee name
)

)

)

[wpcf-referee-organisation] => Array
(
[value] => Smith and Co
[name] => wpcf-referee-organisation
[type] => textfield
[repetitive] => 0
[plugin_type] => types
[validation] => Array
(
[required] => Array
(
[active] => 1
[value] => true
[message] => This field is required. Please enter the referee organisation name.
)

)

)

[wpcf-referee-position] => Array
(
[value] => Director
[name] => wpcf-referee-position
[type] => textfield
[repetitive] => 0
[plugin_type] => types
[validation] => Array
(
[required] => Array
(
[active] => 1
[value] => true
[message] => This field is required. Please enter the referee position.
)

)

)

[wpcf-reference-date] => Array
(
[value] => Array
(
[display-only] => June 21, 2021
[datepicker] => 1624233600
)

[name] => wpcf-reference-date
[type] => date
[repetitive] => 0
[plugin_type] => types
[validation] => Array
(
[required] => Array
(
[active] => 1
[value] => true
[message] => This field is required. Please select the date
)

[date] => Array
(
[active] => 1
[format] => mdy
[pattern] => check.format
[message] => Please enter a valid date.
)

)

)

[wpcf-em1] => Array
(
[value] => 1
[name] => wpcf-em1
[type] => select
[repetitive] =>
[plugin_type] => types
[validation] => Array
(
[required] => Array
(
[active] => 1
[value] => true
[message] => This field is required. Please select an option.
)

)

)

[wpcf-em2] => Array
(
[value] => 1
[name] => wpcf-em2
[type] => select
[repetitive] =>
[plugin_type] => types
[validation] => Array
(
[required] => Array
(
[active] => 1
[value] => true
[message] => This field is required. Please select an option.
)

)

)

[wpcf-em3] => Array
(
[value] => 1
[name] => wpcf-em3
[type] => select
[repetitive] =>
[plugin_type] => types
[validation] => Array
(
[required] => Array
(
[active] => 1
[value] => true
[message] => This field is required. Please select an option.
)

)

)

[wpcf-em4] => Array
(
[value] => 1
[name] => wpcf-em4
[type] => select
[repetitive] =>
[plugin_type] => types
[validation] => Array
(
[required] => Array
(
[active] => 1
[value] => true
[message] => This field is required. Please select an option.
)

)

)

[wpcf-cc5] => Array
(
[value] => 1
[name] => wpcf-cc5
[type] => select
[repetitive] =>
[plugin_type] => types
[validation] => Array
(
[required] => Array
(
[active] => 1
[value] => true
[message] => This field is required. Please select an option.
)

)

)

[wpcf-cc6] => Array
(
[value] => 1
[name] => wpcf-cc6
[type] => select
[repetitive] =>
[plugin_type] => types
[validation] => Array
(
[required] => Array
(
[active] => 1
[value] => true
[message] => This field is required. Please select an option.
)

)

)

[wpcf-cc7] => Array
(
[value] => 1
[name] => wpcf-cc7
[type] => select
[repetitive] =>
[plugin_type] => types
[validation] => Array
(
[required] => Array
(
[active] => 1
[value] => true
[message] => This field is required. Please select an option.
)

)

)

[wpcf-cc8] => Array
(
[value] => 1
[name] => wpcf-cc8
[type] => select
[repetitive] =>
[plugin_type] => types
[validation] => Array
(
[required] => Array
(
[active] => 1
[value] => true
[message] => This field is required. Please select an option.
)

)

)

[wpcf-tc9] => Array
(
[value] => 1
[name] => wpcf-tc9
[type] => select
[repetitive] =>
[plugin_type] => types
[validation] => Array
(
[required] => Array
(
[active] => 1
[value] => true
[message] => This field is required. Please select an option.
)

)

)

[wpcf-tc10] => Array
(
[value] => 1
[name] => wpcf-tc10
[type] => select
[repetitive] =>
[plugin_type] => types
[validation] => Array
(
[required] => Array
(
[active] => 1
[value] => true
[message] => This field is required. Please select an option.
)

)

)

[wpcf-tc11] => Array
(
[value] => 1
[name] => wpcf-tc11
[type] => select
[repetitive] =>
[plugin_type] => types
[validation] => Array
(
[required] => Array
(
[active] => 1
[value] => true
[message] => This field is required. Please select an option.
)

)

)

[wpcf-ud12] => Array
(
[value] => 1
[name] => wpcf-ud12
[type] => select
[repetitive] =>
[plugin_type] => types
[validation] => Array
(
[required] => Array
(
[active] => 1
[value] => true
[message] => This field is required. Please select an option.
)

)

)

[wpcf-ud13] => Array
(
[value] => 1
[name] => wpcf-ud13
[type] => select
[repetitive] =>
[plugin_type] => types
[validation] => Array
(
[required] => Array
(
[active] => 1
[value] => true
[message] => This field is required. Please select an option.
)

)

)

[wpcf-ud14] => Array
(
[value] => 1
[name] => wpcf-ud14
[type] => select
[repetitive] =>
[plugin_type] => types
[validation] => Array
(
[required] => Array
(
[active] => 1
[value] => true
[message] => This field is required. Please select an option.
)

)

)

[wpcf-ud15] => Array
(
[value] => 1
[name] => wpcf-ud15
[type] => select
[repetitive] =>
[plugin_type] => types
[validation] => Array
(
[required] => Array
(
[active] => 1
[value] => true
[message] => This field is required. Please select an option.
)

)

)

[wpcf-hs16] => Array
(
[value] => 1
[name] => wpcf-hs16
[type] => select
[repetitive] =>
[plugin_type] => types
[validation] => Array
(
[required] => Array
(
[active] => 1
[value] => true
[message] => This field is required. Please select an option.
)

)

)

[wpcf-o17] => Array
(
[value] => 1
[name] => wpcf-o17
[type] => select
[repetitive] =>
[plugin_type] => types
[validation] => Array
(
[required] => Array
(
[active] => 1
[value] => true
[message] => This field is required. Please select an option.
)

)

)

[wpcf-o18] => Array
(
[value] => 8
[name] => wpcf-o18
[type] => select
[repetitive] =>
[plugin_type] => types
[validation] => Array
(
[required] => Array
(
[active] => 1
[value] => true
[message] => This field is required. Please select your rating.
)

)

)

[wpcf-o19] => Array
(
[value] =>
[name] => wpcf-o19
[type] => wysiwyg
[repetitive] =>
[plugin_type] => types
)

[form_submit_1] => Array
(
[value] => Submit Reference
[name] => form_submit_1
[type] => submit
[repetitive] =>
)

[_cred_cred_wpnonce_cred_form_1696] => Array
(
[value] => 9e3afce043
[name] => _cred_cred_wpnonce_cred_form_1696
[type] => hidden
[repetitive] =>
)

[_cred_cred_prefix_post_id] => Array
(
[value] => 1728
[name] => _cred_cred_prefix_post_id
[type] => hidden
[repetitive] =>
)

[_cred_cred_prefix_cred_container_id] => Array
(
[value] => 39
[name] => _cred_cred_prefix_cred_container_id
[type] => hidden
[repetitive] =>
)

[_cred_cred_prefix_form_id] => Array
(
[value] => 1696
[name] => _cred_cred_prefix_form_id
[type] => hidden
[repetitive] =>
)

[_cred_cred_prefix_form_count] => Array
(
[value] => 1
[name] => _cred_cred_prefix_form_count
[type] => hidden
[repetitive] =>
)

)

[21-Jun-2021 22:20:15 UTC] post relationship field value: 1477

#2095095

Thanks Christian - I thought I had already posted this reply but it seems to have disappeared so I'm posting again (so apologies if you see this twice!) I have carried out what you instructed and this is the new code from my error_log.txt file (I left out all of the extra stuff to do with my page builder / theme this time and just put in the Toolset stuff):

[21-Jun-2021 22:20:15 UTC] validating form: 1696
[21-Jun-2021 22:20:15 UTC] Array
(
[@client-reference.parent] => Array
(
[value] =>
[name] => @client-reference.parent
[type] => select
[repetitive] =>
[validation] => Array
(
[required] => Array
(
[message] => This field is required
[active] => 1
)

)

)

[organisation] => Array
(
[value] => Array
(
[0] => 3
)

[name] => organisation
[type] => taxonomyhierarchical
[repetitive] =>
)

[contract-type] => Array
(
[value] => Array
(
[0] => 4
)

[name] => contract-type
[type] => taxonomyhierarchical
[repetitive] =>
)

[post_title] => Array
(
[value] => ORC-908715-2021-06-21
[name] => post_title
[type] => textfield
[repetitive] => 0
[validation] => Array
(
[required] => Array
(
[active] => 1
[value] => 1
[message] => This field is required
)

)

)

[wpcf-contract-name] => Array
(
[value] => dbwjkl
[name] => wpcf-contract-name
[type] => textfield
[repetitive] => 0
[plugin_type] => types
[validation] => Array
(
[required] => Array
(
[active] => 1
[value] => true
[message] => This field is required. Please enter the contract name.
)

)

)

[wpcf-contract-value] => Array
(
[value] => 250000
[name] => wpcf-contract-value
[type] => numeric
[repetitive] => 0
[plugin_type] => types
[validation] => Array
(
[required] => Array
(
[active] => 1
[value] => true
[message] => This field is required. Please enter the contract value.
)

[number] => Array
(
[active] => 1
[message] => Please enter numeric data only (no letters or special characters can be entered here).
)

)

)

[wpcf-referee-name] => Array
(
[value] => John Smith
[name] => wpcf-referee-name
[type] => textfield
[repetitive] => 0
[plugin_type] => types
[validation] => Array
(
[required] => Array
(
[active] => 1
[value] => true
[message] => This field is required. Please enter the referee name
)

)

)

[wpcf-referee-organisation] => Array
(
[value] => Smith and Co
[name] => wpcf-referee-organisation
[type] => textfield
[repetitive] => 0
[plugin_type] => types
[validation] => Array
(
[required] => Array
(
[active] => 1
[value] => true
[message] => This field is required. Please enter the referee organisation name.
)

)

)

[wpcf-referee-position] => Array
(
[value] => Director
[name] => wpcf-referee-position
[type] => textfield
[repetitive] => 0
[plugin_type] => types
[validation] => Array
(
[required] => Array
(
[active] => 1
[value] => true
[message] => This field is required. Please enter the referee position.
)

)

)

[wpcf-reference-date] => Array
(
[value] => Array
(
[display-only] => June 21, 2021
[datepicker] => 1624233600
)

[name] => wpcf-reference-date
[type] => date
[repetitive] => 0
[plugin_type] => types
[validation] => Array
(
[required] => Array
(
[active] => 1
[value] => true
[message] => This field is required. Please select the date
)

[date] => Array
(
[active] => 1
[format] => mdy
[pattern] => check.format
[message] => Please enter a valid date.
)

)

)

[wpcf-em1] => Array
(
[value] => 1
[name] => wpcf-em1
[type] => select
[repetitive] =>
[plugin_type] => types
[validation] => Array
(
[required] => Array
(
[active] => 1
[value] => true
[message] => This field is required. Please select an option.
)

)

)

[wpcf-em2] => Array
(
[value] => 1
[name] => wpcf-em2
[type] => select
[repetitive] =>
[plugin_type] => types
[validation] => Array
(
[required] => Array
(
[active] => 1
[value] => true
[message] => This field is required. Please select an option.
)

)

)

[wpcf-em3] => Array
(
[value] => 1
[name] => wpcf-em3
[type] => select
[repetitive] =>
[plugin_type] => types
[validation] => Array
(
[required] => Array
(
[active] => 1
[value] => true
[message] => This field is required. Please select an option.
)

)

)

[wpcf-em4] => Array
(
[value] => 1
[name] => wpcf-em4
[type] => select
[repetitive] =>
[plugin_type] => types
[validation] => Array
(
[required] => Array
(
[active] => 1
[value] => true
[message] => This field is required. Please select an option.
)

)

)

[wpcf-cc5] => Array
(
[value] => 1
[name] => wpcf-cc5
[type] => select
[repetitive] =>
[plugin_type] => types
[validation] => Array
(
[required] => Array
(
[active] => 1
[value] => true
[message] => This field is required. Please select an option.
)

)

)

[wpcf-cc6] => Array
(
[value] => 1
[name] => wpcf-cc6
[type] => select
[repetitive] =>
[plugin_type] => types
[validation] => Array
(
[required] => Array
(
[active] => 1
[value] => true
[message] => This field is required. Please select an option.
)

)

)

[wpcf-cc7] => Array
(
[value] => 1
[name] => wpcf-cc7
[type] => select
[repetitive] =>
[plugin_type] => types
[validation] => Array
(
[required] => Array
(
[active] => 1
[value] => true
[message] => This field is required. Please select an option.
)

)

)

[wpcf-cc8] => Array
(
[value] => 1
[name] => wpcf-cc8
[type] => select
[repetitive] =>
[plugin_type] => types
[validation] => Array
(
[required] => Array
(
[active] => 1
[value] => true
[message] => This field is required. Please select an option.
)

)

)

[wpcf-tc9] => Array
(
[value] => 1
[name] => wpcf-tc9
[type] => select
[repetitive] =>
[plugin_type] => types
[validation] => Array
(
[required] => Array
(
[active] => 1
[value] => true
[message] => This field is required. Please select an option.
)

)

)

[wpcf-tc10] => Array
(
[value] => 1
[name] => wpcf-tc10
[type] => select
[repetitive] =>
[plugin_type] => types
[validation] => Array
(
[required] => Array
(
[active] => 1
[value] => true
[message] => This field is required. Please select an option.
)

)

)

[wpcf-tc11] => Array
(
[value] => 1
[name] => wpcf-tc11
[type] => select
[repetitive] =>
[plugin_type] => types
[validation] => Array
(
[required] => Array
(
[active] => 1
[value] => true
[message] => This field is required. Please select an option.
)

)

)

[wpcf-ud12] => Array
(
[value] => 1
[name] => wpcf-ud12
[type] => select
[repetitive] =>
[plugin_type] => types
[validation] => Array
(
[required] => Array
(
[active] => 1
[value] => true
[message] => This field is required. Please select an option.
)

)

)

[wpcf-ud13] => Array
(
[value] => 1
[name] => wpcf-ud13
[type] => select
[repetitive] =>
[plugin_type] => types
[validation] => Array
(
[required] => Array
(
[active] => 1
[value] => true
[message] => This field is required. Please select an option.
)

)

)

[wpcf-ud14] => Array
(
[value] => 1
[name] => wpcf-ud14
[type] => select
[repetitive] =>
[plugin_type] => types
[validation] => Array
(
[required] => Array
(
[active] => 1
[value] => true
[message] => This field is required. Please select an option.
)

)

)

[wpcf-ud15] => Array
(
[value] => 1
[name] => wpcf-ud15
[type] => select
[repetitive] =>
[plugin_type] => types
[validation] => Array
(
[required] => Array
(
[active] => 1
[value] => true
[message] => This field is required. Please select an option.
)

)

)

[wpcf-hs16] => Array
(
[value] => 1
[name] => wpcf-hs16
[type] => select
[repetitive] =>
[plugin_type] => types
[validation] => Array
(
[required] => Array
(
[active] => 1
[value] => true
[message] => This field is required. Please select an option.
)

)

)

[wpcf-o17] => Array
(
[value] => 1
[name] => wpcf-o17
[type] => select
[repetitive] =>
[plugin_type] => types
[validation] => Array
(
[required] => Array
(
[active] => 1
[value] => true
[message] => This field is required. Please select an option.
)

)

)

[wpcf-o18] => Array
(
[value] => 8
[name] => wpcf-o18
[type] => select
[repetitive] =>
[plugin_type] => types
[validation] => Array
(
[required] => Array
(
[active] => 1
[value] => true
[message] => This field is required. Please select your rating.
)

)

)

[wpcf-o19] => Array
(
[value] =>
[name] => wpcf-o19
[type] => wysiwyg
[repetitive] =>
[plugin_type] => types
)

[form_submit_1] => Array
(
[value] => Submit Reference
[name] => form_submit_1
[type] => submit
[repetitive] =>
)

[_cred_cred_wpnonce_cred_form_1696] => Array
(
[value] => 9e3afce043
[name] => _cred_cred_wpnonce_cred_form_1696
[type] => hidden
[repetitive] =>
)

[_cred_cred_prefix_post_id] => Array
(
[value] => 1728
[name] => _cred_cred_prefix_post_id
[type] => hidden
[repetitive] =>
)

[_cred_cred_prefix_cred_container_id] => Array
(
[value] => 39
[name] => _cred_cred_prefix_cred_container_id
[type] => hidden
[repetitive] =>
)

[_cred_cred_prefix_form_id] => Array
(
[value] => 1696
[name] => _cred_cred_prefix_form_id
[type] => hidden
[repetitive] =>
)

[_cred_cred_prefix_form_count] => Array
(
[value] => 1
[name] => _cred_cred_prefix_form_count
[type] => hidden
[repetitive] =>
)

)

[21-Jun-2021 22:20:15 UTC] post relationship field value: 1477

#2095099

Okay great, we should be able to use the value from $_POST in this case. Here is the updated code using the value from $_POST:

add_filter('cred_form_validate','ts_ref_input_form_validation_staff', 10, 2);
function ts_ref_input_form_validation_staff( $field_data, $form_data ) {
$forms = array( 1696 );
$user_field_slug = 'orc-credits';
$reference_slug = 'client-reference';
$error_message = 'No remaining credits';
 
// field data are field values and errors
list($fields,$errors)=$field_data;
// validate if correct CRED form ID
if ( in_array( $form_data['id'], $forms ) ) {
error_log('validating form: ' . $form_data['id']);
error_log(print_r($fields, true));
// get the reference post ID
$ref_id = isset( $_POST['@' . $reference_slug .'_parent']) : $_POST['@' . $reference_slug .'_parent'] : 0;
if($ref_id) {
// get the reference post title (email address) and use that to query Users
$ref_title = get_the_title($ref_id);
error_log('ref_title: ' . $ref_title);
$user = get_user_by('email', $ref_title);
// get the User credits information from User field
$user_field = get_user_meta($user->ID, 'wpcf-'. $user_field_slug, true);
error_log('user_field: ' . $user_field);
// test the value of the user field. add an error message if no credits remain
if(intval($user_field) < 1) {
$errors[$reference_slug]=$error_message;
}
}
}
return array( $fields, $errors );
}

Let's try that change and if the results are still not what you expect, please provide the updated log information. If the results are more like you expect, you can delete all the error_log statements and turn off error logging in your wp-config.php file.

#2095103
client post 1477.jpg

also just in case it helps - I can see the number '1477'. I have checked and indeed '1477' is the post ID of the 'Client' Post for 'test10@akcreation.co.uk' (please see my attached acreenshot of it in the back end).
So I'm guessing we just need some way to get the either the title of that post (or the author of the post as they are both test10@akcreation.co.uk), and then to cross reference the user for test10@akcreation.co.uk, and the 'orc-credits' custom field value for that user? 🙂

Thanks again

#2095139

Thanks Christian. I'm really sorry but unfortunately that still did not work 🙁
I put in your code and it broke my site (as in I got a blank white screen on front and back end), I checked your code through a php validator and it seemed to suggest this line was incorrect:

$ref_id = isset( $_POST['@' . $reference_slug .'_parent']) : $_POST['@' . $reference_slug .'_parent'] : 0;

It said "unexpected ':' "
So I cross referenced it against your old code and it looks like the 1st ':' should be a '?', so I switched it for a '?' and then my site came back again. However like I say, the original problem still persists (the Reference post processed even though the user has 0 credits).

I checked the error_log.txt which I have pasted below, and even stranger, the last 2 lines seem to be what we want to see (ref_title: test10@akcreation.co.uk and user_field = 0), so I don't understand why it still isn't working? 🙁

[21-Jun-2021 23:01:11 UTC] validating form: 1696
[21-Jun-2021 23:01:11 UTC] Array
(
[@client-reference.parent] => Array
(
[value] =>
[name] => @client-reference.parent
[type] => select
[repetitive] =>
[validation] => Array
(
[required] => Array
(
[message] => This field is required
[active] => 1
)

)

)

[organisation] => Array
(
[value] => Array
(
[0] => 3
)

[name] => organisation
[type] => taxonomyhierarchical
[repetitive] =>
)

[contract-type] => Array
(
[value] => Array
(
[0] => 4
)

[name] => contract-type
[type] => taxonomyhierarchical
[repetitive] =>
)

[post_title] => Array
(
[value] => ORC-605512-2021-06-22
[name] => post_title
[type] => textfield
[repetitive] => 0
[validation] => Array
(
[required] => Array
(
[active] => 1
[value] => 1
[message] => This field is required
)

)

)

[wpcf-contract-name] => Array
(
[value] => dbwjkl
[name] => wpcf-contract-name
[type] => textfield
[repetitive] => 0
[plugin_type] => types
[validation] => Array
(
[required] => Array
(
[active] => 1
[value] => true
[message] => This field is required. Please enter the contract name.
)

)

)

[wpcf-contract-value] => Array
(
[value] => 250000
[name] => wpcf-contract-value
[type] => numeric
[repetitive] => 0
[plugin_type] => types
[validation] => Array
(
[required] => Array
(
[active] => 1
[value] => true
[message] => This field is required. Please enter the contract value.
)

[number] => Array
(
[active] => 1
[message] => Please enter numeric data only (no letters or special characters can be entered here).
)

)

)

[wpcf-referee-name] => Array
(
[value] => John Smith
[name] => wpcf-referee-name
[type] => textfield
[repetitive] => 0
[plugin_type] => types
[validation] => Array
(
[required] => Array
(
[active] => 1
[value] => true
[message] => This field is required. Please enter the referee name
)

)

)

[wpcf-referee-organisation] => Array
(
[value] => Smith and Co
[name] => wpcf-referee-organisation
[type] => textfield
[repetitive] => 0
[plugin_type] => types
[validation] => Array
(
[required] => Array
(
[active] => 1
[value] => true
[message] => This field is required. Please enter the referee organisation name.
)

)

)

[wpcf-referee-position] => Array
(
[value] => Director
[name] => wpcf-referee-position
[type] => textfield
[repetitive] => 0
[plugin_type] => types
[validation] => Array
(
[required] => Array
(
[active] => 1
[value] => true
[message] => This field is required. Please enter the referee position.
)

)

)

[wpcf-reference-date] => Array
(
[value] => Array
(
[display-only] => June 22, 2021
[datepicker] => 1624320000
)

[name] => wpcf-reference-date
[type] => date
[repetitive] => 0
[plugin_type] => types
[validation] => Array
(
[required] => Array
(
[active] => 1
[value] => true
[message] => This field is required. Please select the date
)

[date] => Array
(
[active] => 1
[format] => mdy
[pattern] => check.format
[message] => Please enter a valid date.
)

)

)

[wpcf-em1] => Array
(
[value] => 1
[name] => wpcf-em1
[type] => select
[repetitive] =>
[plugin_type] => types
[validation] => Array
(
[required] => Array
(
[active] => 1
[value] => true
[message] => This field is required. Please select an option.
)

)

)

[wpcf-em2] => Array
(
[value] => 1
[name] => wpcf-em2
[type] => select
[repetitive] =>
[plugin_type] => types
[validation] => Array
(
[required] => Array
(
[active] => 1
[value] => true
[message] => This field is required. Please select an option.
)

)

)

[wpcf-em3] => Array
(
[value] => 1
[name] => wpcf-em3
[type] => select
[repetitive] =>
[plugin_type] => types
[validation] => Array
(
[required] => Array
(
[active] => 1
[value] => true
[message] => This field is required. Please select an option.
)

)

)

[wpcf-em4] => Array
(
[value] => 1
[name] => wpcf-em4
[type] => select
[repetitive] =>
[plugin_type] => types
[validation] => Array
(
[required] => Array
(
[active] => 1
[value] => true
[message] => This field is required. Please select an option.
)

)

)

[wpcf-cc5] => Array
(
[value] => 1
[name] => wpcf-cc5
[type] => select
[repetitive] =>
[plugin_type] => types
[validation] => Array
(
[required] => Array
(
[active] => 1
[value] => true
[message] => This field is required. Please select an option.
)

)

)

[wpcf-cc6] => Array
(
[value] => 1
[name] => wpcf-cc6
[type] => select
[repetitive] =>
[plugin_type] => types
[validation] => Array
(
[required] => Array
(
[active] => 1
[value] => true
[message] => This field is required. Please select an option.
)

)

)

[wpcf-cc7] => Array
(
[value] => 1
[name] => wpcf-cc7
[type] => select
[repetitive] =>
[plugin_type] => types
[validation] => Array
(
[required] => Array
(
[active] => 1
[value] => true
[message] => This field is required. Please select an option.
)

)

)

[wpcf-cc8] => Array
(
[value] => 1
[name] => wpcf-cc8
[type] => select
[repetitive] =>
[plugin_type] => types
[validation] => Array
(
[required] => Array
(
[active] => 1
[value] => true
[message] => This field is required. Please select an option.
)

)

)

[wpcf-tc9] => Array
(
[value] => 1
[name] => wpcf-tc9
[type] => select
[repetitive] =>
[plugin_type] => types
[validation] => Array
(
[required] => Array
(
[active] => 1
[value] => true
[message] => This field is required. Please select an option.
)

)

)

[wpcf-tc10] => Array
(
[value] => 1
[name] => wpcf-tc10
[type] => select
[repetitive] =>
[plugin_type] => types
[validation] => Array
(
[required] => Array
(
[active] => 1
[value] => true
[message] => This field is required. Please select an option.
)

)

)

[wpcf-tc11] => Array
(
[value] => 1
[name] => wpcf-tc11
[type] => select
[repetitive] =>
[plugin_type] => types
[validation] => Array
(
[required] => Array
(
[active] => 1
[value] => true
[message] => This field is required. Please select an option.
)

)

)

[wpcf-ud12] => Array
(
[value] => 1
[name] => wpcf-ud12
[type] => select
[repetitive] =>
[plugin_type] => types
[validation] => Array
(
[required] => Array
(
[active] => 1
[value] => true
[message] => This field is required. Please select an option.
)

)

)

[wpcf-ud13] => Array
(
[value] => 1
[name] => wpcf-ud13
[type] => select
[repetitive] =>
[plugin_type] => types
[validation] => Array
(
[required] => Array
(
[active] => 1
[value] => true
[message] => This field is required. Please select an option.
)

)

)

[wpcf-ud14] => Array
(
[value] => 1
[name] => wpcf-ud14
[type] => select
[repetitive] =>
[plugin_type] => types
[validation] => Array
(
[required] => Array
(
[active] => 1
[value] => true
[message] => This field is required. Please select an option.
)

)

)

[wpcf-ud15] => Array
(
[value] => 1
[name] => wpcf-ud15
[type] => select
[repetitive] =>
[plugin_type] => types
[validation] => Array
(
[required] => Array
(
[active] => 1
[value] => true
[message] => This field is required. Please select an option.
)

)

)

[wpcf-hs16] => Array
(
[value] => 1
[name] => wpcf-hs16
[type] => select
[repetitive] =>
[plugin_type] => types
[validation] => Array
(
[required] => Array
(
[active] => 1
[value] => true
[message] => This field is required. Please select an option.
)

)

)

[wpcf-o17] => Array
(
[value] => 1
[name] => wpcf-o17
[type] => select
[repetitive] =>
[plugin_type] => types
[validation] => Array
(
[required] => Array
(
[active] => 1
[value] => true
[message] => This field is required. Please select an option.
)

)

)

[wpcf-o18] => Array
(
[value] => 6
[name] => wpcf-o18
[type] => select
[repetitive] =>
[plugin_type] => types
[validation] => Array
(
[required] => Array
(
[active] => 1
[value] => true
[message] => This field is required. Please select your rating.
)

)

)

[wpcf-o19] => Array
(
[value] =>
[name] => wpcf-o19
[type] => wysiwyg
[repetitive] =>
[plugin_type] => types
)

[form_submit_1] => Array
(
[value] => Submit Reference
[name] => form_submit_1
[type] => submit
[repetitive] =>
)

[_cred_cred_wpnonce_cred_form_1696] => Array
(
[value] => bb6f45aafa
[name] => _cred_cred_wpnonce_cred_form_1696
[type] => hidden
[repetitive] =>
)

[_cred_cred_prefix_post_id] => Array
(
[value] => 1730
[name] => _cred_cred_prefix_post_id
[type] => hidden
[repetitive] =>
)

[_cred_cred_prefix_cred_container_id] => Array
(
[value] => 39
[name] => _cred_cred_prefix_cred_container_id
[type] => hidden
[repetitive] =>
)

[_cred_cred_prefix_form_id] => Array
(
[value] => 1696
[name] => _cred_cred_prefix_form_id
[type] => hidden
[repetitive] =>
)

[_cred_cred_prefix_form_count] => Array
(
[value] => 1
[name] => _cred_cred_prefix_form_count
[type] => hidden
[repetitive] =>
)

)

[21-Jun-2021 23:01:11 UTC] ref_title: test10@akcreation.co.uk
[21-Jun-2021 23:01:11 UTC] user_field: 0

#2095199

Okay I apologize for the inconvenience, there was a typo in the code and you picked it up correctly. The first ':' should be '?'. The ref_title is now finding the email address test10@akcreation.co.uk, and user _field = 0, so that should trigger the error as expected and prevent the post from being created. There must be a problem with the way the error is set now. I was using a post reference field in my local, and I think it must be handled a bit differently. Let me set up a more accurate test in my local environment to try to pinpoint the problem, and I'll give you an update shortly.

#2095221
Screen Shot 2021-06-21 at 9.51.50 PM.png

I set up a Form with a relationship field for a one-to-many relationship, which allows the User to select the parent post inside the child post creation Form. I see the error key was incorrect, it should be @relationship-slug.parent. So the correct code to set that error inside the intval($user_field) conditional should be:

$errors['@' . $reference_slug . '.parent'] = $error_message;

I set up a more complete test scenario, with a parent post using an email address as the post title. That email address title corresponds to the email address of a known User. I added a custom field with a numeric value in that User profile, and tested with different values like 11, 0, and also an empty value. I am seeing the validation results I expect now in my local test (screenshot attached for a validation failure scenario), so I think we are getting very close now. Here is the updated code using your actual field slugs, with error logs commented out:

// Reference: https://toolset.com/forums/topic/how-to-check-for-a-custom-user-field-value-when-inputting-a-cpt-post-form/
add_filter('cred_form_validate','ts_ref_input_form_validation_staff', 10, 2);
function ts_ref_input_form_validation_staff( $field_data, $form_data ) {
  $forms = array( 1696 );
  $user_field_slug = 'orc-credits';
  $reference_slug = 'client-reference';
  $error_message = 'No remaining credits';
  // field data are field values and errors
  list($fields,$errors)=$field_data;
  // validate if correct CRED form ID
  if ( in_array( $form_data['id'], $forms ) ) {
    // get the reference post ID
    $ref_id = isset( $_POST['@' . $reference_slug .'_parent']) ? $_POST['@' . $reference_slug .'_parent'] : 0;
    //error_log('refid: ' . $ref_id);
    if($ref_id) {
      // get the reference post title (email address) and use that to query Users
      $ref_title = get_the_title($ref_id);
      //error_log('ref title: ' . $ref_title);
      $user = get_user_by('email', $ref_title);
      // get the User credits information from User field
      $user_field = get_user_meta($user->ID, 'wpcf-'. $user_field_slug, true);
      //error_log('user field: ' . $user_field);
      if(intval($user_field) < 1) {
        //error_log('inside intval user field conditional: ' . intval($user_field));
        $errors['@' . $reference_slug . '.parent'] = $error_message;
      }
    }
  }
  return array( $fields, $errors );
}

I think this latest test more accurately reflects your existing setup, so the syntax for error/field keys should be consistent in both our cases. If this is still not working as expected, please follow these next steps:
- Uncomment the error log statements include the newest log information in your next reply.
- Let me know if the Form is using AJAX or using standard submissions.
- Confirm you have a Form Messages block in the drag-and-drop Form builder, or if you are using expert mode in the Form builder, confirm the corresponding shortcode for the form_messages field exists:

[cred_field field='form_messages' class='alert alert-warning']

- Let me know the setting you have chosen for "After visitors submit this form:" in the Form editor.

If it's working more like you expect now, I also recommend testing a scenario where the post title email address does not match the email address of any known User. This line will trigger a PHP notice "Trying to get property 'ID' of non-object" if the email address query fails to produce a matching User:

$user_field = get_user_meta($user->ID, 'wpcf-'. $user_field_slug, true);

You may want to add some conditional logic to display a different message in this scenario, bypassing this line and the intval conditional, if you think it is possible for User email addresses or parent post titles to change and become out of sync. As it is now, the post will not be created upon submission, but the error message might be misleading.

#2095601

Hi Christian - you will be delighted to hear that this has now worked!!! 🙂

Thank you so much I am beyond grateful for your help and perseverance in getting this issue soved for me 🙂

There are one or two other small issues, but I can try and deal with those myself (now I know how to debug and check value in the variables that is a HUGE help) - the main thing is, the form now is stopped from submitting the post if the selected User does not have credits - wonderful.

One question I do have, is regarding the error message which now displays above my form. So on my post form code, I have the following shortcode in there:

[cred_field field='form_messages' class='uk-alert uk-alert-warning']

Notice I have slightly changed the css classes of the alert to 'uk-alert' etc (this is to use my theme framework styling, I am not using bootstrap but the uikit framework). However, the error message which displays is wrapped in the classes 'alert' and 'alert warning', so it reverts back to the bootstrap classes, even though I have overwritten those in my 'form_messages' shortcode.

Do you have any idea why that might be?

And thanks again Christian, your help has been really above and beyond 🙂 Hopefully this thread will at least help other users who might need a similar scenario!

#2095731

Great - glad you're able to use debug effectively now! Hopefully this saves you some time and headache in the future. I've split off your question about CSS classes into a new ticket, because I think we have a separate issue there. It seems the classes applied in the messages field are not added in the front-end. I will need to investigate and escalate this issue separately. I will follow up in the new ticket shortly.

#2095885

thank you Christian 🙂
I will revert to the new ticket.

Also (my apologies but I genuiniely think this is the last issue I have left) I had to create one more ticket - basically after this Reference Post submits (and checks for the ORC Credits), I need it then then reduce the Credit field by 1 (basically the client is paying to submit a Reference each time).

I tried to write my own code which looks like it should work (using the code from this thread and another thread), but it doesn't (I really am sorry).

So I created another ticket here = https://toolset.com/forums/topic/how-to-reduce-a-different-users-custom-user-field-value-by-1-after-submitting/

It would be great if you could pick this one up too as you are familiar with how my project fits together, and it's so closely related to this thread. Now that I have error checking / debugging setup, hopefully what I have explained in the new thread will help a lot!

I promise after this I will disappear (I need a holiday as this project has stressed me out so much haha!)

Thank you so much again for your help,

Keith

#2095937

Sure, no problem! I'll assign your new ticket to myself. There are several tickets ahead of yours in the queue, and I'll get to it in turn.