Welcome Guest, Not a member yet? Register   Sign In
CI removes the "0" from the values
#1

[eluser]DeaD SouL[/eluser]
hey there..

I'm facing a problem with the sent values from a form

there is a text input in my form called "cat_code"

and it might have zero digit "0" (without quotes) in the beginning

but CI removes them automatically..

any ideas how can i keep them from being removed?

thanks a lot in advance Smile
#2

[eluser]BrianDHall[/eluser]
Hm, I didn't know it did that - are you setting form validation rules? If so, which ones?
#3

[eluser]DeaD SouL[/eluser]
here is my validation rule

Code:
$this->form_validation->set_rules('cat_code'        , 'Category\'s Code'        , 'trim|integer|required|xss_clean');

but even if i remove that rule.. it still removes the zeros Sad
#4

[eluser]BrianDHall[/eluser]
Hm, indeed odd, I'm not able to test it myself at the moment, but have you tried running var_dump on $_POST and var_dump on the value returned by $this->input->post('cat_code') to ensure they are coming back empty?

As a lark, try explicitly casting the returned value to an integer - I _think_ that turns even an empty string into 0, but I can't remember for sure.
#5

[eluser]Michael Wales[/eluser]
It's the typecasting to an integer that is doing it. 00123 is not an integer, 123 is. You need to handle your category codes as strings.

Code:
<?php
  // Output: 123

  $int = (int) '00123';
  echo $int;
#6

[eluser]BrianDHall[/eluser]
Wow, I entirely mis-read the thread topic, my bad. I read that 0 was turning into an empty string - not that it had zero-fill at the beginning of a number. Oops.

Michael Wales is dead on - if '00123' is a string, it will come out '00123', but if it is an integer it will be '123' because integers don't get zero-filled. After all, no one says "I'm going to turn 040 soon"...but that damned James Bond doesn't play by such rules with his '007'. Ole James never plays by the rules...

Yes, so specifically when you assign a value to your form be sure it is in quotes, such as:

Code:
<input name="category" value="00123">

If the value isn't surrounded in quotes it'll be treated as an integer.
#7

[eluser]DeaD SouL[/eluser]
i'm sorry for being late..

i didn't use the manual inputs..

here check my code...

the template:

Code:
$cat_code = array(
    'name'    => 'cat_code' ,
    'id'    => 'cat_code' ,
    'size'    => 90 ,
    'style'    => 'width:98%;' ,
    'value'    => set_value('cat_code')
);
...
    <tr><th width="150">&lt;?php echo form_label('Code', $cat_code['id']); ?&gt;</th>
    <td>
        &lt;?php echo form_input($cat_code); ?&gt;
        &lt;?php echo form_error($cat_code['name']); ?&gt;
    </td></tr>


and this is the rule

Code:
$this->form_validation->set_rules('cat_code'        , 'Category\'s Code'        , 'trim|required|xss_clean');

and it still removes them...!

thanks bros Smile
#8

[eluser]BrianDHall[/eluser]
Check the HTML that form outputs. For fun, maybe try form_input("$cat_code") and see what it's HTML output is as well Smile
#9

[eluser]DeaD SouL[/eluser]
i just did

here is the "add category" page
Code:
&lt;input type="text" name="cat_code" value="" id="cat_code" size="90" style="width:98%;" /&gt;

and this is the "edit category" page
Code:
&lt;input type="text" name="cat_code" value="321654987" id="cat_code" size="90" style="width:98%;"  /&gt;


as you can see the quotes are there.. but it still removes the "zeros"

please guys... i really do need some help

and thanks for everyone that tried or will try to help
#10

[eluser]Jay Logan[/eluser]
Is it posting to a database? If so, is your "cat_code" field set to accept leading zeros? Shouldn't just be set for INT.




Theme © iAndrew 2016 - Forum software by © MyBB