Tag Archive for drupal form validation

Custom validation on existing Drupal form

Share

validationCustom validation on existing Drupal form

Hello folks, How are you doing? Thank you for your comments and queries on other topics I have written. There were queries on custom validation on existing Drupal form. So here is the post for you 🙂

Below is how we add custom validation on existing Drupal form. for example we want to add form validation to user login form, as this form is in Drupal’s core module we are not supposed to hack that module. So we need to create a new custom module say for example my_module. (I am assuming you know Drupal module development or comment below if you want me to write a blog on Drupal module development :))

Ok, so we have a module named my_module. Now we need to implement hook_form_alter to add a custom validation function to validate user login form, below is code snippet to do that.

Above code will look for the function validate_this_form() in your module when the form the user form is submitted.

So, lets implement that function and restrict some login names (Just to know how it works). Lets add if condition in function to check user name and set error message accordingly.

Above function will be executed and set validation error message for the form if username used is ‘sachin’

Below is how the error message gets added through our above function where username ‘sachin’ is checked.

custom validation on existing Drupal form

Please feel free to ask queries in comments or contact me using contact form on this site. I will be happy to help you out. Thank you for reading this post and see you again later with new post. 🙂

Or, please login using google or facebook and create a forum topic to get your query answered in few hours here http://www.learn-drupal.in/forums/forum/drupal-forum-post-installation

Thank you,
Sachin

Sharing is sexy...Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn
329 views