Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3350902
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T01:50:37+00:00 2026-05-18T01:50:37+00:00

I want to validate the input of a form before saving it to database

  • 0

I want to validate the input of a form before saving it to database to prevent Sql Injection and XSS.

I don’t want to modify the code of the module directly so I’ve created a custom module to do this kind of stuff but I’m not exactly sure how to use the #validate element to call a validation function.

In my custom module, I have this function with this logic implemented:

function mymodule_form_alter(&$form, $form_state, $form_id) {

  switch ($form_id) {
    case 'myform':
      drupal_set_message('this message is printed :D');
      $form['#validate'] = array('my_validation_function');
    break;
  }        
}

function my_validation_function($form, &$form_state) {

  drupal_set_message('not printed :(');
  watchdog('not printed :___(', 'not printed :___(');

}

I don’t know exactly how it works but It seems that the second function is never called when I submit the form.

EDIT (SOLUTION):

I’ve finally found where the validation was placed in the module:

$form['mod_name']['submit']['#validate'][] = 'my_validation_function';

I thought that just adding form[‘#validate’] would work despite of the contents on $form variable, but seems that depending on the implementation of the module if you don’t place the #validate element where it is expected it is ignored.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T01:50:37+00:00Added an answer on May 18, 2026 at 1:50 am

    Be careful of replacing the #validate key as you will replace any other validators that have been added before you.

    Best to append your function to it;

    $form['#validate'][] = 'my_validation_function';
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to refactor this piece of code, it takes input from a form,
I want my errors to float above, left-justified, the input field that doesn't validate.
Say you have a web form with some fields that you want to validate
I'm trying to validate my inputs with jQuery before a form is sent. This
In order to validate a form like: <input type=text name=trueExplain /> <input type=text name=true1
Hi I want to validate a form using a button, but this button call
i have a form which i want to be validated with javascript before submitting.
I have this regular expression to help me validate a form input. var nrExp
I have form who validate user input, I use PHP, Javascript and AJAX. I
I want to validate the value a user enters in a text box, so

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.