Home › Forums › Front-end Issues › Disabled and required Post Object field fails validation
Hi all,
So here’s the problem.
I have a front-end form using acf_form(). It contains two post_object
fields. Both are marked as required. Both are pre-filled and disabled via acf/load_field
filter.
I need to keep them required to ensure a back-end admin won’t leave them empty. But I also have to disable them on the front-end.
function populate_application_fields($field){
if ($field['wrapper']['class'] == 'application-job') {
if (!is_admin()) {
$value = get_value_from_somewhere();
$field['value'] = $value;
$field['disabled'] = true;
}
}
return $field;
}
add_filter('acf/load_field', 'populate_application_fields');
When the form is submitted, they both get an error messages saying “…value is required”, even despite the fact they have <select> tags with one <option selected=”selected”>.
If I do not disable one of these fields (the other one stays disabled), the first one passes the validation and the second one fails.
I tried to mark these two fields as valid via acf/validate_value
filter, but it doesn’t seem to work with is_admin()
.
When you disable a field in html, any field, the value of that field is not submitted. This is working as expected and it’s the way that browsers work. If you want to have the value submitted but not editable then you would use readonly.
Hi @hube2,
Thanks, this was helpful.
I need to rephrase my issue and explain it a bit better.
I need to show an ACF form on the front-end to add/edit posts of custom type “Application”. Fields “Job” and “Company” are pre-selected and must be always blocked or marked as read-only (user can’t change them), but I don’t know how I can do it with a Post Object field type (and whether it’s possible at all).
I tried smth like this:
acf.add_action('select2_init', function($input, args, settings, $field){
if ($field.hasClass('application-job')) {
args.disabled = true;
settings.disabled = true;
args.readonly = true;
settings.readonly = true;
}
});
Unfortunately, none of this worked.
On the back-end fields “Job” and “Company” must be enabled and required so an administrator cannot leave them empty.
If you have an idea how I can reach this goal, please share 🙂
As the last possible (alternative) solution I can do the following:
Create another fields group, where both Post Object fields are replaced by Text fields. Text fields can be made read-only very easily. This field group will be shown only on the front-end.
And my task would be just to synchronize them on post update.
Select2, as far as I can remember, has an issue with readonly, and they don’t plan on correcting this issue, https://github.com/select2/select2/issues/3387. There are some links to some possible work-a-rounds posted in that ticket.
Hi @hube2,
Thanks for your help!
I decided to clone the field group and use a copy with Post Object fields on the back-end, and a copy with Text fields on the front-end.
It works great and loads faster, because Select2 script isn’t used.
The topic ‘Disabled and required Post Object field fails validation’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.