Welcome Guest, Not a member yet? Register   Sign In
Validation library/Helper form
#1

[eluser]Mat-Moo[/eluser]
I'm trying to do a contact form, and learning as I go Smile But I've hit a small issue :-

Code:
$rules['fname'] = "trim|required|min_length[5]|max_length[48]|xss_clean";
$rules['femail'] = "trim|required|valid_email";
$rules['fcomments'] = "trim|required|min_length[16]|max_length[1024]|xss_clean";

$this->validation->set_error_delimiters('<li>', '</li>');
$this->validation->set_rules($rules);

# set data
$data["fname"] = "Name";
$data["femail"] = "E-mail address";
$data["fcomments"] = "Comments";
$this->validation->set_fields($data);

$data["fname"] = array('name' => 'fname', 'id' => 'fname', 'class' => ($this->validation->fname_error<>"") ? 'textbox error' : 'textbox', 'value' => $this->validation->fname);
$data["femail"] = array('name' => 'femail', 'id' => 'femail', 'class' => 'textbox', 'value' => $this->validation->femail);
$data["fcomments"]=array('name' => 'fcomments', 'rows' => 5, 'cols' => 80, 'value' => $this->validation->fcomments);

As you can see in $data[fname] I'm trying to set the class of the box depending on the bases of the validation (textbox) or (textbox error). But I can't work out how you validate, or check the validation of a single field? I'm doing it this way as my form uses the form_input($fname) rather than a basic html form.

Clues and inputs aer welcome Smile
#2

[eluser]roj[/eluser]
Just thinking off the top of my head, but a quick function to see if the error exists and return the error class if it does might work?
Code:
function return_error_class($err_msg)
{
if ($err_msg != null)
{
return 'error';
}
else
{
return 'normal_class';
}
}

then just call it for each input you want to check?
not sure if that' what your looking, nor if it'll work....
#3

[eluser]Mat-Moo[/eluser]
Code:
($this->validation->fname_error<>"") ? 'textbox error' : 'textbox'

Should be the same thing really.
#4

[eluser]McNoggin[/eluser]
I think you need to run the validation before it will generate the error messages.

You need to call $this->validation->run() somewhere after this line

$this->validation->set_fields($data);

and before

data["fname"] = array('name' => 'fname', 'id' => 'fname', 'class' => ($this->validation->fname_error<>"") ? 'textbox error' : 'textbox', 'value' => $this->validation->fname);

Otherwise I don't think its generated the error message yet.
#5

[eluser]Mat-Moo[/eluser]
Thanks - but that is called before data["fname"] (Line before infact)
#6

[eluser]roj[/eluser]
Sorry reading this on phone just isn't as good as on a computer...

you should be setting the form_helper data after the validation is run ie something like this:
Code:
$rules['fname'] = "trim|required|min_length[5]|max_length[48]|xss_clean";
    $rules['femail'] = "trim|required|valid_email";
    $rules['fcomments'] = "trim|required|min_length[16]|max_length[1024]|xss_clean";

    $this->validation->set_error_delimiters('<li>', '</li>');
    $this->validation->set_rules($rules);

    # set data
    $data["fname"] = "Name";
    $data["femail"] = "E-mail address";
    $data["fcomments"] = "Comments";
    $this->validation->set_fields($data);

    if ($this->validation->run() == FALSE)
    {
        $data["fname"] = array('name' => 'fname', 'id' => 'fname', 'class' => ($this->validation->fname_error<>"") ? 'textbox error' : 'textbox', 'value' => $this->validation->fname);
        $data["femail"] = array('name' => 'femail', 'id' => 'femail', 'class' => 'textbox', 'value' => $this->validation->femail);
        $data["fcomments"]=array('name' => 'fcomments', 'rows' => 5, 'cols' => 80, 'value' => $this->validation->fcomments);
        
        $this->load->view('form', $data);
    }
    else
    {
        $this->load->view('formsuccess');
    }
#7

[eluser]Mat-Moo[/eluser]
Sometimes you can stare so hard at these things that you miss them Smile I wrongly read the set_fields to set validation and of course it is the validation! now all working as expected!




Theme © iAndrew 2016 - Forum software by © MyBB