Jojodae Ganesh Sivaji
January 31, 2012
Every project is unique and has something new to learn. This fact was realized again in my recent work. As the title says, the task was as simple as adding AJAX validation to a specific field (not for the entire form). While this looked pretty easy at first glance but had to perceive and apply a little crux of Form and Field API.
The field in which I had to execute this task comes from Link module. Essentially I need to check for duplicate URLs. If the URL already exists in another node, I need to show an error message in on the blue event. Like most other CCK field modules, it does implement its own Form API #type link_field that can take URL and title.
My first endeavour was to implement a hook_form_alter() in a custom module as to inject #ajax property to $form['field_link']. But this trick didn't work as expected. The reason, the #ajax property does not work with the custom type provided by the Link module. The next essay was to try the same trick but in #after_build function. Again it turned out a futile attempt. After spending a couple of hours I realized that #after_build was too late to inject #ajax property.
Later, I learned from Tushar (founder @unfollowersme) the #process property where custom form API types are expanded to primitive types. In my case link_field to textfield. Adding #ajax to the expanded url textfield field did the trick.
The minimal version of code that worked for me is below,
Just like how your fellow techies do.
We'd love to talk about how we can work together
Take control of your AWS cloud costs that enables you to grow!