Hello,

i'm working on a form, where students can apply for certain classes. They choose an option of a select field (using webform module). There is a limit for every class. When this limit is reached, i unset this option in mytheme_form_alter

unset($form['submitted']['class']['#options'][3]);

Everything seems to work fine, but i'm not sure about possible side effects. Does anybody see a problem with this solution? (or a better one?)

Thank you for any advice.
Best regards

Adriana

(the only problem i've seen ist, when users applying simultanously, there is an error, when choosing an option /class that has been filled in meantime, but that's ok, as it is not possible to apply anymore)

Comments

No major problems. The common

No major problems.

The common wisdom however is that business logic should be kept in modules and out of themes; technically, it works the same, but doing this in your theme may earn you a few raised eyebrows if you need to collaborate with someone or hand over the project some day.

As for handling errors when the form has become outdated - see adding custom validation to webforms.

Thanks. I'm not sure: You

Thanks.
I'm not sure: You would hide the options in the template (xy_webform.tpl.php) instead of using 'unset'?
Isn't there a better possibility to do this in the module itself instead at another place (template file)?

Best regards

Adriana

I tried to hide the option in the template and use validate for the case that someone uses an outdatet form, but when setting an error in the validate function
(form_set_error('', t('my error text'));)

It works so far, but there is a warning when clearing the 'webform'-table (/node/xy/webform-results/clear).

    user warning: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1 query: SELECT *FROM `webform_submitted_data` WHERE nid =27843 AND cid =1 AND DATA = in /srv/www/vhosts/x/sites/all/modules/x/x.module on line 111.
    warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /srv/www/vhosts/x/sites/all/modules/xx.module on line 113.

No, exactly the opposite - I

No, exactly the opposite - I would use unset(), but in mymodule_form_alter(), not in mytheme_form_alter(). (Perhaps I should write shorter sentences :)

Ok, so I did not completly

Ok, so I did not completly wrong (I have the unset in mymodule_form_alter).

What outdated forms is concerning.

How would you do the validating? I tried both, f.e. validating by number of times the option was chosen. It worked so far, but when clearing the table (webform->clear), there was this sql warning described before.

$result = db_query("SELECT *FROM `webform_submitted_data` WHERE nid =$nid AND cid =$cid AND DATA =$data");
$applications= mysql_num_rows($result);

if $applications>=$max_applications){
form_set_error('', t('my error text'));
}

Or validating with checking if the option is still set (isset). First not only my error was displayed, but also the system error, so I had two warnings. Second I couldn't clear the table at all (webform settings->clear).

if (isset($form['submitted']['gruppe']['#options'][$chosen_class])){
}
else{
form_set_error('', t('my error text'));
}