如何限制Drupal中的标题长度?
我想提供标题长度的验证。现在我得到一个例外:如何限制Drupal中的标题长度?,drupal,drupal-7,Drupal,Drupal 7,我想提供标题长度的验证。现在我得到一个例外: PDOException: SQLSTATE[22001]: String data, right truncated: 1406 Data too long for column 'title' at row 1: INSERT INTO {accesslog} (title, path, url, hostname, uid, sid, timer, timestamp) VALUES (...) in statistics_exit()
PDOException: SQLSTATE[22001]: String data, right truncated:
1406 Data too long for column 'title' at row 1:
INSERT INTO {accesslog} (title, path, url, hostname, uid, sid, timer, timestamp)
VALUES (...) in statistics_exit() (line 90 of
/opt/bitnami/apps/.../htdocs/modules/statistics/statistics.module).
我尝试了和,但没有成功。统计模块只是获取页面标题并尝试直接将其保存到数据库中,但是数据库列的最大大小为255个字符。看起来您正在查看的特定页面的标题长度超过255个字符 您应该将hook_exit的实现添加到自定义模块中,以便在统计模块尝试将页面标题插入数据库之前对其进行修剪
/**
* Implements hook_exit().
*/
function mymodule_exit() {
$title = drupal_get_title();
drupal_set_title(substr($title, 0, 255), PASS_THROUGH);
}
只需确保设置模块,使此钩子在统计模块的钩子之前运行
如果您需要更多信息,请告诉我。统计模块仅获取页面标题并尝试直接将其保存到数据库中,但数据库列的最大大小为255个字符。看起来您正在查看的特定页面的标题长度超过255个字符 您应该将hook_exit的实现添加到自定义模块中,以便在统计模块尝试将页面标题插入数据库之前对其进行修剪
/**
* Implements hook_exit().
*/
function mymodule_exit() {
$title = drupal_get_title();
drupal_set_title(substr($title, 0, 255), PASS_THROUGH);
}
只需确保设置模块,使此钩子在统计模块的钩子之前运行
如果您需要更多信息,请告诉我。我编码了自定义验证模块
function custom_validate_form_alter(&$form, &$form_state, $form_id) {
if ( $form_id =='user_register_form') {
$form['#validate'][] = '_custom_validate_form_alter_user_register_validate';
}
if ( $form_id =='user_profile_form') {
$form['#validate'][] = '_custom_validate_form_alter_user_profile_validate';
}
$types = array("your_content_type_node_form");
if(in_array($form_id, $types)){
$form['#validate'][] = '_custom_validate_form_alter_title_node_form_validate';
}
return $form;
}
function _custom_validate_form_alter_user_register_validate($form, &$form_state) {
if (strlen($form_state['values']['name'])>30){
form_set_error('name', 'Username must be less than 30 characters. Please correct username');
}
}
function _custom_validate_form_alter_user_profile_validate($form, &$form_state) {
if(!empty($form_state['values']['pass']) && strlen($form_state['values']['pass'])>30){
form_set_error('pass', 'Password must be less than 30 characters. Please correct password');
}
if(!empty($form_state['values']['pass']) && strlen($form_state['values']['pass'])<6){
form_set_error('pass', 'Password must be at least 6 characters long. Please correct password');
}
}
function _custom_validate_form_alter_title_node_form_validate($form, &$form_state) {
if (strlen($form_state['values']['title'])>200){
form_set_error('title', 'Title must be less than 200 characters. Please correct title');
}
}
function custom\u validate\u form\u alter(&$form,&$form\u state,$form\u id){
如果($form\u id=='user\u register\u form'){
$form['#validate'][]='_custom_validate_form_alter_user_register_validate';
}
如果($form\u id=='user\u profile\u form'){
$form['#validate'][='_custom_validate_form_alter_user_profile_validate';
}
$types=数组(“您的内容\类型\节点\表单”);
if(在数组中($form\u id,$types)){
$form['#validate'][]='_custom_validate_form_alter_title_node_form_validate';
}
返回$表格;
}
函数(自定义)(验证)(表单)(更改)(用户)(注册)(验证)($form,&$form)状态{
如果(strlen($form_state['values']['name'])>30){
表单设置错误('name','Username必须少于30个字符。请更正Username');
}
}
函数(自定义)(验证)(表单)(更改)(用户)(配置文件)(验证)($form,&$form){
如果(!empty($form_state['values']['pass'])和&strlen($form_state['values']['pass'])>30){
表单设置错误(“通过”,“密码必须少于30个字符。请更正密码”);
}
如果(!empty($form_state['values']['pass'])和&strlen($form_state['values']['pass'])200){
表单设置错误('title','title必须少于200个字符。请更正title');
}
}
我编码了自定义验证模块
function custom_validate_form_alter(&$form, &$form_state, $form_id) {
if ( $form_id =='user_register_form') {
$form['#validate'][] = '_custom_validate_form_alter_user_register_validate';
}
if ( $form_id =='user_profile_form') {
$form['#validate'][] = '_custom_validate_form_alter_user_profile_validate';
}
$types = array("your_content_type_node_form");
if(in_array($form_id, $types)){
$form['#validate'][] = '_custom_validate_form_alter_title_node_form_validate';
}
return $form;
}
function _custom_validate_form_alter_user_register_validate($form, &$form_state) {
if (strlen($form_state['values']['name'])>30){
form_set_error('name', 'Username must be less than 30 characters. Please correct username');
}
}
function _custom_validate_form_alter_user_profile_validate($form, &$form_state) {
if(!empty($form_state['values']['pass']) && strlen($form_state['values']['pass'])>30){
form_set_error('pass', 'Password must be less than 30 characters. Please correct password');
}
if(!empty($form_state['values']['pass']) && strlen($form_state['values']['pass'])<6){
form_set_error('pass', 'Password must be at least 6 characters long. Please correct password');
}
}
function _custom_validate_form_alter_title_node_form_validate($form, &$form_state) {
if (strlen($form_state['values']['title'])>200){
form_set_error('title', 'Title must be less than 200 characters. Please correct title');
}
}
function custom\u validate\u form\u alter(&$form,&$form\u state,$form\u id){
如果($form\u id=='user\u register\u form'){
$form['#validate'][]='_custom_validate_form_alter_user_register_validate';
}
如果($form\u id=='user\u profile\u form'){
$form['#validate'][='_custom_validate_form_alter_user_profile_validate';
}
$types=数组(“您的内容\类型\节点\表单”);
if(在数组中($form\u id,$types)){
$form['#validate'][]='_custom_validate_form_alter_title_node_form_validate';
}
返回$表格;
}
函数(自定义)(验证)(表单)(更改)(用户)(注册)(验证)($form,&$form)状态{
如果(strlen($form_state['values']['name'])>30){
表单设置错误('name','Username必须少于30个字符。请更正Username');
}
}
函数(自定义)(验证)(表单)(更改)(用户)(配置文件)(验证)($form,&$form){
如果(!empty($form_state['values']['pass'])和&strlen($form_state['values']['pass'])>30){
表单设置错误(“通过”,“密码必须少于30个字符。请更正密码”);
}
如果(!empty($form_state['values']['pass'])和&strlen($form_state['values']['pass'])200){
表单设置错误('title','title必须少于200个字符。请更正title');
}
}
您可以使用此功能更改特定节点类型标题的最大允许长度:
function mymodule_form_mynodetype_node_form_alter(&$form, &$form_state) {
$form['title']['#maxlength'] = 34;
}
用模块名替换mymodule,用节点类型id替换mynodetype
通过这种方式,Drupal进行验证并提供有用的(可翻译的)错误消息。您可以使用此函数更改特定节点类型标题的最大允许长度:
function mymodule_form_mynodetype_node_form_alter(&$form, &$form_state) {
$form['title']['#maxlength'] = 34;
}
用模块名替换mymodule,用节点类型id替换mynodetype
通过这种方式,Drupal进行验证并提供有用的(可翻译的)错误消息
<script>
window.onload = function() {
document.getElementById("edit-title").setAttribute("maxlength","60");
}
</script>
window.onload=函数(){
document.getElementById(“编辑标题”).setAttribute(“maxlength”、“60”);
}
…在头文件或page.tpl.php或生成表单时存在的位置中
onload
意味着该函数只有在页面完全加载后才被激活,并且该函数基本上用您选择的数字设置/替换标题字段的maxlength
确保用标题字段中的id
替换edit title
。在没有<代码> ID <代码>的情况下,您可以考虑使用其他<代码> GETEngult方法。< /P>
显然,只有当用户在表单上填写标题时,这种验证才有效。JS的方法是放置如下内容:
<script>
window.onload = function() {
document.getElementById("edit-title").setAttribute("maxlength","60");
}
</script>
window.onload=函数(){
document.getElementById(“编辑标题”).setAttribute(“maxlength”、“60”);
}
…在头文件或page.tpl.php或生成表单时存在的位置中
onload
意味着该函数只有在页面完全加载后才被激活,并且该函数基本上用您选择的数字设置/替换标题字段的maxlength
确保用标题字段中的id
替换edit title
。在没有<代码> ID <代码>的情况下,您可以考虑使用其他<代码> GETEngult方法。< /P>
显然,只有当用户在表单上填写标题时,这种验证才有效<