Custom Validation CCK Custom fields in Drupal

Custom Validation CCK Custom fields in Drupal

In drupal we can create custom validation for the custom fields created using the CCK module for

content types. We can create a validation wizard or field validation for each content type

seperatly. This requires two modules CCK and CCK_validation.

To add a validation field to a content type, go to administer > content >
content types, select the content type you want to add to, and click on the
‘add field’ tab. One of the field types available should be ‘Validation’, and it
should have one bullet point under it, labelled ‘Textarea PHP Code’. If you select
this, give your field a name, and submit the form, you will get to the
configuration page for your new computed field.

Suppose imagine a content type named “Book” and it has custom field “ISBN”. In the ‘Textarea PHP Code’ of the Validation field we can write a validation code like the one below.

if (!isbn_format($node->field_text[0][’value’])){
form_set_error(’field_text’,'The ISBN is not in a valid format.’);
}

function isbn_format($code)
{

if(!preg_match(”/[a-zA-Z]/”,substr($code,0,3),$match))
return false;
if(!is_numeric(substr($code,0,7)))
return false;
return true;
}

Leave a Reply


All material @ copyrighted by chrisranjana.com. If you want to link to this article you are welcome to do so. Unauthorized publication is strictly prohibited. This developer tutorial website contains articles by Php programmers , Software developers, Mysql programmers and asp c# programmers. This website also contains ajax tutorials and advanced mysql sql stored procedures and functions tutorials and sample codes.