Validate only if field not empty

Discussion in 'Standard Support' started by dee, Feb 22, 2012.

Thread Status:
Not open for further replies.
  1. dee

    dee Member

    Level: Community
    is it somehow possible to validate only when a field has been filled-out?

    i tried many things but could not suceed...

    the field has the following specs:
    Maximum length: 7 (in order to have 1234,00)
    Decimal: YES
    Integer length: 4
    Decimal length: 2

    the following validations i got running:
    1. PHP, Condition "return $data >= 2500;", match

    2. PHP, Condition "return $data <= 1;", match

    3. Regular expression: /^\d+(\,\d{1,2})?$/
    , match (this one allows only decimal numbers with one comma and two decimal points)


    i want for this field the following to be possible:

    A. the possibility that the field can be empty as well (or not to run the validations if empty)

    B. not only entering the values of max 2499,99 but also 2500,00 (as stated in "$data >= 2500") and min 2,00 but also 1,00 !!! i tried using "$data >= 2500,00") and 2500.00, all combinations, but something seems to round the numbers...???
     
  2. troester

    troester Well-Known Member Staff Member

    Level: Standard
    You can set a condition for the validation, e.g. to run validation only if field not empty:
    return $data!='';

    and validation (if value should be between 1 and 2500):
    return 1<= (float)$data AND (float)$data<=2500;
     
    2 people like this.
  3. dee

    dee Member

    Level: Community
    ahhh! now i got it! i totally ignored the meaning of "condition"... many thanks, works perfect!
     
  4. dee

    dee Member

    Level: Community
    very strange... i had to reopen the thread, because suddenly it doesnt work anymore...
    i was using as
    "validation rule": PHP
    and
    "condition":
    return (float)$data == 0;

    so even 0,5 was valid but 0 not...

    now i changed the condition to:
    return 0<=(float)$data;

    it still doesnt work...

    i updated today from svn, maybe there is a bug?
     
  5. dee

    dee Member

    Level: Community
    "friendly bump"
     
  6. troester

    troester Well-Known Member Staff Member

    Level: Standard
    Don't mix up condition field and php field;
    The condition determines IF the validation should be run, so something like
    don't run the validation (=return false) if input is empty or if user is superadmin.

    The code in the validation validates the input.

    So in your case if the field may be empty or the value must be between 0 and 2500:
    condition (to run validation only if field not empty):
    return $data!='';

    and validation (PHP code) (if value should be between 0 and 2500):
    return 0<= (float)$data AND (float)$data<=2500;
     
  7. dee

    dee Member

    Level: Community
    i am totally confused...
    my field is not allowed to be empty (so i leave condition empty ???)
    and is not allowed to be "0" or "0,00" or "00,00" or "000,00" or "00,0" etc. = all ZEROS
    (so i enter in PHP return 0<=(float)$data; ???)
    ...which does not work! i can enter "0" "0,00" etc... and it does not throw the error...
     
  8. troester

    troester Well-Known Member Staff Member

    Level: Standard
    Yes

    If 0 isn't allowed you must validate
    return 0< (float)$data AND (float)$data<=2500;

    Validation must return "true" if the input is ok;
    0<=(float)$data says "0 is ok "
     
  9. dee

    dee Member

    Level: Community
    of course, my mistake...
    ok, so now it is
    CONDITION: empty

    and
    PHP code: return 0<(float)$data;
    but i still cannot enter values <1, eg: 0,5 or 0,6
    it is as if it gets rounded!
    any idea?
     
  10. troester

    troester Well-Known Member Staff Member

    Level: Standard
    What are your element settings?
    Standard decimal is 0.5
    You can change the decimal point by setting the "number format", but I don't know which format is in $data. If you change the standard maybe you'll have to do some stringhandling, numberformatting... in the php validation.
     
  11. dee

    dee Member

    Level: Community
    Maximum length Format Text Integer Decimal Integer length Decimal length Number Format No Yes Thousand Separator Decimal Point Format string
     
  12. dee

    dee Member

    Level: Community
    max.length: 10
    format: decimal
    integer lentgh: 7
    decimal length: 2
    number format: no
    thousand separator: .
    decimal point: ,
    format string: "empty"

    ...i could bet it was working once!
     
  13. troester

    troester Well-Known Member Staff Member

    Level: Standard
    Numberformat = yes if it shoud use
    thousand separator: .
    decimal point: ,
     
  14. dee

    dee Member

    Level: Community
    ok, i just set it to yes, and i still cannot enter 0,05 ...
     
  15. dee

    dee Member

    Level: Community
    again to be 100% sure:
    i am using
    validation rule: PHP
    condition:

    php code: return 0<(float)$data;
    and i want to be able to enter values >0 , like "0,5" "0,05" etc.
    were "0" is not allowed
     
  16. dee

    dee Member

    Level: Community
    all values above "1" work though!
     
  17. dee

    dee Member

    Level: Community
    maybe the trouble is somewhere in the process that i HAVE to use a comma instead of a dot for the decimal separator?
     
  18. troester

    troester Well-Known Member Staff Member

    Level: Standard
    Yes,
    $data will contain your string with comma, which is no php number.
    So you'll suspect you'll have to to some stringhandling.
     
  19. dee

    dee Member

    Level: Community
    ??? this is so unintuitive... i just cant get it done. please help. i tried with and without "number format" enabled, i tried with
    $no = number_format($data, 2, ',', '');
    return 0<(float)$no;
    as well as last line
    return 0<$no;

    ...no luck at all!
     
  20. dee

    dee Member

    Level: Community
    ... i was getting mental on this ...
    i guess i got it for now:

    the clue was in "$data will contain your string with comma"

    so in PHP i entered:
    $no = str_replace(',','.',$data);
    return 0<(float)$no;


    i will continue testing tomorrow, that thingy drove me mad!
     
Thread Status:
Not open for further replies.

Share This Page