Jump to content

Required field option


pers0n
 Share

Recommended Posts

  • 3 months later...

Thanks Soma, I was just about to ask for this feature. For such an advanced system it seems strange not to have a "required' feature for fields. Is there any reason this is hidden by default?

Also would be interested in a feature to limit characters for a field (ie: summary fields should only be 256 characters). I know this can be done in the code but I'd really like the user to be told to resubmit his summary if it is too long.

My apologies, don't think I have my head switched on today! Of course you can limit characters on a text field.

Would still be very interested in the required thing though....

Edited by onjegolders
Link to comment
Share on other sites

Proper required fields aren't quite as easy as they sound in ProcessWire. There are some problematic scenarios. For instance, what if you edit a published page, make a bunch of edits and leave a required field blank. When you save, if there is a missing required field, then logically the save should be aborted. But any other changes you made that were valid would also have to be aborted, otherwise we'd have a page in a partially saved state (which is not possible). A page can't be in two states at once. And if we're saying we support real required fields, we can't have a published page in the system in a state where one of the required fields is blank. So the required field support you see in PW right now is not much more than symbolic, because the page can still exist without a required field populated. PW will complain and give you errors telling you about it, but it can still exist.

Real required field support means letting a copy of a given page exist in some unpublished/draft state, until the requirements are met. Once the requirements are met, it can replace the published page. Until versioning is built in, making this happen is overly complex. So until that happens, any required field support will likely just be symbolic required field support via javascript or PW error messages. People will still want to code their templates to account for empty value situations, even on required fields. That's the primary reason why the 'required' option is hidden in advanced mode, because I don't want to imply that you can always expect some field to always be populated.

Another scenario that not even versioning can fix: lets say you have a site with a bunch of active pages in it. You add a new field to a heavily used template and make it 'required'. All the existing pages will have an empty required field. It's a tricky situation. The expectation of required fields seems to open more potential for site-breaking situations. Better to code things as if required fields didn't exist.

Link to comment
Share on other sites

Proper required fields aren't quite as easy as they sound in ProcessWire. There are some problematic scenarios. For instance, what if you edit a published page, make a bunch of edits and leave a required field blank. When you save, if there is a missing required field, then logically the save should be aborted. But any other changes you made that were valid would also have to be aborted, otherwise we'd have a page in a partially saved state (which is not possible). A page can't be in two states at once. And if we're saying we support real required fields, we can't have a published page in the system in a state where one of the required fields is blank. So the required field support you see in PW right now is not much more than symbolic, because the page can still exist without a required field populated. PW will complain and give you errors telling you about it, but it can still exist.

Real required field support means letting a copy of a given page exist in some unpublished/draft state, until the requirements are met. Once the requirements are met, it can replace the published page. Until versioning is built in, making this happen is overly complex. So until that happens, any required field support will likely just be symbolic required field support via javascript or PW error messages. People will still want to code their templates to account for empty value situations, even on required fields. That's the primary reason why the 'required' option is hidden in advanced mode, because I don't want to imply that you can always expect some field to always be populated.

Another scenario that not even versioning can fix: lets say you have a site with a bunch of active pages in it. You add a new field to a heavily used template and make it 'required'. All the existing pages will have an empty required field. It's a tricky situation. The expectation of required fields seems to open more potential for site-breaking situations. Better to code things as if required fields didn't exist.

OK thanks for taking the time to explain that!

I think allowing for empty fields will make me a better coder in the long run anyway!

  • Like 1
Link to comment
Share on other sites

The less complicated solution to this problem is to use javascript and hook into the system on before save (not familiar with PW internals yet). MODx approached this in the evolution build with a module that used a simple text config file which could have field by field options by template in the backend, such as required or even replacing the label text. Worked well for the intended purpose of "helping" publisher/editors.

I will need to dig deeper into the backend to see what's available.

Link to comment
Share on other sites

JS is certainly an easy way to accomplish it for helping the editors. It's more the developers I'm worried about, coding in a manner that expects a field to always be populated, when it may not be technically possible for that to always be the case. But if it's just helping the editors that we need, then that's no problem: there are no technical challenges there.

Link to comment
Share on other sites

  • 3 months later...

I just built a module that adds a "addHookBefore('save' ...) Hook and the checks the inputs I want to be required. If the requirements aren't met, there's an error and I set $page->addStatus(Page::statusUnpublished); ...

This seems to work great so far. Is there anything I'm missing? (since this method isn't mentioned here ...)

Link to comment
Share on other sites

Sounds good to me. Though something to consider is that you may be unpublishing a live page from the site, which could be undesirable in many situations. Depending on the situation, it may be better to save the page and notify the editor of the error rather than fully unpublishing it.

Link to comment
Share on other sites

I figured we were talking about some condition other than required? But if that's the case, Soma is right that this is built in. However, it's not going to unpublish a page if you leave a required field empty. Though it won't publish an unpublished page that has a missing required field.

Link to comment
Share on other sites

Hm ... I remember trying it (that is "advanced", right?) but it didn't work for fieldtypes Pages and Image, which is what I needed. I'll try again, although on my sites unpublishing a page is better than publishing one without a teaser image, for instance. And as I said, my editors won't learn.

Thanks!

t

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

×
×
  • Create New...