Hello
I'm trying, in my module 'cartscheduler', to add a validation test on 'delivery_postal_code', into Ubercart cart checkout. Pretty newbie in Drupal dev, I dont really understand what I should do...
My function doesn't work :

function cartscheduler_form_alter(&$form,&$form_state,$form_id) {
	if ($form_id == 'uc_cart_checkout_form') {
	  if ($form_state['panes']['delivery']['delivery_postal_code'] !=='93330') {
            form_set_error('delivery_postal_code','we dont deliver in your town');
	  }
	}
}

Maybe my form_set_error cannot find 'delivery_postal_code' as it is nested into another one. But how to specify it into the args of the function ?

Maybe It's possible to override a validate function but I dont know how to achieve this.
Any idea is welcome...

Comments

Web Assistant’s picture

I think you need to add your own validation function, rather than override:

<?php
function cartscheduler_form_alter(&$form,&$form_state,$form_id) {
  if ($form_id == 'uc_cart_checkout_form') {
    // add your own validation function
    $form['#validate'][] = 'cartscheduler_form_alter_validate';
  }
}

function cartscheduler_form_alter_validate($form, &$form_state) {
  if ($form_state['values']['delivery_postal_code'] !== '93330') {
    form_set_error('delivery_postal_code', t('we dont deliver in your town'));
  }
}
?>
artatum’s picture

oh, oh !! I'm going to check this out. Right now!
Thx a lot Web Assistant :-)

artatum’s picture

Great! It works! Thx again.
How can I display the red outline around the faulty field plz?

Web Assistant’s picture

Just use form_set_error(), make sure you've given the correct name of the form field.

artatum’s picture

I dont copy : as the message 'we dont deliver in your town' is correctly display on top of the page, so the test on
if ($form_state['values']['delivery_postal_code'] !== '93330') ...
was successfully performed...
Therefore, it seems that 'delivery_postal_code' in
form_set_error('delivery_postal_code',...
is correct ?
(red outline fails in IE and FF...)

Web Assistant’s picture

Do you have devel installed? I'd do a variable dump of $form_state['values'] in the validation function to make sure the name is correct:

<?php
dsm($form_state['values']);
?>
artatum’s picture

I'm working in Visual Studio and my spy shows me this tree :
$form_state
['values']
['panes']
['delivery']
['delivery_postal_code'];
...

Web Assistant’s picture

If the form #tree is set to true then it would be:

<?php
function cartscheduler_form_alter_validate($form, &$form_state) {
  if ($form_state['values']['panes']['delivery']['delivery_postal_code'] !== '93330') {
    form_set_error('panes][delivery][delivery_postal_code', t('we dont deliver in your town'));
  }
}
?>
artatum’s picture

The most bizarre being that if I empty the field then my red outline is displayed. But it is achieved by a validate function which is using $element argument...
Strange...

artatum’s picture

'form #tree' ?
By tree, I mean the hierarchy of the array...

artatum’s picture

Oh, oh. Bingo! You're right ! It works!
I've never seen this strange notation :

'panes][delivery][delivery_postal_code'

but I'm not going to forget it !
It's very cool : I dont need Rules anymore!!!

Thanks a couple of million times !