Hi,
I am creating forms to edit fields of a custom post type and would like to understand this.
In my custom post type I have fields, example field1, field2 , field3 and field4. Field2 and field4 are required.
I wanted to understand this:
- From the cred forms can I set a field, example field1, as mandatory even if it is not?
- If I believe an edit cred form, and I enter camp1 and camp2 (camp3 and camp4 are not entered) is the form saved even if camp4 is mandatory (and it is empty because it was not valorized because it was not entered in the cred form)?
Hello. Thank you for contacting the Toolset support.
In my custom post type I have fields, example field1, field2 , field3 and field4. Field2 and field4 are required.
I wanted to understand this:
- From the cred forms can I set a field, example field1, as mandatory even if it is not?
==>
Yes, you can make the field as required on fly using the Toolset form hook: cred_filter_field_before_add_to_form
For example:
add_filter('cred_filter_field_before_add_to_form', 'required_fields_func', 10, 1);
function required_fields_func($field){
//error_log(print_r($field, true));
if(in_array($field['id'], array('field1'))){
// in some cases $fields['data'] is an empty string, so you'll need to first set it's expected format for PHP 7.1 compatibility
if (!is_array($field['data'])) {
$field['data'] = array();
}
$field['data']['validate']['required'] = array (
'active' => 1,
'message' => 'This field is required'
) ;
}
return $field;
}