I am using Node Reference w/Autocomplete and when something doesn't match from the source content (e.g., I type 'asd asdadasdsa') it does not give an error on submit even if the field is required; in both cases nothing is saved in the field.

Comments

ezra-g’s picture

Title: When you don't provide valid data ... » Nodereference should require a valid referenced node when the field is reqiured
Version: 5.x-1.x-dev » 5.x-1.6
Category: support » bug
Priority: Critical » Normal

I agree that when a nodereference field is required, that the node form should validate only when a valid node has been referenced. In my opinion, when a field is required, two things should be true:

1) The field should contain data

2) That data should be the appropriate type of data for the field. In the case of the integer field, only integers are accepted.

ezra-g’s picture

Status: Active » Needs review
StatusFileSize
new417 bytes

With this patch, nodereference performs a form_set_error when the nodereference field is required and no valid node has been referenced. The error message reads "Please reference a valid post." which might not be ideal for all use cases. Perhaps it would be better to make this this error message configurable on a per-field basis. At least it's run through the t() function.

zmove’s picture

The module should provide 2 options for the case a node reference don't match :

  • Display an error and don't validate the form (it's the patch way)
  • Save the no matched result as a simple string (there is no existing patch for that case)

I'm working on 3 module right now so I don't have time to create a patch for the second case, but it could be goot to find these option in nodereference core.

wrunt’s picture

StatusFileSize
new825 bytes

Using $error_field doesn't work when a valid node wasn't found. The above patch doesn't highlight the field in red when an invalid node title is referenced (at least not for me).

This patch works for me but may have problems when a field has multiple items.

wrunt’s picture

Actually, I think ezra-g's patch was right, I was just misreading it. My patch was for nodereference_field('validate', ...), whereas ezra-g's patch is for nodereference_widget('validate', ...). Doing it in the widget works better.

marcoBauli’s picture

same problem here, patch at #4 applies and works great, thanks ;)

gustav’s picture

StatusFileSize
new841 bytes

I feel that the user should be alerted when they have entered a non-existent title, no matter whether the field is required or not. This patch provides an error message for each autocomplete nodereference field that contains an incorrect title. It does that inside nodereference_widget() which, as wrunt pointed out, is the best place for this.

ezra-g’s picture

I agree with #7, but I thought wrunt said in #5 that nodereference_widget() was in fact the preferred function.

gustav’s picture

ezra-g, what you write agrees with what I wrote: the best place to set the message is in nodereference_widget.

ezra-g’s picture

Pardon me - I misread that.

nicolash’s picture

Out of interest...could someone briefly explain why it works better in the _widget hook than in the _field one?

I have a few custom CCK fields, all with some sort of validation and conditional display rules...but for the life of me I can't decide where to put them...there's plenty of places where it'll work...but it seems a bit arbitrary.

nico_’s picture

Cheers gustav,

thanks a lot for the patch. Works fine for me as well! I hope the patch will in one way or another be included in a future version of the module..

gustav’s picture

Status: Needs review » Reviewed & tested by the community

Hi Cynic, thanks for your feedback. In response to this I have changed the status to "reviewed and tested by the community". It is really a very simple patch.

yched’s picture

Status: Reviewed & tested by the community » Fixed

I fixed that in D6 a few days ago, and thus backported to D5 instead - only slightly different patch (we accept a match on the exact title).

Anonymous’s picture

Status: Fixed » Closed (fixed)

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

yched’s picture

Status: Closed (fixed) » Fixed

got fixed meanwhile in D5 and D6
(D5 is only in 5.x-1.x-dev for now)

Anonymous’s picture

Status: Fixed » Closed (fixed)

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