Hello Everyone,

I just spent the better part of a day figuring out how to validate a NODE ID when it has been entered into a form in the Administrative GUI.

Some things I ran across that tripped me up:

  • Form data is treated like a string
  • MariaDB weakly compares strings ("1" and "1abcd" both match NODE ID "1")
  • I had to use is_numeric() to screen out alphanumeric text
  • I had to use intval() to make sure zero and negative NODE IDs could not be supplied
  • I had to use strlen() to make sure floats could not be supplied
  • I had to use $result->rowCount() to make sure a NODE ID already existed in the database
  • I had to use #required as a form attribute to make sure the value couldn't be skipped

Phew!  Hopefully the above tips will help future module developers save time.

Did I miss anything?

g.
----

Comments

Hi. Why do you need to validate node IDs? Node IDs are never entered through the UI. They are assigned to the node when saved, by the database server. There is no administrative UI that asks the user to enter a node ID (except, perhaps, in a View filter, where you can enter the id of an existing node to filter the view). Am I misunderstanding what you are trying to do? 

Hello @argiepiano,

I made a technical observation.  Did you respond with a policy observation?

For a fruitful discussion to occur, we need to be in the same continuum.

NODE ID is unique in the system.  If you want to absolutely identify something for certain, NODE ID looks like a pretty good candidate.  Other methods can prove to be "problematic" as per the discussion at:

StackOverflow

Still, if you need a use case that conforms to the legitimate use of a NODE ID outside the context of programming code, I offer the following example:

https://www.drupal.org/node/201848

Cheers,

g.
----