Yes, I have tried the same code as a separate module as well. But the
behaviour was identical. In D7, I believe, you can use _form_alter in
the template.php but anyway it does not seem to make any difference.
Thanasis
On 21/02/12 12:32, Todd wrote:
What are you using for the name space for your functions?
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).
You'll also need a .info file.
So, for instance, your directory structure in /sites/all/modules will
look like this, assuming that the module name is "velios" (your last name).
velios [directory]
+ velios.info <http://velios.info> [See here for info:
http://drupal.org/node/542202]
+ velios.module [contains the code below, using the namespace "velios"
for your hooks. Ie, "velios_form_alter()"]
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.
Regards,
Todd
On 20 February 2012 12:09, Athanasios Velios <[email protected]
<mailto:[email protected]>> wrote:
Sorry, I will have to accept your offer for editing as I am clearly
missing something obvious:
function MYTHEME_form_alter(&$form, $form_state, $form_id) {
global $user;
//alter forms but not for admin account
if ($user->uid != 1){ //limit it for some users, let's say admin only
//various irrelevant alterations here
} else {
if ($form_id == 'uc_cart_view_form') {
//add extra registration logic
$form['#validate'] = array('congr_afternoon___bookings');
}
}
}
/**
* Extra logic
*/
function congr_afternoon_bookings(&$__form, &$form_state){
//check afternoon tours for a max of 2 bookings
$congr_highlight_afternoon___fields = array();
$congr_max_afternoon = 0;
foreach($form['items'] as $key=>&$val) {
if (isset($val['nid']['#value'])) {
$node = node_load($val['nid']['#value'__]); //load the node of
the corresponding item in the form
if ($node->field_product_type['__und'][0]['tid'] == 476){ //if
the associated term tid is 476
$congr_max_afternoon = $congr_max_afternoon +
$val['qty']['#default_value']; //add the quantity field of the item
list to our max value
$congr_highlight_afternoon___fields[] = $key; //also keep
the key in an array
}
}
}
if ($congr_max_afternoon > 2) { //if the max number of quantity
items is more than 2
//$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...
foreach($congr_highlight___afternoon_fields as $key=>$val) {
//for each item in the list
if ($key == 0) { //if it is the first one, show the full
message which is independent of a specific quantity field
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.');
form_set_error('items]['.$val,__'Please amend this item:
'.$form['items'][$val]['title'__]['#markup'].'.');
} else { //for the rest do not show the message only highlight
form_set_error('items]['.$val,__'Please amend this item:
'.$form['items'][$val]['title'__]['#markup'].'.');
}
}
}
}
Thanks for looking into this.
Thanasis
On 20/02/12 16:02, Mukesh Agarwal wrote:
http://api.drupal.org/api/__drupal/modules%21system%__21system.api.php/function/__hook_form_alter/7
<http://api.drupal.org/api/drupal/modules%21system%21system.api.php/function/hook_form_alter/7>
-- the hook takes $form as an argument passed by reference.. so
all the
changes you make in $form, it should get reflected in the UI..
Please
share your code if it does not work for you..
On Mon, Feb 20, 2012 at 8:57 PM, Athanasios Velios
<[email protected] <mailto:[email protected]>
<mailto:[email protected] <mailto:[email protected]>>> wrote:
Hm, I commented the lines in template.php and built a new module
with the hook_form_alter and additional logic but this does
not make
any difference at all. Exactly the same behaviour...
Thanasis
On 20/02/12 09:25, Mukesh Agarwal wrote:
Yes, Drupal is quite structured and has defined ways to
deal with
stuff.. template.php and hook_form_alter are for 2 different
purposes
and in your case writing a custom module which
implements the
hook is
the right way.
On Mon, Feb 20, 2012 at 2:53 PM, Athanasios Velios
<[email protected] <mailto:[email protected]>
<mailto:[email protected] <mailto:[email protected]>>
<mailto:[email protected] <mailto:[email protected]>
<mailto:[email protected] <mailto:[email protected]>>>> wrote:
Do you reckon that the problem is putting the code in
template.php
instead of a custom module?
Thanks again,
Thanasis
On 20/02/12 08:27, Mukesh Agarwal wrote:
The place to implement logic based
form_set_error should
be in the
validate function and not template.php .. I would
recommend you use
hook_form_alter
<http://api.drupal.org/api/______drupal/modules%21system%______21system.api.php/function/______hook_form_alter/7
<http://api.drupal.org/api/____drupal/modules%21system%____21system.api.php/function/____hook_form_alter/7>
<http://api.drupal.org/api/____drupal/modules%21system%____21system.api.php/function/____hook_form_alter/7
<http://api.drupal.org/api/__drupal/modules%21system%__21system.api.php/function/__hook_form_alter/7>>
<http://api.drupal.org/api/____drupal/modules%21system%____21system.api.php/function/____hook_form_alter/7
<http://api.drupal.org/api/__drupal/modules%21system%__21system.api.php/function/__hook_form_alter/7>
<http://api.drupal.org/api/__drupal/modules%21system%__21system.api.php/function/__hook_form_alter/7
<http://api.drupal.org/api/drupal/modules%21system%21system.api.php/function/hook_form_alter/7>>>>
-> $form_id will indicate the form you wish to
alter,
and there
you can
add a custom validate function in
$form['#validate'] --
the custom
function should then take care of form_set_error
http://api.drupal.org/api/______drupal/includes%21form.inc/______function/form_set_error/7
<http://api.drupal.org/api/____drupal/includes%21form.inc/____function/form_set_error/7>
<http://api.drupal.org/api/____drupal/includes%21form.inc/____function/form_set_error/7
<http://api.drupal.org/api/__drupal/includes%21form.inc/__function/form_set_error/7>>
<http://api.drupal.org/api/____drupal/includes%21form.inc/____function/form_set_error/7
<http://api.drupal.org/api/__drupal/includes%21form.inc/__function/form_set_error/7>
<http://api.drupal.org/api/__drupal/includes%21form.inc/__function/form_set_error/7
<http://api.drupal.org/api/drupal/includes%21form.inc/function/form_set_error/7>>>
-- you need to set the element name as first
parameter
to make
sure that
the particular field is highlighted.
On Mon, Feb 20, 2012 at 1:51 PM, Athanasios Velios
<[email protected] <mailto:[email protected]>
<mailto:[email protected] <mailto:[email protected]>>
<mailto:[email protected] <mailto:[email protected]>
<mailto:[email protected] <mailto:[email protected]>>>
<mailto:[email protected] <mailto:[email protected]>
<mailto:[email protected] <mailto:[email protected]>>
<mailto:[email protected] <mailto:[email protected]>
<mailto:[email protected] <mailto:[email protected]>>>>> wrote:
Hello,
I am trying to alter a form
(uc_cart_view_form) with
various
input
fields (Qty for products). In template.php I am
adding some
extra
logic which applies across a selection of these
fields. Once the
logic is checked I need to display an error
message.
I am using
form_set_error to show the message. The
problem is
that the user
then updates the input values and updates
the form,
but the new
figures are not taken into account. They are
shown
in the
form, but
not used in my template.php extra logic.
drupal_set_message allows the new values to
work but
the message
appears twice and it does not, obviously,
link to
specific
fields in
the form which are problematic.
Setting form_state rebuild to true before
form_set_error
does not
have an effect.
Any ideas of what needs to be done for these
values
to be
taken into
account with form_set_error?
Thanks
Thanasis
--
Cheers,
Mukesh Agarwal
______________________________________
Innoraft Solutions <http://www.innoraft.com> || +91
8017220799 <tel:8017220799>
--
Cheers,
Mukesh Agarwal
____________________________________
Innoraft Solutions <http://www.innoraft.com> || +91
8017220799 <tel:%2B91%208017220799>
--
Cheers,
Mukesh Agarwal
__________________________________
Innoraft Solutions <http://www.innoraft.com> || +91 8017220799
<tel:%2B91%208017220799>