Skip Navigation

[Resolved] making a field required

This support ticket is created 3 years, 10 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
- 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/Hong_Kong (GMT+08:00)

This topic contains 8 replies, has 2 voices.

Last updated by edr-2 3 years, 10 months ago.

Assisted by: Luo Yang.

Author
Posts
#1653363
cred-auto-draft.png
function-code-for-required.png

Tell us what you are trying to do? i've added a hierarchical taxonomy to the cred, but both parent and child need to be checked
for it to be required, yet it passes thru with only parent checked.

Is there any documentation that you are following? i found user code, and after testing it didn't seem to work for what i need. see pic of current code usage.

Is there a similar example that we can see?

What is the link to your site? hidden link
scroll to bottom, see the parent child there. then fill out form and submit.
other oddities lately abound, not just this issue. see some pics. the cred auto draft may have something to do with added caching plugins, i deactivated the plugin though, or its caching, and still that weirdness remains.
also toolset cred is slow to load the checkboxes, and seeing odd output from its loading.

#1653809

Hello,

The filter cred_filter_field_before_add_to_form will not work as your expect on taxonomy field.

You can use another filter hook "cred_form_validate" to do the validation, see our document:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_form_validate
This hook provides custom validation for form fields in server side after user submit the form.

See similar thread:
https://toolset.com/forums/topic/how-to-make-taxonomies-required-fields/#post-686993

#1654327

Hi Luo,
I have actually tried several variations of the validate api such as in
add_filter('cred_form_validate','my_validation',10,2);
function my_validation($error_fields, $form_data)
{
//field data are field values and errors
list($fields,$errors)=$error_fields;
//uncomment this if you want to print the field values
//print_r($fields);
//validate if specific form
if ($form_data['id']==1166)
{
//check my_field value
// if ($fields['statecounty']['value']=='')
// {
// //set error message for my_field
// $errors['statecounty']='Wrong Value';
// }
// if (empty($fields['statecounty']['value']))
// {
// //set error message for featured image
// $errors['statecounty'] = 'Missing value';
// }
//For select and checkboxes fields, values are stored as arrays:
$fields['statecounty']['value']=array(
'value1',
'value2',
'value3'
);
print_r($fields);

}
//return result
return array($fields,$errors);
}
but they do not seem to catch either of the cred's several ways of outputting the taxonomic field, whether checkboxes, or as dropdowns.
here is the portion of the cred that i am currently trying to validate
Enter state and county here, then scroll to bottom to submit (Required)
Select Both State and County
<div class="boxes"];
[cred_field scaffold_field_id='statecounty' field='statecounty' force_type='taxonomy' output='bootstrap' display='checkbox' label='State and County']
</div>

[cred_field field="form_submit" value="Submit" urlparam=""]

[/credform]

when i submit the form other fields are highlighted but not the taxonomy, since this is a parent hierarchy, how do i check both that the parent and that the child are clicked? in some testing, as in a dropdown display, clicking on one would pass validation.

#1654355

the commented out portions of the validate function were all tried earlier. but actually the only api call that seemed to find the field was the before safe cred calls, if i remember. i am using the checkbox fields but would it be easier to validate if it were a dropdown?
and i also tried the code referenced , with values 'value1' value2 and 'value3' - but i am not sure what i should have in those strings.
( all state names and county names?)

#1654359

would this be a lot easier if i somehow changed the custom post type to have as a field a state and a county?

#1654887

You can try to put below PHP codes in your theme file "functions.php":

add_filter( 'cred_form_validate', function($field_data, $form_data) {
  
    // Split $field_data into separate $fields and $errors
    list( $fields,$errors ) = $field_data;
       
    // validate specific form
    if ( $form_data['id'] == 1166) {
  
        // check at least one of required taxonomies set
        if ( empty( $fields['statecounty']['value'] )  ) {
  
            $errors['statecounty'] = 'You must choose at least one statecounty';
        }
 
    }
  
    return array($fields,$errors);
}, 10, 2 );

Please replace 1166 with your post form ID, and you don't need to use select dropdown.

For the new question:
would this be a lot easier if i somehow changed the custom post type to have as a field a state and a county?

It depends on your self, you can also setup two custom fields to replace the taxonomy field "statecounty":
- state
- county

See below thread:
https://toolset.com/forums/topic/how-do-implement-country-state-dropdown-in-post-forms/
Above thread is using two custom fields to populate country and state using JS codes, but in your case, it will need to modify the JS file manually.

#1655645
post-id-1166.png
field-is-required-not-showing-for-statecounty.png

Dear Luo,
that code, and multiple other php tries, do not work. the POST ID is 1166.
as well, adding the jQuery code provides a "none checked" alert.
jQuery(document).on( 'cred_form_ready', function() {

if (jQuery('input[name="statecounty"]').prop("checked"))
alert("first button checked");
else
alert("none checked");

});

i've tried to clean up the form as well, removing unnecessary divs.
i've used multiple php codes, from multiple articles. the taxonomy is a parent child, with a state as a parent, and the county is a child. i've used checkboxes and dropdowns. in one case i was able to see that the code worked i think for a dropdown but it only caught the parent, not if the child was checked. both have to be enforced. can you escalate this issue?

#1655927

i was able to make progress using jQuery ie;
jQuery( "#cred_form_1166_1" ).submit(function( event ) {
alert( "Handler for .submit() called." );
// event.preventDefault();
var sList = "";
var found = "";
var doSubmit = 0;
tmp = 0;
jQuery('input[type=checkbox]').each(function () {
var sThisVal = (this.checked ? "1" : "0");
if ( this.checked )
tmp = tmp + 1;
// alert ( " tmp is now " + tmp );
if ( tmp == 2 ) {
// alert ( "sThisVal is now " + sThisVal);
doSubmit = 1;
}

// create a list of values
sList += (sList=="" ? sThisVal : "," + sThisVal);
// need to search for 1 here

// found = sList.search("1");

// if (found == -1 )
// ;

// else
// tmp = tmp + 1;
});

// alert ( " do submit = " + doSubmit );
if (doSubmit == 1 )
alert ("form will submit ok ");
else {
alert (" you need to fill out state and county correctly ");
event.preventDefault();
}
});

so i will close this case.

#1655933

My issue is resolved now. Thank you!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.