Validating node creation / edit form

I’m using form_alter to play with the node form for a specific content type. This content type has many different fields and I’m also using Field Collection module to organize all these fields & logic, if that makes any difference.

Apparently, there are some incompatibilities between the Field Collection & Field Dependencies modules so I thought I might be able to build a similar field dependency functionality with my own module.

I’ve created a validation function for the form. My problem is that I’m trying to access $form_state values that users insert in the form in order to validate these values but I think I’m doing something totally wrong here:

function _mymodule_my_validation_function($form, &$form_state){     $short_field_name = $form_state['values']['long_field_name'];     if (empty($short_field_name) || $$short_field_name == '') {         form_set_error('error','That field cannot be empty!');     } } 

Unfortunately this validation script doesn’t work. To be more exact, I

dpm($form_state); 

in my form_alter and $form_state doesn’t even contain ‘values’ (shouldn’t it be present but empty?).

Is there a different best practice for validation node form fields instead of dealing with the $form_state[‘values’] array?

Also, I need to be able to see the values submitted by users each time the node form is submitted. Currently, I dpm($form) in my form_alter function and everything is returned on the node/%/edit page as expected. But how can I catch submitted values on the node/% page after submitting the node form??

Update: When I call dpm($form_state) within my validation function I get what I need if the form doesn’t validate (as we still are on the same node edit page). But how can I receive the (new/changed) values that users entered in the form fields in case the form is submitted successfully and user is directed to content list page?

This article was republished from its original source.
Call Us: 1(800)730-2416

Pixeldust is a 20-year-old web development agency specializing in Drupal and WordPress and working with clients all over the country. With our best in class capabilities, we work with small businesses and fortune 500 companies alike. Give us a call at 1(800)730-2416 and let’s talk about your project.

FREE Drupal SEO Audit

Test your site below to see which issues need to be fixed. We will fix them and optimize your Drupal site 100% for Google and Bing. (Allow 30-60 seconds to gather data.)

Powered by

Validating node creation / edit form

On-Site Drupal SEO Master Setup

We make sure your site is 100% optimized (and stays that way) for the best SEO results.

With Pixeldust On-site (or On-page) SEO we make changes to your site’s structure and performance to make it easier for search engines to see and understand your site’s content. Search engines use algorithms to rank sites by degrees of relevance. Our on-site optimization ensures your site is configured to provide information in a way that meets Google and Bing standards for optimal indexing.

This service includes:

  • Pathauto install and configuration for SEO-friendly URLs.
  • Meta Tags install and configuration with dynamic tokens for meta titles and descriptions for all content types.
  • Install and fix all issues on the SEO checklist module.
  • Install and configure XML sitemap module and submit sitemaps.
  • Install and configure Google Analytics Module.
  • Install and configure Yoast.
  • Install and configure the Advanced Aggregation module to improve performance by minifying and merging CSS and JS.
  • Install and configure Schema.org Metatag.
  • Configure robots.txt.
  • Google Search Console setup snd configuration.
  • Find & Fix H1 tags.
  • Find and fix duplicate/missing meta descriptions.
  • Find and fix duplicate title tags.
  • Improve title, meta tags, and site descriptions.
  • Optimize images for better search engine optimization. Automate where possible.
  • Find and fix the missing alt and title tag for all images. Automate where possible.
  • The project takes 1 week to complete.