Welcome Guest, Not a member yet? Register   Sign In
[SOLVED] GRRR! set_select form validation failing on integer [SOLVED]
#1

[eluser]drakeonfire[/eluser]
Hello,

Essentially I'm trying to modify an item from the database in a form, the set_select works fine before form submittion (has the right field selected etc), but once the form has been posted it appears to stop working. I believe this is because it's comparing the supplied Integer in the set_select to the post value (which is a string as it's a post value).

I've tried strval, (string) etc on the integer but they don't work, even though my logic tests say they should.

Any idea on how to fix this?

My code for the set select is:
Code:
<option
value="&lt;?php echo $cat-&gt;id; ?&gt;"
&lt;?php echo  set_select(
  'ca_sub_category',
  $cat->id,
  ((!empty($original->sub_category) && $original->sub_category == $cat->id) ? TRUE : FALSE)); ?&gt;
>&lt;?php echo $cat->title; ?&gt;</option>
(I tabbed it to make it easier to read, it's just in one block in my code).
Thanks.
#2

[eluser]drakeonfire[/eluser]
Ummm... Okay?
#3

[eluser]@robertotra[/eluser]
PHP function: intval()

anyway, you should first check if the input value is as expected before using it into the code: as example with is_numeric($_POST['var'])

#4

[eluser]drakeonfire[/eluser]
Hello Robertotra,

Sorry but integer based functions won't help, as I'm passing it the integer and it's comparing it against a string, and as mentioned I've also tried to convert that integer into a string (and when it is converted, it passes the logic test I do to see if it's the same as the value).

And the only way I'd be able to use those functions would be to edit or change the validation / set_select method, which there should be no reason to do as it'll probably cause issues for any selects that are actually strings.

Thanks
#5

[eluser]@robertotra[/eluser]
Why do not just add a condition inside your function set-select? In pseudo-code:

Code:
if (is_numeric($post_value))
    // compare with intval(database_value)
else
    // compare with raw database_value

You have just to be sure that there will be no comparison with values evaluated to 0 (FALSE)

R.

#6

[eluser]drakeonfire[/eluser]
Hello Robertotra,

The main reason is there's no reason to re-invent the wheel, this function should be working. I think it may not be down to type casting at all, doing some research, the post values appeear to be fine in my controller, but anything trying to get passed to the view seems to disappear. I don't think the form helper functions are actually doing anything, (Well they are, but it's thinking there is no post etc).

I'm trying to find the problem now, it goes from one library into my main one to load the views, in the first library it's working fine, in the library i use for things like loading it's not echoing out any post values, so not sure what has happened.
#7

[eluser]@robertotra[/eluser]
It might depend on your condition, which starts with (!empty($original->sub_category)) : if for any reason the category is empty it evaluates automatically to false without checking the post value, so nothing is selected anyway.

Try debugging adding temporarily a more complex condition on multiple lines before passing the parameter to the function, just to check if the sub_category is empty:

Code:
if (empty($original->sub_category))
    echo "WARNING, category is empty!!!"
else
    echo "Category is " . $original->sub_category . " going on...<p>"
#8

[eluser]drakeonfire[/eluser]
Hello Robertotra,

As mentioned in my previous post, I don't think this is a issue with my checking anymore, somehow the post data is not making it to the form (even though it is getting posted, checked by print_r($_POST) and the profiler.

I'm trying to figure out how the form helper is being loaded even though I've removed the line where it gets loaded ... can't seem to see where it's loading from.
#9

[eluser]drakeonfire[/eluser]
How odd... My problem for the post being lost was:
Code:
$CI->form_validation->set_rules(
array(
'field' => 'ca_title',
'label' => 'Title',
'rules' => 'htmlspecialchars|required|min_length[3]|max_length[200]'
);
I changed it so it is:
Code:
$CI->form_validation->set_rules('ca_title','Title','htmlspecialchars|required|min_length[3]|max_length[200]');

And now the set_value function is working correctly when an item is posted (it doesn't use default, uses posted item).

Only issue is the set_select is still not working grr!
#10

[eluser]drakeonfire[/eluser]
set_select is now working - apparently set_rules over rides any config in the form_validation config file, and as set_value / set_select etc only work on elements with validation, it wasn't showing.

All is working now.




Theme © iAndrew 2016 - Forum software by © MyBB