Community Documentation

Custom PHP function

Last updated October 28, 2011. Created by g089h515r806 on October 28, 2011.
Log in to edit this page.

1,click the link "Custom PHP function" to add a validation rule using php.

2,on "Add validation rule" form,Enter following info:
Rule name: test Custom PHP
Column:value
Function name: mymodule_validate_myfield
Custom error message: test Custom PHP error message.

3,add mymodule_validate_myfield to one of your custom module file,such as mymodule.module,here is the example code:

function mymodule_validate_myfield($variables){
//drupal_set_message('123456');
  //a flag, TRUE means pass, FALSE means fail.
  $flag = TRUE;
//maybe what you need is the current value
  $value = $variables['value'];
//you could also access other variables as your need
//$item = $variables['item'];
//$delta = $variables['delta'];
//$items = $variables['items'];
//$rule = $variables['rule'];
//$langcode = $variables['langcode'];
//$entity = $variables['entity'];

//Here add you validate logic,for example
// $flag  = mymodule_validate_myfield_through_webservice($value);
if($value == 88){
  $flag = FALSE;

}
//return TRUE or FALSE
return $flag;
}

In this function, you should return TRUE,or FALSE. you could visit context variable through $variables, such as:
$value = $variables['value'];

It is very simple.

Page status

Needs updating

Log in to edit this page

About this page

Drupal version
Drupal 7.x
Level
Beginner, Intermediate, Advanced
Audience
Contributors, Programmers, Site administrators, Site users
Keywords
custom, field, php, validation

Site Building Guide

Drupal’s online documentation is © 2000-2013 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License. Comments on documentation pages are used to improve content and then deleted.