Welcome Guest, Not a member yet? Register   Sign In
Bug in validation library (Codeigniter 1.5.4)
#1

[eluser]Unknown[/eluser]
I had a problem with a code. I think it a bug.

My view template (register_view.php):
Code:
<form method='post' action='./'>
  <input type="checkbox" name="contract" value="accept" />
  <input type="submit" value="Submit" name="create">
</form>

My controller:
Code:
$this->load->library('validation');

$rules['contract'] = "callback_contract";
$this->validation->set_rules($rules);

$fields['contract'] = 'Contract';
$this->validation->set_fields($fields);

if ($this->validation->run() == FALSE)
{
  $this->load->view('register_view', $data);
}
else
{
  echo 'Ok!';
}

And callback function for contract-checkbox:
Code:
function contract($contract)
{
  if($contract == 'accept')
  {
    return true;
  }
  else
  {
    $this->validation->set_message('login_check', 'Чекбокс необходим');
    return false;
  }
}
The problem is that when the checkbox is not checked (that means, the "$contract" variable doesn't have "accept" value), we don't get an error message, so the case is processed incorrectly. Could you please the issue. Thank you!
#2

[eluser]Michael Wales[/eluser]
Checkboxes currently have no Validation capability - it's a known issue.
#3

[eluser]Unknown[/eluser]
Is there any way to solve this problem? If you don't call the callback function, and just specify "required", then CodeIgniter processes unselected checkbox, and sends an error to the template. The problems occurs if only if the callback function is used. If it is a known error, then why does the manual say that it is possible to use it with checkboxes.
#4

[eluser]Michael Wales[/eluser]
http://ellislab.com/forums/viewthread/56269/
#5

[eluser]elrolfe[/eluser]
Hi, its not as elegant as hacking a core library, but you can put a hidden field just before the checkbox. The hidden field should have the same name as the ckeckbox, but a different value ( i guess the checkbox has the value 1, so give the hidden field value 0 for example).

If you do it that way you can validate the field with CI normally. More important, you can update the CI-framework without caring about small hacks.

However, i dont know if the markup is still valid xhtml...

Cheers
#6

[eluser]Michael Wales[/eluser]
Yeha - I don't agree with hacking the core library. I would instead create a MY_Validation library in your application/library folder. Copy over the offending method from CI_Validation and insert the hack there.

Your new method will be used instead of the method found within the CI library.

@elrolfe
Your solution is to use Javascript to set the value of the hidden input based on the checkbox's status, right? I didn't see any mention of JS - unless you are skipping the JS part and just updating that field if the input reads 0 and the POST var now appears (or vice-versa).

Nice solution to a PITA.
#7

[eluser]elrolfe[/eluser]
Well actually you can simply put a hidden field just before the checkbox. They both have the same name. No javascript here. If you check the checkbox it will overwrite (kind of) the value of the hidden field.
Is this bad practice?
I used it just the day before (when i wondered why there is no value passed to my controller), so i made sure there is a value in any case.
Just make sure you dont use the same id attribute for different fields for xhtml-compliance.

elrolfe
#8

[eluser]elrolfe[/eluser]
Took me 10 minutes with google to get the meaning of pita.
#9

[eluser]JayTee[/eluser]
[quote author="elrolfe" date="1195245527"]Well actually you can simply put a hidden field just before the checkbox. They both have the same name. No javascript here. If you check the checkbox it will overwrite (kind of) the value of the hidden field.
Is this bad practice?
[/quote]

+1 for "works without having to spend more time than necessary"

Your solution does the job nicely. I wish there were a nicer way to do this - oh well.

Thanks Wink
#10

[eluser]kenjis[/eluser]
Hi!

How about?

Code:
$rules['contract'] = "required|callback_contract";




Theme © iAndrew 2016 - Forum software by © MyBB