Validation problem

Posts: 83
Joined: 03/31/2008

Hi All,

Sorry for maybe a very simple question, but it has been bugging me for the last few productive hours.

<?php
function uc_attribute_base_num_code_validate($form) {
    if (!
is_int($form['#value'])){
            
form_error($form, t('This is not a valid code.'));}
    if (
10!=strlen($form['#value'])) {
           
form_error($form, t('This is not a valid code.'));}
    return;
}
?>

Why is number 1234567890 not a valid integer? How can I make sure only numbers in this format are submitted through the form?

Hope somebody sees what I missed.

Thanks
Mark

Posts: 4
Joined: 11/10/2008

Edit
Sorry, Was wrong...

Are you accessing the form values right?

Posts: 83
Joined: 03/31/2008

Hi vordude

Yes,

got a working solution now:

<?php
function uc_attribute_base_code_validate($form) {
  
$thevalue = $form['#value'];
   if (
is_numeric($thevalue)) {
    
$thevalue = $thevalue + 0;
   } else {
    
form_error($form, t('This is not a valid numerical code.'));;
   }
   if (!
is_int($thevalue)){
    
form_error($form, t('This is not a valid code 2.'));}
   if (
10!=strlen($form['#value'])) {
    
form_error($form, t('This is not a valid code.'));}
   return;
}
?>

Thanks for checking! Seemed PHP needed some extra support to see a number as an integer.
Found great help here: http://drupal.org/node/36899

Posts: 4
Joined: 11/10/2008

I thought it was the is_int at first, but I tried a simple

That was my initial reply, but I tried:

if (is_int(1234567890)) {
  echo "yup";
}
else {
  echo "nope";
}

and got a "yup"

Posts: 5617
Joined: 08/07/2007
AdministratorHead Code Monkey - I eat bugs.

Your validate function is actually all wrong. Instead of looking at $form, you should be looking at $form_values which you need to be accepting as a second parameter to your validate function.

Example:

<?php
function uc_cart_checkout_form_validate($form_id, $form_values) {
  if (!
is_int($form_values['some_key'])) {
   
// ...
 
}
}
?>

Posts: 2352
Joined: 08/07/2007
AdministratoreLiTe!

When you get data from a form, it's usually a string, especially from textfields. is_int() returns FALSE for strings, even if they are numeric (is_numeric() returns TRUE). If you really need to make sure that it's only digits, you can cast it to an integer

<?php
  $value
= (int)$value; // Implicitly done by $value + 0 above.
?>

or check that the string contains only the characters 0-9
<?php
 
if (!preg_match('/[0-9]+/', $value)) {
   
// Error, not validated.
 
}
?>

Posts: 83
Joined: 03/31/2008

@vordude_
Textfield outputs are normally seen by PHP as text. So it would look more like "1234567890" which is_int will return a false on. Like Lyle points out, it is quite easy to make "1234567890" 1234567890 again, but if you don't know...

@Ryan
Hi Ryan, I'm playing around with form element formatting. Which I prefer on complete form validation.

<?php
      $form
['import']['taric'] = array(
       
'#type' => 'textfield',
           
'#title' => t('Taric code'),
           
'#required' => TRUE,
           
'#default_value' => $node->taric,
           
'#description' => t('Please check online: <a href="!url">TARIC-home</a>.', array('!url' => 'http://ec.europa.eu/taxation_customs/dds/cgi-bin/tarchap?Lang=EN')),
           
'#weight' => -5,
           
'#size' => 15,
       
'#validate' => array('uc_attribute_base_taric_code_validate' => array()),
      );

// Which will lead to

function uc_attribute_base_taric_code_validate($form) {
  
$thevalue = $form['#value'];
   if (
is_numeric($thevalue) && is_int((int)$thevalue) && 10==strlen($form['#value'])) {
       return;
   } else {
    
form_error($form, t('This is not a valid 10-digit TARIC code.'));;
   }
}
?>

@Lyle
Thanks! Don't know if my code works better now, but it sure looks better! Eye-wink

Why is form element validation almost nowhere used in Ubercart? Asking this after a more or less dumb copy paste from UC product to get the standard validation function working on my custom price fields?

<?php
function uc_attribute_base_product_price_validate($form) {
 
$pattern = '/^\d*(\.\d*)?$/';
 
$thevalue = $form['value'];
  if (!empty(
$form['#value']) && !is_numeric($thevalue) && !preg_match($pattern, $form['#value'])) {
   
form_error($form, t('Price must be in a valid number format. No commas and only one decimal point.'));
  }
}
//from the original in uc_product

function uc_product_validate($node) {
 
$pattern = '/^\d*(\.\d*)?$/';
 
$price_error = t('Price must be in a valid number format. No commas and only one decimal point.');
  if (!empty(
$node->list_price) && !is_numeric($node->list_price) && !preg_match($pattern, $node->list_price)) {
   
form_set_error('list_price', $price_error);
  }
  if (!empty(
$node->cost) && !is_numeric($node->cost) && !preg_match($pattern, $node->cost)) {
   
form_set_error('cost', $price_error);
  }
  if (!
is_numeric($node->sell_price) && !preg_match($pattern, $node->sell_price)) {
   
form_set_error('sell_price', $price_error);
  }
  if (!empty(
$node->weight) && !is_numeric($node->weight)) {
   
form_set_error('weight', t('Weight must be in a valid number format. No commas and only one decimal point.'));
  }
  if (
$node->default_qty) {
    if (!
is_numeric($node->default_qty)) {
     
form_set_error('default_qty', t('Quantities should be numeric.'));
    }
    else if (
$node->default_qty < 0) {
     
form_set_error('default_qty', t("Adding negative items to the cart doesn't make sense, so don't make it easy."));
    }
  }
}
?>

For me it would make sense to have a standard form element check for prices, weights, etc.

Thanks
Mark