3

I have a decimal field. The field has a minimum precision of 10. Can I change this precision? I need that the numbers allowed have the next format: 19.1, 20.3. In other words that the number be of 2 integers and one decimal.

I select one decimal in the configuration of the decimal field , however the field accepts big numbers. How can I change this, the solution can be with form_alter.

Thanks.

0

I don't think this is possible via form alter however I would implement these verifications in a hook_nodeapi() to check that your number is between 10 and 99.9 (2 decimals) and a precision of just one digit.
Put this code in a custom.module:

function mymodule_nodeapi($node, $op, $a3, $a4) {
  if ($op == 'validate') {
    $number = $node->field_mynumber[0]['value'];
    if ($number < 10 || $number > 100) {
      form_set_error('field_mynumber', t('Please select a number between 10 and 99.9.'));
    }
    if (round($number, 1) != $number) {
      form_set_error('field_mynumber', t('Please select a number with a precision of 1 digit.'));
    }
  }
}

Of course you can also add a test on $node->type to restrict this on some of your content types. PS: answer updated to use D6

14
  • Thanks for your answer and your time. Your idea is good but your option did not work to me. I replacement the "field_number" by my field in cck in the code but it doesn't work, what will it be my mistake? What's the meaning of "node->language"? I'm sorry for this question. Thanks again.
    – cabita
    Aug 10 '11 at 12:30
  • Are you on Drupal 7 because [$node->language] is Drupal 7 specific, if you're on D6 you have to remove this part.
    – tostinni
    Aug 10 '11 at 13:22
  • hello. I put the next code: function mymodule_node_validate($node, $form, &$form_state) { $number = $node->field_mynumber[0]['value']; if ($number < 10 || $number > 100) { form_set_error('field_mynumber', t('Please select a number between 10 and 99.9.')); } if (round($number, 1) != $number) { form_set_error('field_mynumber', t('Please select a number with a precision of 1 digit.')); } } and it doesn't work. I am working with Drupal 6. I'm sorry, I forgot to put the version of drupal.
    – cabita
    Aug 10 '11 at 15:41
  • Ok I updated my answer, hook_node_validate is specific to D7.
    – tostinni
    Aug 10 '11 at 16:49
  • Hello. Very good. This it works well for validate the numbers between 10 and 100 but it doesn't validate that the number only has one decimal. Thanks very much.
    – cabita
    Aug 10 '11 at 17:09
2

yeah, _node_validate is a flexible way. an alternative to a custom module, can be use a Rules module and integration with Rules Forms support module. Also will be useful use a more flexible CCK field, then, this will allow to validate whatever number with the model that you want.

.1. install the modules.

.2. then we need now is indicate to rules in what form must work. You must go to Rules > Form events and enable the option Enable event activation messages on forms. then go to the respective form and click in the Activate events for.. (the name of the form)

.3. go to rules and add a new rule and select in the Event: the respective event on which you want to evaluate this rule. (to this case Rules Forms modules will include the name of your form in the Rules Forms section on the Event select list.

.4. In a condition of rule add a numeric comparison (or whatever other condition that you want, for example a custom php code):

example with numeric comparison:

field Number 1 (php code): $number= $form_state['values']['number'][0]['value']; if ($number < 10 || $number > 100) { echo 1; }

Field Comparison: Equal to

Field Number 2: 1

.5. in the action option, of your rule, add an action like a form error message to warn about change the syntax of the number. in Form element ID add the ID of the field, for example field_color (you can review the name in the list of fields CCK of the content/node type).

.6. Create a new rule if is necessary, like $number= $form_state['values']['number'][0]['value']; if (...) { echo 1; } and add the respective error message.

do not forget enable the rule, to see working your rule!.

(I hope that this information be useful)

1
  • Hi. Your idea it's works with rules. I will take it this idea if with node_validation is not possible, because I have many forms for validate. I am going to try to validate that the number of decimals is not bigger that one. Thanks for your answer and your time.
    – cabita
    Aug 10 '11 at 12:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.