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.
#12

[eluser]CroNiX[/eluser]
Show your form element that this rule is being applied on. Something is strange with your mail test. It says "invalid string" where it should be showing the value of the form field.

Also remove the last closing php tag - they are unnecessary and can cause problems if whitespace appears after them - so it's best to just not use them.

You'll notice none of CI's libraries or anything else have a close php tag.
#13

[eluser]seba22[/eluser]
[quote author="CroNiX" date="1354475386"]Show your form element that this rule is being applied on. Something is strange with your mail test. It says "invalid string" where it should be showing the value of the form field.[/quote]

Sorry, i just enter this as a string Smile
To just express, that string should not pass validation because simple "aaa" actually should pass.

Here's part form
Code:
<input type="text" name="admin_title" value="<?php echo $populate['admin_title']; ?>">  
    <?php echo form_error('admin_title'); ?>


Sorry, for make if confusing

Added:
I remove close php tag, still ISE error
#14

[eluser]seba22[/eluser]
Hello,

Finally i got it working.

Replace this


Code:
$this->set_error('valid_array_element', 'The %s field can only be one of the following options: ' . implode(', ', $allowed));

to

Code:
$this->error('valid_array_element', 'The %s field can only be one of the following options: ' . implode(', ', $allowed));


function name is just "error" not "set_error"


Regards ;-)




Theme © iAndrew 2016 - Forum software by © MyBB