CodeIgniter Forums
form validation and checking for allowed values for example in listbox - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=22)
+--- Thread: form validation and checking for allowed values for example in listbox (/showthread.php?tid=56175)

Pages: 1 2


form validation and checking for allowed values for example in listbox - El Forum - 12-01-2012

[eluser]seba22[/eluser]
Hello,

How check is value in form validation is inside allowed values.

For example, user submit a listbox.
It can have some values for example
a
aa
aaa

So i need to make sure that some_listbox have value a or aa or aaa

When i dont use codeigniter i use statement:


Code:
$allowed = array("a", "aa", "aaa");
if (in_array($_POST['someval'], $allowed)) {
// no hacking attempt
$val_for_db=$_POST['someval'];
}
else
{
//hacking attmpt , set default value
$val_for_db='aa';
}

I can clear see, that there is no option like this in manual... so i need to create it by my own.


I extend validation class and add this function:

Code:
function valid_array_element($str, $allowed)
{

if (in_array($str, $allowed)) {
return TRUE;
}
else
{
return FALSE;
}
          
        }

And i don't know how pass array
Code:
$allowed = array("a", "aa", "aaa");
                  
                   $this->form_validation->set_rules('admin_title', 'Somefield', 'required|valid_array_element[$array]');

But that's of course not working...

Do You have any idea ?


I think we can, serialize and base64 code, and just put this
Code:
valid_array_element['.base64(serialize($array)).']')
But maybe there is other, bether way ?



form validation and checking for allowed values for example in listbox - El Forum - 12-01-2012

[eluser]CroNiX[/eluser]
You can only pass strings via the 2nd parameter, so you need to do something like:
Code:
$this->form_validation->set_rules('admin_title', 'Somefield', 'required|valid_array_element[' . implode(';', $array) . ']');

And then in your validation function, first line:
Code:
function valid_array_element($str, $allowed)
{
  $allowed = explode(';', $allowed);
  //..rest of your code
}



form validation and checking for allowed values for example in listbox - El Forum - 12-01-2012

[eluser]seba22[/eluser]
[quote author="CroNiX" date="1354387994"]You can only pass strings via the 2nd parameter, so you need to do something like:
Code:
$this->form_validation->set_rules('admin_title', 'Somefield', 'required|valid_array_element[' . implode(';', $array) . ']');

And then in your validation function, first line:
Code:
function valid_array_element($str, $allowed)
{
  $allowed = explode(';', $allowed);
  //..rest of your code
}
[/quote]


Thank You for reply.
It's working quite well.


What about displaying error ?

Drop down is a bad example, but i would like have error showing
You entered wrong value, allowed are a,aa,aaa

I was trying out to figure this by myself but i don't know...

I was trying to look at language file, for example here

Code:
$lang['min_length']   = "The %s field must be at least %s characters in length.";

But how pass them separated via coma values Wink

Regards


form validation and checking for allowed values for example in listbox - El Forum - 12-01-2012

[eluser]CroNiX[/eluser]
You can set the error message, and retrieve it. See the "callbacks" section of the form validation library in the user guide.
Code:
function valid_array_element($str, $allowed)
{
  $allowed = explode(';', $allowed);
  if (in_array($str, $allowed))
  {
    return TRUE;  //return TRUE, passed the rule
  }
  else
  {
    //failed rule, set error message and return FALSE
    $this->form_validation->set_error('valid_array_element', 'The %s field can only be one of the following options: ' . implode(', ', $allowed));
    return FALSE;  
  }
}

Then in your form, where this form element is, you'd show the individual error like any other validation error.
Code:
echo form_error('name-of-field-this-rule-was-applied-on');
Which would show "The Somefield field can only be one of the following options: a, aa, aaa", if it failed.


form validation and checking for allowed values for example in listbox - El Forum - 12-02-2012

