Welcome Guest, Not a member yet? Register   Sign In
Problem with setting a default zero in form validation rule
#11

[eluser]jprateragg[/eluser]
I wouldn't modify the core--I've already extended it to incorporate some other functions.

How would I modify the data before the validation takes place then?
#12

[eluser]Mirge[/eluser]
Code:
if($this->input->post('adjusted') == '') { $_POST['adjusted'] = 0; }

tried this yet?
#13

[eluser]jprateragg[/eluser]
I had thought about it--but it just seemed too easy. I thought CI would have a special way of doing that.
#14

[eluser]PhilTem[/eluser]
Why don't you just set a default value for the form input field?

Code:
echo form_input('field_name', set_value('field_name', '0'));
// will create
<input type="text" value="0" /> // before the first submit
// and
<input type="text" value="whatever-has-been-submitted" /> // after the first submit and validation failed on other inputs, assuming the user entered "whatever-has-been-submitted" in the input
#15

[eluser]jprateragg[/eluser]
I was going to do that later as I modified the form to be used for both adding and editing. But I needed to be able to set some values dynamically depending on the values of other fields. In this case, I have several fields whose values cannot be ""--must be 0. I know my default values in the database would take care of this, but not every situation will be like this one and will have to have a default value of a different number.
#16

[eluser]CroNiX[/eluser]
You don't really need to do that in validation. You can cast it to an int.
Code:
$number = (int)$this->input->post('number_field');

If number_field doesn't exist in CI's post array, $number will be 0 because the value will be boolean FALSE which is int(0) and the same with an empty string.
#17

[eluser]Mirge[/eluser]
[quote author="CroNiX" date="1353538121"]You don't really need to do that in validation. You can cast it to an int.
Code:
$number = (int)$this->input->post('number_field');

If number_field doesn't exist in CI's post array, $number will be 0 because the value will be boolean FALSE which is int(0) and the same with an empty string.[/quote]

Unless I misunderstood, he just explained that it won't always be "0" and is looking for more flexibility.
#18

[eluser]jprateragg[/eluser]
@Mirge--that's correct. I'm porting a financial analysis application where we need to set some values to be other numbers than just 0. Thanks again for all your help!




Theme © iAndrew 2016 - Forum software by © MyBB