Welcome Guest, Not a member yet? Register   Sign In
Get validation rules
#1

[eluser]bas_vdl[/eluser]
Hi there,

is it possible to get a array or string of the validation rules set for a form field?

i'm building a live validation library for CI and i will pass an extra variable to the form_helper.

<?php echo form_input(array('name' => 'first_name', 'validation' => 'required|max_length[10]')); ?>

but i want to grab the value "required|max_length[10]" from out of the form_validation files. how do i have to do that?

or maybe it is better to grab the values in the form_helper.php files and renerate the textfield with the extra values...

in the end i need the values "required|max_length[10]" in my javascript file as soon as the cursor leaves the textfield.


thank you for your time

regards

Bas
#2

[eluser]TheFuzzy0ne[/eluser]
Welcome to the CodeIgniter community!

I don't understand. You have to manually set the validation rules in order for the validation to run, so why can't you just re-use that array?
Code:
$rules = array( // <-- Your validation rules are here!
    array(
        'field' => 'field1',
        'rules' => 'trim|required|xss_clean'
    ),
    array(
        'field' => 'field2',
        'rules' => 'trim|required|xss_clean'
    ));

$this->form_validation->set_rules($rules);
#3

[eluser]bas_vdl[/eluser]
i have a file form_validation.php. at the moment a user has filled in a text field i will check with jquery if it filled in right.

so i thought, if i can read the validation rules from out of the form_validation file and put them in a HTML attribute i can read this and check in my javascript file.

so maybe it is a good idea at the moment that the form_helper the text field build he grabs the validation rules that belongs to the text field and pass them in a html attribute.

so you get something similar like: &lt;input name="firs_name" validation="required|alpha" /&gt;

with the unblur function i can pass the validation attributes en chec live if a field is filled in correctly.

Code:
&lt;?php
$config = array(
    'login' => array(
        array(
            'field' => 'username',
            'label' => 'gebruikersnaam',
            'rules' => 'required'
        ),
        array(
            'field' => 'password',
            'label' => 'wachtwoord',
            'rules' => 'required|md5'
        )
    )
)
?&gt;
#4

[eluser]TheFuzzy0ne[/eluser]
Code:
$this->load->config('form_validation', TRUE);

And then you can extract the part of the array you need.

Code:
$form_validation = $this->config->item('form_validation', 'login');
#5

[eluser]bas_vdl[/eluser]
[quote author="TheFuzzy0ne" date="1236532175"]
Code:
$this->load->config('form_validation', TRUE);

And then you can extract the part of the array you need.

Code:
$form_validation = $this->config->item('form_validation', 'login');
[/quote]

Code:
$this->load->config('form_validation', TRUE);

what means the second (true) parameter?
#6

[eluser]Tom Schlick[/eluser]
it makes it go into a sub array called form_validation to avoid collisions with other config values
#7

[eluser]bas_vdl[/eluser]
i can not use this in the form helper, anyboudy an idea?
#8

[eluser]TheFuzzy0ne[/eluser]
Why can't you use this in the form helper?
#9

[eluser]xwero[/eluser]
Code:
function get_validation($field)
{
   $CI =& get_instance();
  
   if( ! isset($CI->form_validation->_field_data[$field]))
   {
      return '';
   }

   return ' validation="'.$CI->form_validation->_field_data[$field]['rules'].'"';
}
usage
Code:
&lt;input type="text" name="text"&lt;?php echo get_validation('text') ?&gt;&gt;
To be on the safe side you better check if the form_validation library is instantiated.
#10

[eluser]bas_vdl[/eluser]
when i use this method in the form_helper file it will return an empty array, what is wrong?

function get_validation($field)
{
$CI =& get_instance();
$CI->load->library('form_validation');


print_r($CI->form_validation->_field_data); //EMPTY ARRAY


if( ! isset($CI->form_validation->_field_data[$field]))
{
return 'error';
}

return ' validation="'.$CI->form_validation->_field_data[$field]['rules'].'"';
}




Theme © iAndrew 2016 - Forum software by © MyBB