作者:老葛,北京亚艾元软件有限责任公司,http://www.yaiyuan.com
/**
* Implements hook_schema().
*/
function field_validation_schema() {
$schema['field_validation_rule'] = array(
'description' => 'Stores rule definitions',
'export' => array(
'key' => 'name',
'key name' => 'Name',
'primary key' => 'ruleid',
'identifier' => 'rule',
'default hook' => 'default_field_validation_rule',
'api' => array(
'owner' => 'field_validation',
'api' => 'default_field_validation_rules',
'minimum_version' => 2,
'current_version' => 2,
),
),
'fields' => array(
'ruleid' => array(
'type' => 'serial',
'description' => 'Unique identifier of the validation rule',
'unsigned' => TRUE,
'not null' => TRUE,
),
'rulename' => array(
'type' => 'varchar',
'description' => 'Name of the validation rule',
'not null' => TRUE,
'default' => '',
'length' => 255,
),
'name' => array(
'type' => 'varchar',
'description' => 'Machine name of the validation rule',
'not null' => TRUE,
'default' => '',
'length' => 32,
),
'field_name' => array(
'type' => 'varchar',
'length' => 32,
'not null' => TRUE,
'default' => ''
),
'col' => array(
'type' => 'varchar',
'length' => 32,
'not null' => TRUE,
'default' => 'value'
),
'entity_type' => array(
'type' => 'varchar',
'length' => 32,
'not null' => TRUE,
'default' => ''
),
'bundle' => array(
'type' => 'varchar',
'length' => 128,
'not null' => TRUE,
'default' => ''
),
'validator' => array(
'type' => 'varchar',
'description' => 'The validator key',
'not null' => TRUE,
'default' => '',
'length' => 255,
),
'settings' => array(
'type' => 'text',
'size' => 'big',
'description' => 'Serialized settings for the validator to be used',
'serialize' => TRUE,
'object default' => array(),
),
'error_message' => array(
'type' => 'varchar',
'description' => 'Rule error message',
'not null' => FALSE,
'length' => 255,
),
),
'primary key' => array('ruleid'),
'indexes' => array(
'field_name_bundle' => array('field_name', 'entity_type', 'bundle'),
),
);
return $schema;
}
与1.x相比,这里将data字段改为了settings,并且使用了序列化的形式,这一点是从Views/Panels里面学来的,好处是我们可以在settings里面定义多个字段,比如最大最小值限制,我们可以使用两个参数min/max,分别定义;而不是使用一个data,让用户输入[min,max]这样的格式,这样我们还需要解析用户输入的数据。