Community & Support

50$ for a simple validation script module

Hello all,

I need for my site a simple module that will validate user input for a certain content type:

1) All node titles must be unique
2) All node titles must be 9 numbers (no letters)
3)the site should display an error message and the problem if validation fails
4) this is a cck content type

That's it!

Tell me if you want to do it and how much!

Patchak

Comments

80$

80$ :P
____________________________________________________
Tj Holowaychuk

Vision Media
Free Photoshop Downloads
CSS Doc

____________________________________________________
Tj Holowaychuk

Vision Media - Victoria BC Web Design

Here you go

Here is a variation of something I posted elsewhere on the site. Create a folder under modules called "restrict" and add restrict.module and restrict.info to the folder. Edit restrict.module to use you content type and then enable the module.

restrict.info

name =Restricted titles
description = Applies restrictions to titles for selected content type

restrict.module

<?php
function restrict_nodeapi(&$node, $op) {

if ( $op == 'validate' ) {
// We only care about nodes of type 'desired type'
$type = 'story';  // Change this to the type you want to restrict titles on
if ( $node->type == $type ) {
  $sql = "SELECT n.title, n.nid FROM {node} n WHERE type = '%s' AND title = '%s'";
  $results = db_query($sql, $type, $node->title);
  $existing = db_fetch_object($results);
  // We get here on both inserts and updates
  // For updates we want to make sure the title does not match another one
  // So we make sure it is an insert ( ! $node->nid )
  // or for update that we have not just found the node being updated ( $exisiting->nid != $node->nid )
  if ( ( ! $node->nid ||  $existing->nid != $node->nid ) && $existing->title == $node->title ) {
    $link = l("existing $type", "node/$existing->nid");
    form_set_error('title', t('There is already a %type called %title, see ', array('%type' => $type, '%title' => $node->title)) . $link . '.');
  }
  if ( strlen($node->title) != 9 ) {
  form_set_error('title', t('Title must be exactly 9 digits'));
  }
  if ( ! is_numeric($node->title) ) {
  form_set_error('title', t('Title must only use the digits 0-9'));
  }
 
}
}
}

is_numeric will return true

is_numeric will return true for numbers with a decimal point also?

Thats correct

You could replace

is_numeric($node->title)

with
preg_match('/[0-9]{9,9}/', $node->tiitle)

Is_numeric will work

is_numeric(); should validate with decimals as well

____________________________________________________
Tj Holowaychuk

Vision Media
Free Photoshop Downloads
CSS Doc

____________________________________________________
Tj Holowaychuk

Vision Media - Victoria BC Web Design

It will work

but as panis pointed out is_numeric() allows for decimals so '123456.89' would be valid with is_numeric().

That was really nice of you

That was really nice of you Nevets. This would also work for any field you would want to be unique, correct? So, if you wanted to make sure the URL (field_example_url) was unique, you would just treat (field_example_url) as you treated the title in your snippet, and change the error message of course.

@-OP: Sorry to semi-hijack your thread

Correct

Though you need to be aware CCK fields are more complex and are normally arrays (you can have more than one of the field)

My bad

I thought he was looking to validate ANY number.
____________________________________________________
Tj Holowaychuk

Vision Media
Free Photoshop Downloads
CSS Doc

____________________________________________________
Tj Holowaychuk

Vision Media - Victoria BC Web Design

HI there, The code seems to

HI there,

The code seems to work pretty well, except that I have a blank page after I submit the node. Normally I use the Node Go to module to redirect the user to a confirmation page, so maybe this is interfering??

Note: after disabling the node go to module it still seems to do the same blank page bug, and also when I activate the module on the modules page it does the same thing (blank page). I doubt it's a memory issue cause If I disable that module and enable all core modules I never get the same blank page. Any ideas?

Patchak

Try disabling the module

The problem tends to stem from unexpected (and unwanted output). If you disable the module does the problem go away? If yes, do you have and characters before the opening <?php? If you re-enable the module, do a submit and then open another page (say admin) do you see any errors?

Hi Nevets unfortunately it

Hi Nevets unfortunately it seems that I can't see any error, and I don't have access to the logs on that client's machine. But yes, the error goes away when I disable the module and comes back if I re-enable the module. Maybe there are some slight syntax typos?

Patchak

Did you copy it exactly?

I actually tested the code before posting so I know the syntax is correct. Did you change anything?

Sorry nevets I didn't want

Sorry nevets I didn't want to offend you by saying that, it's just something I've been told in #drupal when I showed the code to some people.. whaterver..

I reinstalled the module and it seems I was not using the right .info file. SIte has been running for a couple of tests now without crashing so it seems that I solved the problem by re-copying the .info file, sorry for the trouble!

Patchak

Hey there Nevets, By

Hey there Nevets,

By curiosity, I as wondering how can I do the same thing but adding another content type in the 'verified content" list...

I tried the following :
if ( $node->type == 'paquet' OR $node->type == 'paquet_yverdon' ){

Without success...

I also tried using in_array but also did not work...

thanks for any advice/help!

Patchak

A couple of thoughts

It should work though I am not sure if 'OR' works or if it needs to be 'or' (I always use '||' for the or test). If that does not help right before the 'if' statement add

drupal_set_message("Checking against type: $node->type");

then try creating a saveing the new content type to make sure you have the machine name correct.

Great everything works

Great everything works now
Thanks again

In such a case, you need

In such a case, you need also to change the db_query to

$results = db_query($sql, $node->type, $node->title);

Otherwise it will not work.

---

http://www.openify.com - Drupal Services in Israel

---

http://www.openify.com - Drupal Services in Israel

A small bug

Great module!

A small bug:

In the line

if ( ( ! $node->nid ||  $existing->nid != $node->nid ) && $existing->title == $node->title ) {

there is no need to check again if $existing->title == $node->title. It was already checked in the sql SELECT query. What is needed to be checked is whether the query returned any match of existing node (with the same title and type), therefore this line should be changed to:

if ( ( ! $node->nid ||  $existing->nid != $node->nid ) && $existing ) {

---

http://www.openify.com - Drupal Services in Israel

---

http://www.openify.com - Drupal Services in Israel

Thank you for all the

Thank you for all the answers, I'll test all this this weekend and I'll kepp you posted on the results

We also used this for

We also used this for checking for duplicates on a custom content type and a cck field.

<?php
function restrict_nodeapi(&$node, $op) {

if ( $op == 'validate' ) {

// We only care about nodes of type 'desired type'
$type = 'customtype';  // Change this to the type you want to restrict titles on

if ( $node->type == $type ) {
  $sql = "SELECT n.field_name_value, n.nid FROM {content_type_customtype} n WHERE field_name_value = '%s'";
  $results = db_query($sql, $node->field_name[0]['value']);
  $existing = db_fetch_object($results);
  // We get here on both inserts and updates
  // For updates we want to make sure the title does not match another one
  // So we make sure it is an insert ( ! $node->nid )
  // or for update that we have not just found the node being updated ( $exisiting->nid != $node->nid )

  if ( ( ! $node->nid ||  $existing->nid != $node->nid ) && $existing->field_name_value == $node->field_name[0]['value'] ) {
    $link = l("existing $type", "node/$existing->nid");
    form_set_error('field_name', t('There is already a record with this, see ', array('%type' => $type, '%title' => $node->title)) . $link . '.');

  }

}
}
}

[Edit: Added <code> and </code> tags: nevets]

if ( ( ! $node->nid || 

if ( ( ! $node->nid ||  $existing->nid != $node->nid )
                       && $existing->field_name_value == $node->field_name[0]['value']
                        &&  $node->field_name[0]['value'] ) {

I think if $node->field_name[0]['value'] == null, we needn't validate.

one more time!

subscribe

subscribe

Validating url

Hi,

Could this code also be used to validate if the url (of links_weblink/links_package) is unique?
I am trying to get this working but until now without success?

How do I have to alter the code to get this working?
Thanks in advance for your reply!

greetings,
Martijn

titles must be 9 numbers (no

titles must be 9 numbers (no letters)??

We go the drupal way. How about you?
http://www.drupalway.com

Advertising helps build a successful ecosystem around Drupal.