I would like to provide validation for title length. R开发者_StackOverflowight now I get an exception:
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).
I tried http://drupal.org/project/maxlength and http://drupal.org/project/maxlength_js, but no luck.
I coded custom validation module
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');
}
}
The statistics module just takes the page title and attempts to directly save it in the database, however the database column has a maximum size of 255 characters. It looks like the particular page you are viewing has a title longer than 255 characters.
You should add an implementation of hook_exit to a custom module to trim the page title before the statistics module attempts to insert it into the database.
/**
* Implements hook_exit().
*/
function mymodule_exit() {
$title = drupal_get_title();
drupal_set_title(substr($title, 0, 255), PASS_THROUGH);
}
Just be sure to set up your module so that this hook runs before the statistics module's hook.
Let me know if you need more info.
You can use this function to change the maximum allowed length of the title for a certain node type:
function mymodule_form_mynodetype_node_form_alter(&$form, &$form_state) {
$form['title']['#maxlength'] = 34;
}
Replace mymodule with your module name and mynodetype with your node type id
This way, Drupal does the validation and provides useful (translatable) error messages.
A JS approach would be to put something like:
<script>
window.onload = function() {
document.getElementById("edit-title").setAttribute("maxlength","60");
}
</script>
...in a header file or page.tpl.php or placement which exists when the form is generated.
The onload
would mean that the function gets activated only after the page is fully loaded and the function basically sets/replaces the maxlength of the title field with a number of your choice.
Be sure to replace edit-title
with whatever id
your title field has. In the case that it doesn't have an id
, you can consider using other getElement
methods.
Clearly, this validation only works when a user is filling in the title on a form.
Its quite simple you can use MaxLength Js and set the character limit in the "Content type" edit page and change it when ever you feel like it
Link to an example
精彩评论