Custom PHP function

Last updated on
21 August 2023

NB! The Custom PHP function validator (in Field validation 7.x-1.x) has been replaced by the PHP code validator in version 7.x-2.x.

  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'];

  4. It is very simple.

Help improve this page

Page status: Not set

You can: