<div>What are you using for the name space for your functions? </div><div><br></div><div>Instead of using MYTHEME_form_alter, you should use {modulename}_form_alter (where "modulename" is anything, as long as it's not already being used by Drupal).</div>
<div><br></div><div>You'll also need a .info file.</div><div><br></div><div>So, for instance, your directory structure in /sites/all/modules will look like this, assuming that the module name is "velios" (your last name).</div>
<div><br></div><div>velios [directory]</div><div>+ <a href="http://velios.info">velios.info</a> [See here for info: <a href="http://drupal.org/node/542202">http://drupal.org/node/542202</a>]</div><div>+ velios.module [contains the code below, using the namespace "velios" for your hooks. Ie, "velios_form_alter()"]</div>
<div><br></div><div>Once you have all that added, you'll have to go to your modules page and enable it and it should alter the form properly.</div><div><br></div><div>Regards,</div><div>Todd</div><br><div class="gmail_quote">
On 20 February 2012 12:09, Athanasios Velios <span dir="ltr"><<a href="mailto:a.velios@gmail.com">a.velios@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Sorry, I will have to accept your offer for editing as I am clearly missing something obvious:<br>
<br>
function MYTHEME_form_alter(&$form, $form_state, $form_id) {<br>
global $user;<br>
//alter forms but not for admin account<br>
if ($user->uid != 1){ //limit it for some users, let's say admin only<br>
//various irrelevant alterations here<br>
} else {<br>
if ($form_id == 'uc_cart_view_form') {<br>
//add extra registration logic<br>
$form['#validate'] = array('congr_afternoon_<u></u>bookings');<br>
}<br>
}<br>
}<br>
<br>
/**<br>
* Extra logic<br>
*/<br>
function congr_afternoon_bookings(&$<u></u>form, &$form_state){<br>
//check afternoon tours for a max of 2 bookings<br>
$congr_highlight_afternoon_<u></u>fields = array();<br>
$congr_max_afternoon = 0;<br>
foreach($form['items'] as $key=>&$val) {<br>
if (isset($val['nid']['#value'])) {<br>
$node = node_load($val['nid']['#value'<u></u>]); //load the node of the corresponding item in the form<br>
if ($node->field_product_type['<u></u>und'][0]['tid'] == 476){ //if the associated term tid is 476<br>
$congr_max_afternoon = $congr_max_afternoon + $val['qty']['#default_value']; //add the quantity field of the item list to our max value<br>
$congr_highlight_afternoon_<u></u>fields[] = $key; //also keep the key in an array<br>
}<br>
}<br>
}<br>
if ($congr_max_afternoon > 2) { //if the max number of quantity items is more than 2<br>
//$form_state['rebuild'] = TRUE; //TODO: I am told this is required because otherwise the form is not updated with the newly user inputted figures but it does not work...<br>
foreach($congr_highlight_<u></u>afternoon_fields as $key=>$val) { //for each item in the list<br>
if ($key == 0) { //if it is the first one, show the full message which is independent of a specific quantity field<br>
form_set_error('','You have tried to buy '.$congr_max_afternoon.' items, but the maximum you can buy is 2. See below for a list of items which you can possibly amend.');<br>
form_set_error('items]['.$val,<u></u>'Please amend this item: '.$form['items'][$val]['title'<u></u>]['#markup'].'.');<br>
} else { //for the rest do not show the message only highlight<br>
form_set_error('items]['.$val,<u></u>'Please amend this item: '.$form['items'][$val]['title'<u></u>]['#markup'].'.');<br>
}<br>
}<br>
}<br>
}<br>
<br>
Thanks for looking into this.<br>
<br>
Thanasis<div class="im"><br>
<br>
On 20/02/12 16:02, Mukesh Agarwal wrote:<br>
</div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="im">
<a href="http://api.drupal.org/api/drupal/modules%21system%21system.api.php/function/hook_form_alter/7" target="_blank">http://api.drupal.org/api/<u></u>drupal/modules%21system%<u></u>21system.api.php/function/<u></u>hook_form_alter/7</a><br>
-- the hook takes $form as an argument passed by reference.. so all the<br>
changes you make in $form, it should get reflected in the UI.. Please<br>
share your code if it does not work for you..<br>
<br>
On Mon, Feb 20, 2012 at 8:57 PM, Athanasios Velios <<a href="mailto:a.velios@gmail.com" target="_blank">a.velios@gmail.com</a><br></div><div class="im">
<mailto:<a href="mailto:a.velios@gmail.com" target="_blank">a.velios@gmail.com</a>>> wrote:<br>
<br>
Hm, I commented the lines in template.php and built a new module<br>
with the hook_form_alter and additional logic but this does not make<br>
any difference at all. Exactly the same behaviour...<br>
<br>
Thanasis<br>
<br>
<br>
On 20/02/12 09:25, Mukesh Agarwal wrote:<br>
<br>
Yes, Drupal is quite structured and has defined ways to deal with<br>
stuff.. template.php and hook_form_alter are for 2 different<br>
purposes<br>
and in your case writing a custom module which implements the<br>
hook is<br>
the right way.<br>
<br>
On Mon, Feb 20, 2012 at 2:53 PM, Athanasios Velios<br>
<<a href="mailto:a.velios@gmail.com" target="_blank">a.velios@gmail.com</a> <mailto:<a href="mailto:a.velios@gmail.com" target="_blank">a.velios@gmail.com</a>><br></div><div class="im">
<mailto:<a href="mailto:a.velios@gmail.com" target="_blank">a.velios@gmail.com</a> <mailto:<a href="mailto:a.velios@gmail.com" target="_blank">a.velios@gmail.com</a>>>> wrote:<br>
<br></div><div class="im">
Do you reckon that the problem is putting the code in<br>
template.php<br>
instead of a custom module?<br>
<br>
Thanks again,<br>
<br>
Thanasis<br>
<br>
<br>
On 20/02/12 08:27, Mukesh Agarwal wrote:<br>
<br>
The place to implement logic based form_set_error should<br>
be in the<br>
validate function and not template.php .. I would<br>
recommend you use<br>
hook_form_alter<br></div>
<<a href="http://api.drupal.org/api/____drupal/modules%21system%____21system.api.php/function/____hook_form_alter/7" target="_blank">http://api.drupal.org/api/___<u></u>_drupal/modules%21system%____<u></u>21system.api.php/function/____<u></u>hook_form_alter/7</a><br>
<<a href="http://api.drupal.org/api/__drupal/modules%21system%__21system.api.php/function/__hook_form_alter/7" target="_blank">http://api.drupal.org/api/__<u></u>drupal/modules%21system%__<u></u>21system.api.php/function/__<u></u>hook_form_alter/7</a>><div class="im">
<br>
<br>
<<a href="http://api.drupal.org/api/__drupal/modules%21system%__21system.api.php/function/__hook_form_alter/7" target="_blank">http://api.drupal.org/api/__<u></u>drupal/modules%21system%__<u></u>21system.api.php/function/__<u></u>hook_form_alter/7</a><br>
<<a href="http://api.drupal.org/api/drupal/modules%21system%21system.api.php/function/hook_form_alter/7" target="_blank">http://api.drupal.org/api/<u></u>drupal/modules%21system%<u></u>21system.api.php/function/<u></u>hook_form_alter/7</a>>>><br>
<br>
-> $form_id will indicate the form you wish to alter,<br>
and there<br>
you can<br>
add a custom validate function in $form['#validate'] --<br>
the custom<br>
function should then take care of form_set_error<br>
<br></div>
<a href="http://api.drupal.org/api/____drupal/includes%21form.inc/____function/form_set_error/7" target="_blank">http://api.drupal.org/api/____<u></u>drupal/includes%21form.inc/___<u></u>_function/form_set_error/7</a><br>
<<a href="http://api.drupal.org/api/__drupal/includes%21form.inc/__function/form_set_error/7" target="_blank">http://api.drupal.org/api/__<u></u>drupal/includes%21form.inc/__<u></u>function/form_set_error/7</a>><div>
<div class="h5"><br>
<br>
<<a href="http://api.drupal.org/api/__drupal/includes%21form.inc/__function/form_set_error/7" target="_blank">http://api.drupal.org/api/__<u></u>drupal/includes%21form.inc/__<u></u>function/form_set_error/7</a><br>
<<a href="http://api.drupal.org/api/drupal/includes%21form.inc/function/form_set_error/7" target="_blank">http://api.drupal.org/api/<u></u>drupal/includes%21form.inc/<u></u>function/form_set_error/7</a>>><br>
-- you need to set the element name as first parameter<br>
to make<br>
sure that<br>
the particular field is highlighted.<br>
<br>
On Mon, Feb 20, 2012 at 1:51 PM, Athanasios Velios<br>
<<a href="mailto:a.velios@gmail.com" target="_blank">a.velios@gmail.com</a> <mailto:<a href="mailto:a.velios@gmail.com" target="_blank">a.velios@gmail.com</a>><br>
<mailto:<a href="mailto:a.velios@gmail.com" target="_blank">a.velios@gmail.com</a> <mailto:<a href="mailto:a.velios@gmail.com" target="_blank">a.velios@gmail.com</a>>><br>
<mailto:<a href="mailto:a.velios@gmail.com" target="_blank">a.velios@gmail.com</a> <mailto:<a href="mailto:a.velios@gmail.com" target="_blank">a.velios@gmail.com</a>><br>
<mailto:<a href="mailto:a.velios@gmail.com" target="_blank">a.velios@gmail.com</a> <mailto:<a href="mailto:a.velios@gmail.com" target="_blank">a.velios@gmail.com</a>>>>> wrote:<br>
<br>
Hello,<br>
<br>
I am trying to alter a form (uc_cart_view_form) with<br>
various<br>
input<br>
fields (Qty for products). In template.php I am<br>
adding some<br>
extra<br>
logic which applies across a selection of these<br>
fields. Once the<br>
logic is checked I need to display an error message.<br>
I am using<br>
form_set_error to show the message. The problem is<br>
that the user<br>
then updates the input values and updates the form,<br>
but the new<br>
figures are not taken into account. They are shown<br>
in the<br>
form, but<br>
not used in my template.php extra logic.<br>
<br>
drupal_set_message allows the new values to work but<br>
the message<br>
appears twice and it does not, obviously, link to<br>
specific<br>
fields in<br>
the form which are problematic.<br>
<br>
Setting form_state rebuild to true before form_set_error<br>
does not<br>
have an effect.<br>
<br>
Any ideas of what needs to be done for these values<br>
to be<br>
taken into<br>
account with form_set_error?<br>
<br>
Thanks<br>
<br>
Thanasis<br>
<br>
<br>
<br>
<br>
--<br>
Cheers,<br>
Mukesh Agarwal<br></div></div>
______________________________<u></u>______<div class="im"><br>
<br>
Innoraft Solutions <<a href="http://www.innoraft.com" target="_blank">http://www.innoraft.com</a>> || +91<br>
<a href="tel:8017220799" value="+18017220799" target="_blank">8017220799</a><br>
<br>
<br>
<br>
<br>
--<br>
Cheers,<br>
Mukesh Agarwal<br>
______________________________<u></u>____<br>
Innoraft Solutions <<a href="http://www.innoraft.com" target="_blank">http://www.innoraft.com</a>> || <a href="tel:%2B91%208017220799" value="+918017220799" target="_blank">+91 8017220799</a><br>
<br>
<br>
<br>
<br>
--<br>
Cheers,<br>
Mukesh Agarwal<br>
______________________________<u></u>__<br>
Innoraft Solutions <<a href="http://www.innoraft.com" target="_blank">http://www.innoraft.com</a>> || <a href="tel:%2B91%208017220799" value="+918017220799" target="_blank">+91 8017220799</a><br>
<br>
</div></blockquote>
</blockquote></div><br>