[eluser]seba22[/eluser]
[quote author="CroNiX" date="1354399879"]You can set the error message, and retrieve it. See the "callbacks" section of the form validation library in the user guide.
Code:
function valid_array_element($str, $allowed)
{
  $allowed = explode(';', $allowed);
  if (in_array($str, $allowed))
  {
    return TRUE;  //return TRUE, passed the rule
  }
  else
  {
    //failed rule, set error message and return FALSE
    $this->form_validation->set_error('valid_array_element', 'The %s field can only be one of the following options: ' . implode(', ', $allowed));
    return FALSE;  
  }
}

Then in your form, where this form element is, you'd show the individual error like any other validation error.
Code:
echo form_error('name-of-field-this-rule-was-applied-on');
Which would show "The Somefield field can only be one of the following options: a, aa, aaa", if it failed.[/quote]


Hello, thank You for reply.

I try Your code, but without success (i mean your idea is absolute OK, it's something wrong with my extension of CI_Form_Validation) i got ISE (Internal Server Error).

Inside file
Code:
\libraries\MY_Form_validation.php

i have code
Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class MY_Form_validation extends CI_Form_Validation {

    function __construct()
    {
        parent::__construct();  

    }
    
    

  function valid_array_element($str, $allowed)
{

  $allowed = explode(';', $allowed);
if (in_array($str, $allowed)) {
return TRUE;
}
else
{
    //failed rule, set error message and return FALSE
$CI = & get_instance();    
$CI->form_validation->set_error('valid_array_element', 'The %s field can only be one of the following options: ' . implode(', ', $allowed));
    
return FALSE;
}
          
        }

}
?>

Any idea what i do wrong?

I try,
$this->form_validation
but also without success, that's why i change to get_instance(); but without positive result.

Regards


form validation and checking for allowed values for example in listbox - El Forum - 12-02-2012

[eluser]PhilTem[/eluser]
Well, I guess it's not working properly since you're extending a wrong class

Code:
class MY_Form_validation extends CI_Form_validation

See the Form_validation is just a capitalized F not V.
Then $this-> should work as well for the language line.

Regarding the %s in the error fields, look at [url=http://php.net/manual/en/function.sprintf.php]sprintf[/php]


form validation and checking for allowed values for example in listbox - El Forum - 12-02-2012

[eluser]CroNiX[/eluser]
Also, I showed you how to do a callback. If you extend the form validation library then it's unnecessary to do
$CI =& get_instance(), because $this IS the form validation library.

Try changing
Code:
$CI = & get_instance();    
$CI->form_validation->set_error('valid_array_element', 'The %s field can only be one of the following options: ' . implode(', ', $allowed));
to
Code:
$this->set_error('valid_array_element', 'The %s field can only be one of the following options: ' . implode(', ', $allowed));

in addition to fixing the CI classname like PhilTem pointed out.


form validation and checking for allowed values for example in listbox - El Forum - 12-02-2012

[eluser]seba22[/eluser]
Hello Friends,

No success, still Internal Server Error.

I corrected spell typo like PhilTem found, and make changes like You suggested.

Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class MY_Form_validation extends CI_Form_validation {

    function __construct()
    {
        parent::__construct();  

    }
    
    

  function valid_array_element($str, $allowed)
{

  $allowed = explode(';', $allowed);
if (in_array($str, $allowed)) {
return TRUE;
}
else
{
    //failed rule, set error message and return FALSE
$this->set_error('valid_array_element', 'The %s field can only be one of the following options: ' . implode(', ', $allowed));

return FALSE;
}
          
        }
        
  
}
?>

Hmmm


form validation and checking for allowed values for example in listbox - El Forum - 12-02-2012

[eluser]CroNiX[/eluser]
What do your error logs show? What's the error message? How are you loading the validation library? What is the filename of your new MY_Form_validation.php fileand it's location? How are you setting the rules? You've changed things but aren't showing the rest of the code where you changed them.


form validation and checking for allowed values for example in listbox - El Forum - 12-02-2012

[eluser]PhilTem[/eluser]
I see another, possible issue:

Code:
function __construct($rules = array()) {
  parent::__construct($rules);
}

Took me several hours the first time I extended the form-validation library to figure that out Wink