Robin S Posted May 8, 2017 Share Posted May 8, 2017 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 More sharing options...
LostKobrakai Posted May 8, 2017 Share Posted May 8, 2017 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 4 Link to comment Share on other sites More sharing options...
Robin S Posted May 8, 2017 Author Share Posted May 8, 2017 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. 1 Link to comment Share on other sites More sharing options...
szabesz Posted May 9, 2017 Share Posted May 9, 2017 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 More sharing options...
LostKobrakai Posted May 9, 2017 Share Posted May 9, 2017 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now