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

Repost what you currently have now.

-relevant view portion showing your select and outputting the form error
-relevant controller where you are setting validation rules and also the callback function

Ok, here:
relevant part of the view, where error should be displayed and where select element is render
<p><label>Código UNESCO:</label>
     &lt;?php echo form_error('check_unesco'); ?&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;
   &lt;?php if (isset($article) && $article->ID_UNESCO == $i): ?&gt;
       <option value="&lt;?php echo $clave; ?&gt;" selected="selected">&lt;?php echo $valor; ?&gt;</option>
   &lt;?php else: ?&gt;
       <option value="&lt;?php echo $clave; ?&gt;">&lt;?php echo $valor; ?&gt;</option>
   &lt;?php endif; ?&gt;
      &lt;?php endforeach; ?&gt;
     &lt;?php endif; ?&gt;

relevant part of the controller where logic happens:
public function create() {

$this->form_validation->set_rules('titulo', 'Título', 'trim|required|xss_clean');
$this->form_validation->set_rules('volumen', 'Volumen', 'trim|required|xss_clean');
$this->form_validation->set_rules('number_pub', 'Número de Publicación', 'trim|required|xss_clean');
$this->form_validation->set_rules('number', 'Número', 'trim|required|xss_clean');
$this->form_validation->set_rules('pginicio', 'Página de Inicio', 'trim|required|xss_clean');
$this->form_validation->set_rules('pgfin', 'Página Fin', 'trim|required|xss_clean');
$this->form_validation->set_rules('agno', 'Año', 'trim|required|xss_clean|required');
$this->form_validation->set_rules('palclaves', 'Palabras Claves', 'trim|xss_clean');
$this->form_validation->set_rules('id_unesco', 'Código UNESCO', 'trim|callback_check_unesco|xss_clean');
$this->form_validation->set_rules('id_codificacion', 'Codificación', 'trim|xss_clean');
$this->form_validation->set_rules('isbnpatent', 'Patente ISBN', 'trim|xss_clean');
$this->form_validation->set_rules('id_author', 'Autores', 'trim|xss_clean|required');
$this->form_validation->set_rules('id_revista', 'Revista', 'trim|xss_clean|required');

$this->data_view['arrayUnescocodes'] = $this->Munescocodes->get_select_unescocodes();
$this->data_view['arrayCodificacion'] = $this->Mcodin->getAll();

if ($this->form_validation->run()) {

the callback function
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;

See post #8. You didn't change it in your view.

Nice man, didn't see post #8 my bad, it works now.

Glad it's working.

A few tips.
In your callback, you can use %s in the error message string and it will be replaced with the field name that you enter as the 2nd parameter when using
$this->form_validation->set_rules($actual_field_NAME, $text_to_display_as_field_name, $rules);

So if
$text_to_display_as_field_name = 'Código UNESCO';
and you had in your callback:
$this->form_validation->set_message('check_unesco', 'Debe seleccionar un %s');
When the error is outputted to the screen for THIS field it will say:
Quote:Debe seleccionar un Código UNESCO
Which allows you to use the same rule on many different fields, but have the error message custom to that individual field that it is displayed next to so it can be reused.

Another thing. If this is a rule that you might want to use over and over again, perhaps in another controller or even in another CI project, then it would be best to extend the form validation library with your own rules, which essentially work the same as callbacks except they are native rules just like the built in validation rules.
Here's how to extend a Native CI Library.

It's pretty simple though.
All you'd do is create a file in

class MY_Form_validation extends CI_Form_validation {

  function __construct()

  public function check_unesco($value)
    if ($value == "0") {
      //this->set_message() instead of $this->form_validation->set_message() since $this IS form_validation
      $this->set_message('check_unesco', 'Debe seleccionar un %s');
      return FALSE;
    } else {
      return TRUE;

  //other custom rules

Then the only other difference would be in your controller, where you set the validation rules. You just don't need to use "callback_" prefixing the rule name now since it is a native validation rule:
$this->form_validation->set_rules('id_unesco', 'Código UNESCO', 'trim|check_unesco|xss_clean');

Everything else would be the same. You don't need to do anything special to load these validation rules. You still just
and CI will automatically load your extended class that is prefixed with MY_.

Now, if you need to use this rule in another controller you don't need to recreate the callback rule there. You just use this one. And if you have another CI project that you want to use the rule(s) on, you can just copy that /application/libraries/MY_Form_validation.php file to that project and all of your custom rules are now available. Pretty handy.

Excellent post CroNix and very helpful I'll take into account in this project and the new one starting right now!

Theme © iAndrew 2016 - Forum software by © MyBB