Any reason why the "required" attribute isn't working in the "attributes" form?

Posts: 76
Joined: 09/19/2007
Bug FinderGetting busy with the Ubercode.

I'm accessing the forms API to change the attributes of various inputs in the attributes section of the 'add to cart' form. I can change a number of attributes to, say, make an input a textarea, but when I add the '#required' attribute, it doesn't seem to have an effect. Any reason why this might be?

Posts: 76
Joined: 09/19/2007
Bug FinderGetting busy with the Ubercode.

Just wanted to bump this to see if anyone had any input on how to make certain attributes required. Thanks!

Posts: 5269
Joined: 08/07/2007
AdministratorHead Code Monkey - I eat bugs.

Seems like it should work just fine... you're doing a '#required' => TRUE and it's not persisting? Maybe you could post your function and even some debug info from where the form is rendered.

Posts: 76
Joined: 09/19/2007
Bug FinderGetting busy with the Ubercode.

Hi Ryan,

I've set up a function called phptemplate_uc_product_add_to_cart_form() which themes the add_to_cart form. In it, I change some text boxes to textareas, and also add the #required attribute by doing the following:

$form['attributes']['1']['#required'] = TRUE;

Is the theme function not the place to put this? Would would be a good way to display debug info for the form?

Thanks!

Posts: 76
Joined: 09/19/2007
Bug FinderGetting busy with the Ubercode.

Okay, it looks like I probably need to use the hook_form_alter() in this case. However, when I do a var_dump($form), I don't see any of the attributes in there. I'm guessing that another hook is adding those in? If so, is there a way to edit them via hook_form_alter()?

Posts: 76
Joined: 09/19/2007
Bug FinderGetting busy with the Ubercode.

Ah, I figured it out:

First, you need to change the weight of whatever module is doing the altering to a high number. Mine is a custom module, so I set it to 5000. This will ensure that you alter the form after UC has it's way with it.

Next, you use the hook_form_alter() to iterate through the attributes and add in the #required parameter, as in the following:

<?php
function custom_form_alter($form_id, &$form) {
   
// Check if it's an 'add product' form
   
if (strpos($form_id,'uc_product_add_to_cart_form') !== FALSE) {
       
// Iterate through all of the attributes
       
foreach ($form["attributes"] as $key=>$value) {
           
// Attributes will have a numeric key. If you need to chose a
            // specific attribute, you could match the ["#title"] parameter.
           
if (is_numeric($key)) {
               
// I'm requiring that all the attributes be filled in
               
$form["attributes"][$key]['#required'] = TRUE;
            }
        }
    }
}
?>

Posts: 5269
Joined: 08/07/2007
AdministratorHead Code Monkey - I eat bugs.

You're the man. Cool Thanks for posting this up for others to learn from.

(And glad you were able to figure it out. All the required stuff has to happen before the form ever gets displayed so Drupal can treat it as such when it's processing forms. That's why just adding it in the template didn't work.)

Posts: 163
Joined: 08/07/2007
Uber DonorBug FinderEarly adopter... addicted to alphas.Getting busy with the Ubercode.Not Kulvik

I'm bumping this to make it easier for me to find again. Ditto to what Ryan said about posting this. I want my add to cart to have image submit buttons and an attribute that uses radio buttons instead of drop down list. Early on I hacked core to make that happen but that solution has long ago been clobbered.

Thanks again!