Currently, user can submit a node where the title is only whitespace (see, for example: http://drupal.org/node/113654 and http://drupal.org/node/113655).

I can imagine two possible solutions:

1) A required field cannot be just whitespace. This could be problematic in rare cases. It would require the addition of trim() to _form_validate() where required fields are validated.

2) A node title cannot be whitespace- a new check in node_validate().

The latter is more conservative, and thus where I'll start- patch attached (should apply 5.x or HEAD).

Comments

pwolanin’s picture

bump

pwolanin’s picture

StatusFileSize
new1.22 KB

updated patch for HEAD (previous one applied with offset).

This is pretty trivial- RTBC?

david strauss’s picture

Status: Needs review » Needs work

This patch needs to check whether the content type even has a title. hook_node_info() allows specification of a content type without a title. Also, because you're comparing using only a double-equals, PHP does a type conversion that makes anything empty return TRUE when compared against an empty string. Even if that's the intended comparison, you should use empty() instead (but after the check for whether the content type even has a title).

pwolanin’s picture

@David- yes should probably be ===, not ==

Will the return from node_get_types() indicate wether it has a title?

david strauss’s picture

I believe it's included in the results from node_get_types() because _node_types_build() retrieves all the columns from {node_type}. If you check that the content type has a title, just check for empty($node->title).

david strauss’s picture

But, I have a bit of a philosophical problem with this change. If we allow content types to not have any title, why should we force content types that allow a title to have a non-empty one? If I really want that for my content type, I can add '#required' => TRUE to the title field. This patch would also not indicate though the UI that the title field is always required.

How about moving this functionality to a contrib module that uses hook_form_alter to ensure that all notes with title fields require the title field?

pwolanin’s picture

ok, but it's bad when a blank forum title shows up in the recent block...

Obviously could be dealt with in contrib, but seems like something core should handle.

david strauss’s picture

Okay, how about this compromise: if the title field is present for a node and required for the content type's form, then trim it and check if it has substance. This would allow content type to optionally have a title field and optionally require it but produce more expected behavior from requiring it.

drewish’s picture

i agree with pwolanin. one of Drupal's bis assumptions is that nodes have a title (it's used as the text for links, messages, etc). we should make a good faith effort to enforce that. visit admin/content/types/X and you'll notice that while the body is optional the title is required.

drewish’s picture

that should have read "big assumptions".

david strauss’s picture

@drewish If this patch makes titles required, then it should also remove the option in hook_node_info() for content type providers to specify that their content types don't have titles. I'm just asking for consistency from the hooks to interface.

pwolanin’s picture

yes, it makes sense to check the 'has_title' property before throwing an error. I'm not sure what the use case is for no titles, but that's not a fight I want to pick.

david strauss’s picture

@pwolanin I think you misinterpret my request. I think there are two options here:

* Allow content types to specify has_title and respect the choice.
* Remove has_title from all of Drupal and just require titles across the board.

drewish’s picture

i'm pretty sure all that has_title indicates is if the field should be provided on the node edit form. the node still has to have a title value.

david strauss’s picture

@drewish has_titles does not affect the node editing form. The title field is manually added by the content type provider in hook_form().

drewish’s picture

well, in core it does affect the insertion of the title controls. but in either case core makes a lot of assumptions that nodes have titles.

david strauss’s picture

@drewish So you're saying that the title field in the hook_form() example is redundant? I'm willing to accept that; I just assumed it wasn't.

drewish’s picture

sorry i wasn't clear. by core, i meant the core (node.module) defined content types. the has_title affects those, contrib modules can do what ever magic they like to generate node titles but each node really needs a title. i mean goto admin/content/node and try viewing a node without a title, there's nothing to click on. a module that creates nodes without setting a title value has a bug.

david strauss’s picture

@drewish So, what is the function of has_title in a content type provided by a module?

sun’s picture

Version: 6.x-dev » 7.x-dev
Status: Needs work » Fixed
StatusFileSize
new921 bytes
new1.9 KB

I guess I just wasted some time on this issue...

If a node type allows a title, then http://api.drupal.org/api/function/node_content_form/7 defines the title form element as #required. The Form API validation of #required fields is pretty extensive already, and done in http://api.drupal.org/api/function/_form_validate/7

Therefore, a node title is already validated to be non-empty (disregarding white-space), due to the #required flag.

There's only one possible edge-case, being, a custom node type defining $form['title'] on its own. However, I'd consider that as too edge-casey.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.