Welcome Guest, Not a member yet? Register   Sign In
form validation and checking for allowed values for example in listbox
#11

[eluser]seba22[/eluser]
[quote author="CroNiX" date="1354473720"]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.[/quote]

Actually i don't have any error log!
Just ISE (white page - http header internal server error 500)


So starting from begining, i have controller

Code:
class Admin extends CI_Controller {
    
    
      function __construct() {
        parent::__construct();
$this->load->library('session');
$this->load->helper('url');  


$this->load->helper('form');
$this->load->library('form_validation');

    }


Here i initialize form, and form_validation.



Here i use it
Code:
$allowed = array("a", "aa", "aaa");

                    $this->form_validation->set_rules('admin_title', 'myvalue', 'required|min_length[3]|valid_array_element[' . implode(';', $allowed) . ']');
                    if ($this->form_validation->run() == FALSE) {
                        echo "not validated";
                    } else {
                        echo " validated";
                    }
                    
                   }

When i remove valid_array (entry) validation run OK.


Code:
/application/libraries/MY_Form_validation.php

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

class MY_Form_validation extends CI_Form_validation {

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

  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;
}
          
        }
        
  
}
?>



Function is called properly, look at this:

When i add mail
Code:
function valid_array_element($str, $allowed)
{

  $allowed = explode(';', $allowed);
  mail('admin@xxx','array','String to test:'.PHP_EOL.$str.PHP_EOL.' array to test:'.PHP_EOL.print_r($allowed,true));
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;
}
          
        }

I entered "invalid string" into form validated field.

Email i got:

Code:
String to test:
invalid string
array to test:
Array
(
    [0] => a
    [1] => aa
    [2] => aaa
)


If You need any tests, please tell me i will provide.


Messages In This Thread
form validation and checking for allowed values for example in listbox - by El Forum - 12-02-2012, 12:01 PM



Theme © iAndrew 2016 - Forum software by © MyBB