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?
Any reason why the "required" attribute isn't working in the "attributes" form?
|
|
Just wanted to bump this to see if anyone had any input on how to make certain attributes required. Thanks!
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.
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!
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()?
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;
}
}
}
}
?>You're the man.
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.)
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!
|
|






Joined: 09/19/2007