I have a form to get some user input in Drupal. I want to add some prefix to the user input.
For example, the user enters "Laptop"; I want to save it in the database as resource_Laptop. I want to add the prefix before the validation is done, to avoid it fails.
How do I do this? Will hook_form_alter()
help me here?
I have used this method, but not satisfied with this hack.
function mymodule_form_alter(){
drupal_add_js("
(function ($) {
$(document).ready(function() {
$('.field-suffix').hide();
$('#edit-submit').click(function(){
var s = $('#edit-machine-name').val();
if (s.indexOf('_resources') == −1) {
$('#edit-machine-name').val($('#edit-machine-name').val() + '_resources');
}
});
})(jQuery);", 'inline');
}
resource_
prepended or not? It should still be possible to write a validation for that. Best place to add this, and alter input is in a submit handler. Also don't use#edit-machine-name
as this can become#edit-machine-name--2
or#edit-machine-name--3
. Use a class selector, or add a class through that hook_form_alter