Jump to content

Check if field is required via API


Robin S
 Share

Recommended Posts

Does PW have any API method for checking if a given field is in fact required on a given page? I'm thinking here of fields with a 'Required only if' condition.

Or do I have to write my own code for parsing the field, operator and value from the $field->requiredIf string and checking against the page's fields?

I know about the functions in inputfields.js but I want to check the required status in PHP.

Link to comment
Share on other sites

ProcessWire doesn't have concept of required fields. There are only required "inputs", which is just for the forms handling editing pages.

You can find more about it in the discusions here https://github.com/ryancramerdesign/ProcessWire/issues/1743 and maybe a bit here https://github.com/processwire/processwire-issues/issues/252

  • Like 4
Link to comment
Share on other sites

Thanks, interesting discussion in that first GitHub issue.

It's the second GitHub issue that got me thinking about this in the first place. I understand that the current implementation only checks against the inputs in Page Edit, but that neglects the cases where a page may be created/populated/published via the API, which is an equally valid way to publish content.

The way I think of the 'required fields' feature is that the fields are 'strongly recommended' rather than strictly required since it is possible to leave required fields empty in many different ways. But the feature should provide the information that a field has been marked as 'required' for this circumstance (template overrides, 'required if' settings, etc), and that information should be available from the API as well as Page Edit.

Will look into this some more and consider placing a GitHub request.

  • Like 1
Link to comment
Share on other sites

6 hours ago, Robin S said:

But the feature should provide the information that a field has been marked as 'required' for this circumstance (template overrides, 'required if' settings, etc), and that information should be available from the API as well as Page Edit.

+1 As it sounds like a good compromise.

Link to comment
Share on other sites

Checking for the required field is quite easy ($field->required). Doing it with evaluating required_if, show_if settings is a lot more complex. You can look at the code of that in the InputfieldWrapper / InputfieldForm classes. One potential problem here is that show_if / required_if could be including references to fields added by hooks into the ProcessPageEdit form, where it wouldn't be possible to evaluate the requirement without ever creating the whole form. Also – as you can see in the mentioned classes – the current way of evaluation those requirements is 100% based on working with a InputfieldForm instance and not a page, so we would need to rebuild the whole thing to support such a feature.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...