# States: Strong improvement for developing the Drupal 7 user interface


At one of the recent Drupal Camps (Donetsk, I guess, it was) someone mentioned #states. As I personally had never come across one, only heard of them, I decided to dig into the issue and share my observwtions with you (as this topis will be useful for every Drupal developer, I guess).
So, what kind of feature is it?
# States keeps adding JavaScript that prompts changing the state of one element based on the state of another, after a user action's occurred.
What does it do?
Originally, #states was meant to prompt creating dynamic forms. A simple example: here we have a form with radio buttons "Job Search" and "Employer", while different values are being chosen, the user is displayed various elements of the form for filling.
What exactly can be done using the #states?
To cut a long story short, let's follow the demo-link:

Everything you see here can be done without a single line of JavaScript, only the php, only the Drupal Form API.

Thus, let's consider a basic example:
'# States' => array (
STATE1, STATE2 - a condition that must be applied to the element
CONDITIONS_ARRAY1, CONDITIONS_ARRAY2 - sets of conditions under which a property should be used.

Sets of conditions are specified in an array mode whereby the key being the Jquery selector for any other element on the page (not obligatory restricted to the current form), and the value being a set (as well as a php array) of properties of this element, given the coincidence between the above we need to apply to the initial state of the element STATE1, namely:

‘#states’ => array(

  STATE1 => array(

     JQUERY_SELECTOR1 => array(




     JQUERY_SELECTOR2 => array(





 STATE2 => array.......


The next question that I needed to find the answer for was about the types of properties that were supported?
List of states (STATE1, STATE2 in the example above) that can be used for the element:

  • disabled
  • required
  • optional
  • visible
  • invisible
  • checked
  • unchecked
  • expanded
  • collapsed

Here is the list of conditions (REMOTE_CONDITION1, REMOTE_CONDITION2 in the example above) which can be used for the items that are specified with use of Jquery selectors:

  • filled
  • checked
  • unchecked
  • expanded
  • collapsed
  • value

The following list of states can be used for both types of elements (and STATE1, and REMOTE_CONDITION1), but according to the documentation, they have not yet been fully realized, and their utilization won't bring about any changes to the elements:


  • irrelevant
  • valid
  • invalid
  • touched
  • untouched
  • readwrite
  • readonly

And you are welcome: here's some more 'real world' example of usage  (the code for this demo form)

A short note

At the moment there is no possibility to use a set of conditions (REMOTE_CONDITION1, REMOTE_CONDITION2 ...), method OR. If you have such need, you can use AJAX Forms.
That kind of the implementation of the #states is completely tied to JavaScript, it should be understood that it bears no server-side logic, only an interface for the end users. And it wouldn’t work  with a disabled JavaScript in your browser. It means you have to do something to make sure that all functional works correctly without JavaScript, as it is.

Useful links


5 votes, Rating: 5

Read also


XML - RPC - simple remote procedure call protocol.  XML- RPC - is the father of one of  the most popular protocol SOAP....


The following article is giving a description of Titanium Appcelerator platform opportunities....


In theprevious article we shared about creating install profiles for Drupal 6. In this article it'll be described how to create...


In Drupal 7 API there is a chapter dedicated to site theming. All elements of site, without exclusion, should go through the theming process. But first of all theming itself should be set up.


In this post the bases of creating environment for Drupal projects development on the basis of Debian 6 "Squeeze" will be described. Having this, everyone will be able to practice setting OS...

Need a quote? Let's discuss the project

Are you looking for someone to help you with your Drupal Web Development needs? Let’s get in touch and discuss the requirements of your project. We would love to hear from you.

Join the people who have already subscribed!

Want to be aware of important and interesting things happening? We will inform you about new blog posts on Drupal development, design, QA testing and more, as well news about Drupal events.

No charge. Unsubscribe anytime