Welcome Guest, Not a member yet? Register   Sign In
form_validation library and discrete functions ?
#1

[eluser]Monarobase[/eluser]
Hello,

I'm sorry if there is a very simple answer to this one but I have read the docs, searched the wiki and searched the forum so I am positing my question here.

I'm building a dynamic form which builds a form from database entries, I have made views per type of form input and am now working on the validation.

My database contains collomns like :

'required' => 0 or 1
'min' => 0 (deactivated) or 1 to 99999
'max' => 0(deactivated) or 1 to 999999

I could easily build the rules lines and run something like this for each line :

Code:
if($db->required == 1) $rules .= ($rules == '') ? 'required' : '|required';
if($db->min > 0) $rules .= ($rules == '') ? 'min_length['.$db->min.']' : '|min_length['.$db->min.']';
if($db->max > 0) $rules .= ($rules == '') ? 'max_length['.$db->max.']' : '|max_length['.$db->max.']';
$this->form_validation->set_rules($v->name, $v->descr, $rules);

However the docs mention discrete functions, but I have not managed to get them to work ...

This is what I have tried :

Code:
$this->form_validation->set_rules($v->name, $v->descr);
if($db->required == 1) $this->form_validation->required($v->name);
if($db->min > 0) $this->form_validation->min_length($v->name, $db->min);
if($db->max > 0) $this->form_validation->max_length($v->name, $db->max);

It seems that it doesn't work like the database library though as the discrete functions are not taken into account.

As you can see I have got my code working and it's more out of interest, but how are you supposed to use the "discrete" functions that the doc talks about here :

http://codeigniter.in.th/user_guide/libr...ereference

?

Thanks in advance
#2

[eluser]cahva[/eluser]
I havent used those discrete functions but I have a suggestion to your code. Instead of concatinating, why not add rules to array and glue them together with '|' to finalize rules?

eg.
Code:
$rules = array();

if ($db->required == 1)
{
    $rules[] = 'required';
}

if ($db->min > 0)
{
    $rules[] = 'min_length['.$db->min.']';
}

if ($db->max > 0)
{
    $rules[] = 'max_length['.$db->max.']';
}

$this->form_validation->set_rules($v->name, $v->descr, implode('|',$rules));

Something like that..
#3

[eluser]Monarobase[/eluser]
Thankyou, I like your method and will use it but would still be grateful if someone could tell me what I'm doing something wrong with the functions ...
#4

[eluser]WanWizard[/eluser]
Depends a bit on what you expect.

All the manual says is that you can call the form validation methods directly. In which case they just return TRUE or FALSE, a return value you have to check yourself. They don't magically create rules.
#5

[eluser]Monarobase[/eluser]
Ok Thank you, that makes a lot of sense, the method suggested above makes the code a lot more legible and "magically" building the rules like the db library does would be more fun than necessary.




Theme © iAndrew 2016 - Forum software by © MyBB