Welcome Guest, Not a member yet? Register   Sign In
Callback doesn't work, what's wrong?
#1

[eluser]ReyPM[/eluser]
Hi, I've this code in my template:
Code:
<label>Código UNESCO:</label>&lt;?php echo form_error('unescocode'); ?&gt;
     &lt;?php if ($arrayUnescocodes): ?&gt;
         <select id="id_unesco" name="id_unesco">
      <option value="0">Seleccione un valor ...</option>
      &lt;?php foreach ($arrayUnescocodes as $clave => $valor): ?&gt;
   <option value="&lt;?php echo $clave; ?&gt;">&lt;?php echo $valor; ?&gt;</option>
      &lt;?php endforeach; ?&gt;
         </select>

And this other code in my controller:
Code:
$this->load->library('form_validation');
$this->form_validation->set_rules('id_unesco', 'Código UNESCO', 'trim|xss_clean|callback_check_unesco');

public function check_unesco($value) {
if ($value == 0) {
     $this->form_validation->set_message('check_unesco_value', 'Debe seleccionar un Código UNESCO');
     return FALSE;
} else {
     return TRUE;
}
    }

When I send the form and leave id_unesco as 0 non error is displayed and form is send, why? where I´m wrong?
#2

[eluser]PhilTem[/eluser]
Because you need to check for a string provided as $value in the validation function. If you check for 0 it translates to "If $value is not set or empty"

Thus:
Code:
public function check_unesco($value = '0') {
if ($value == "0") {
     $this->form_validation->set_message('check_unesco_value', 'Debe seleccionar un Código UNESCO');
     return FALSE;
} else {
     return TRUE;
}
#3

[eluser]ReyPM[/eluser]
Thanks PhilTem, now validation works but I don't know how to show message associated to error. I've tried:
Code:
&lt;?php echo form_error('id_unesco'); ?&gt;
&lt;?php echo form_error('check_unesco_value'); ?&gt;
&lt;?php echo form_error('unesco_value'); ?&gt;
And nonw shows the error text, any help?
#4

[eluser]CroNiX[/eluser]
Because the error message name must be the exact same as the rule name.

Code:
function myrule($value)  //rule name
{
  $this->form_validation->set_message('myrule', 'the error message'); //message name
}

Your rule name is "check_unesco", but your message name is "check_unesco_value", so it doesn't match up.
#5

[eluser]ReyPM[/eluser]
Still not working :-( my code now look like this:
Code:
public function check_unesco($value) {
if ($value == "0") {
     $this->form_validation->set_message('check_unesco', 'Debe seleccionar un Código UNESCO');
     return FALSE;
} else {
     return TRUE;
}
    }
And I test as:
Code:
&lt;?php echo form_error('check_unesco'); ?&gt;
#6

[eluser]CroNiX[/eluser]
Show in your controller where you are setting the validation rule for the field (or fields) you are using this callback on.
#7

[eluser]ReyPM[/eluser]
Here is (just the relevant part not the whole code):
Code:
public function create() {
$this->load->helper(array('form'));
$this->load->library('form_validation');

...
$this->form_validation->set_rules('id_unesco', 'Código UNESCO', 'trim|xss_clean|callback_check_unesco');
...
#8

[eluser]CroNiX[/eluser]
It shouldn't matter, but try putting xss_clean last in your rules. Any function that can alter the value (like htmlentities, xss_clean, etc) should go after all rules that only return a boolean true/false. The only exception to this that I can think of would be trim, which you would want first like you have to clear entries that only contain spaces.
#9

[eluser]CroNiX[/eluser]
Oh, but to SHOW that error, you use the NAME of that form element that the rule was applied to, not the RULE name.

Code:
&lt;?php echo form_error('id_unesco'); ?&gt;
#10

[eluser]ReyPM[/eluser]
Does not work yet :-(




Theme © iAndrew 2016 - Forum software by © MyBB