Drupal 7 Form API - Using #states on #markup field
An essential feature of a dynamic site would be to let its users add some contents to it. Often this is done using HTML forms. Drupal offers a rich and relatively simple way to use API for building forms for its Developers. In Drupal parlance it is referred as Form API (or FAPI).
The beauty of this is, developers can create the forms with PHP itself. Essentially using array keys and values (thanks to Drupal coding standards for offering a more readable structure). That is developers don't have to mess with both HTML (for creating / structuring) and PHP (for validation / response handling). With just PHP code we can get the job done.
In one of my recent works I had to show a help text (#markup text) as a part of sign up form based on value chosen for plan field.
It was noticed that #states is having no effect on #markup field. The code I thought would work is below,
Essentially I need to show a specific html text block based on value chosen for plan field. Note the #states in second field. It just didn't work the way it was expected. Looks like a limitation in Form API and core developers might have done this for some obvious reasons (could be because there is no HTML id attribute for markup field).
To workaround this case, a special wrapper field type is there in Form API. Referred by the name 'container'.
Actually this was explained in Form API page comment with a sample code. Thanks to becw for the same. Accidentally searching for '#markup' helped me locate this tip. Source https://api.drupal.org/comment/11049#comment-11049
After updating the code to use 'container' element, it worked as expected. The revised code is below,
I hope this would help others as well.