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

[eluser]loopyno[/eluser]
Hi guys,

I really need your help because I am going crazy. I created a simple form on my site and I wanted to use the validation class. The only problem is that although - I think - I did all that is necessary to make it work I do not get an error message - neither for the form fields nor from the code I wrote ( so I understand that the code is correct )

What I realized is that $this->validation->error_messages does not get populated although I set the rules ( basic ones ) and the fields. The Controller code is below and you can se it not working Smile here www.fuzzy-4x.com/contact.

Any kind of help is really valued Smile

class Contact extends Controller {

function Contact()
{
parent::Controller();
}

function index()
{
$this->output->cache(0);

$query = $this->db->query('SELECT text_name, text_value FROM texts where language = "'.$this->config->item('lang_selected').'"');
$temp = $query->result();
for($i=0; $i<count($temp); $i++) {
$data[$temp[$i]->text_name] = $temp[$i]->text_value;
}
if(strlen($this->session->userdata('autohide')) < 1)
{
$this->session->set_userdata('autohide', 1);
}
$data['stop'] = $this->session->userdata('autohide');
$menu = array("home", "progress", "favorites", "about_me", "contact", "faq", "site_map");
if( !(in_array(trim($this->uri->segment(1)), $menu)) )
{
if(strlen($this->session->userdata('current_page')) < 1)
{
$view = "home";
} else {
$view = $this->session->userdata('current_page');
}
} else {
$view = trim($this->uri->segment(1));
}

$rules['name'] = "required";
$rules['email'] = "required";
$rules['topic'] = "required";
$rules['message'] = "required";
$this->validation->set_rules($rules);

$fields['name'] = 'contact_name';
$fields['email'] = 'contact_email';
$fields['topic'] = 'contact_topic';
$fields['message'] = 'contact_message';
$this->validation->set_fields($fields);

$this->session->set_userdata('current_page', $view);
$this->load->view('menu_view',$data);
if ($this->validation->run() == FALSE)
{
$this->load->view('contact_view');
}
else
{
$this->load->view('contact_success_view');
}
$this->load->view('footer_view');
}
}
#2

[eluser]wiredesignz[/eluser]
Welcome to the forums, try using the forum tags [ code ] around your code [ /code ]

Your controller looks basically ok, could you also post the view which displays the form and messages.
#3

[eluser]loopyno[/eluser]
Sorry for not using code Sad that was my first post Tongue

Ok the view code is bellow:

Code:
&lt;?$attributes = array('id' => 'contact_form');echo form_open('../contact', $attributes);?&gt;
            <fieldset>
                <legend>&lt;?echo $contact_legend;?&gt;</legend>
                <div class="left_text">&lt;?echo $contact_name;?&gt;</div><span>&lt;input type="text" id="name" size="30"&gt;validation->name;?&gt;">&nbsp;&nbsp;</span><span id='name_status'>&lt;?echo $this->validation->name_error;?&gt;</span><br><br>
                <div class="left_text">&lt;?echo $contact_email;?&gt;</div><span>&lt;input type="text" id="email" size="30"&gt;validation->email;?&gt;">&nbsp;&nbsp;</span><span id='email_status'>&lt;?echo $this->validation->email_error;?&gt;</span><br><br>
                <div class="left_text">&lt;?echo $contact_topic;?&gt;</div>
                <span>
                    <select id="topic">
                        <option value="0" &lt;?php echo $this->validation->set_select('topic', '0'); ?&gt;>&lt;?echo $contact_select_0;?&gt;</option>
                        <option value="1" &lt;?php echo $this->validation->set_select('topic', '1'); ?&gt;>&lt;?echo $contact_select_1;?&gt;</option>
                        <option value="2" &lt;?php echo $this->validation->set_select('topic', '2'); ?&gt;>&lt;?echo $contact_select_2;?&gt;</option>
                        <option value="3" &lt;?php echo $this->validation->set_select('topic', '3'); ?&gt;>&lt;?echo $contact_select_3;?&gt;</option>
                        <option value="4" &lt;?php echo $this->validation->set_select('topic', '4'); ?&gt;>&lt;?echo $contact_select_4;?&gt;</option>
                        <option value="5" &lt;?php echo $this->validation->set_select('topic', '5'); ?&gt;>&lt;?echo $contact_select_5;?&gt;</option>
                        <option value="6" &lt;?php echo $this->validation->set_select('topic', '6'); ?&gt;>&lt;?echo $contact_select_6;?&gt;</option>
                        <option value="7" &lt;?php echo $this->validation->set_select('topic', '7'); ?&gt;>&lt;?echo $contact_select_7;?&gt;</option>
                    </select>&nbsp;&nbsp;
                </span>
                <span id='topic_status'>&lt;?echo $this->validation->topic_error;?&gt;</span><br><br>
                <span>&lt;?echo $contact_message;?&gt;</span><br>&lt;textarea id="message" rows="10" cols="43"&gt;&lt;?php echo $this->validation->message;?&gt;&lt;/textarea&gt;&nbsp;&nbsp;<span id='message_status'>&lt;?echo $this->validation->message_error;?&gt;</span><br><br>
                &lt;input type='submit' id="send" value='&lt;?echo $submit;?&gt;' class='subscribe'&gt;&nbsp;&nbsp;&nbsp;<span id='send_status'></span>
            </fieldset>
        &lt;/form&gt;
#4

[eluser]garymardell[/eluser]
Code:
<div class="left_text">
&lt;?echo $contact_name;?&gt;
</div>
<span>
&lt;input type="text" id="name" size="30"&gt;[b]validation->name;?&gt;[/b]">
&nbsp;&nbsp;
</span>
<span id='name_status'>
&lt;?echo $this->validation->name_error;?&gt;
</span>
<br><br>

I have placed a error in bold. You have failed to start the php tags.And i don't see the purpose of it.
It doesn't it in with the html which is another error.

*added returns in your code for my benefit to read it.

You have the same error on the next line as well.
#5

[eluser]loopyno[/eluser]
well I do have a start tag for the php code. I do not understand why It was removed when I pasted the code here Sad

Here is a link to the code in txt format:
www.fuzzy-4x.com/test.txt
#6

[eluser]wiredesignz[/eluser]
Unless you have some pre-defined error message fields then you only have one choice to display validation errors.
Code:
$this->validation->error_messages;
#7

[eluser]loopyno[/eluser]
than how come this works??? There is no setting for the error messages like in my script. Here is the link www.fuzzy-4x.com/form

Controller:
Code:
&lt;?php

class Form extends Controller {
    
    function index()
    {
        $rules['username']    = "required";
        $rules['password']    = "required";
        $rules['passconf']    = "required";
        $rules['email']        = "required";
        
        $this->validation->set_rules($rules);
        
        $fields['username']    = 'Username';
        $fields['password']    = 'Password';
        $fields['passconf']    = 'Password Confirmation';
        $fields['email']    = 'Email Address';
    
        $this->validation->set_fields($fields);
            
        if ($this->validation->run() == FALSE)
        {
            $this->load->view('myform');
        }
        else
        {
            $this->load->view('formsuccess');
        }
    }
}
?&gt;

View
Code:
&lt;?php echo form_open('form'); ?&gt;

<h5>Username</h5>
&lt;?php echo $this->validation->username_error; ?&gt;
&lt;input type="text" name="username" value="&lt;?php echo $this-&gt;validation->username;?&gt;" size="50" />

<h5>Password</h5>
&lt;?php echo $this->validation->password_error; ?&gt;
&lt;input type="text" name="password" value="&lt;?php echo $this-&gt;validation->password;?&gt;" size="50" />

<h5>Password Confirm</h5>
&lt;?php echo $this->validation->passconf_error; ?&gt;
&lt;input type="text" name="passconf" value="&lt;?php echo $this-&gt;validation->passconf;?&gt;" size="50" />

<h5>Email Address</h5>
&lt;?php echo $this->validation->email_error; ?&gt;
&lt;input type="text" name="email" value="&lt;?php echo $this-&gt;validation->email;?&gt;" size="50" />

<div>&lt;input type="submit" value="Submit" /&gt;&lt;/div>

&lt;/form&gt;
#8

[eluser]wiredesignz[/eluser]
I apologise, you are correct, there are custom messages for each field (a new feature I wasn't aware of).
#9

[eluser]loopyno[/eluser]
np Smile

One thing I saw in my code is that although I enter the correct data $this->validation->run() returns always FALSE Sad

maybe this could be a clue
#10

[eluser]loopyno[/eluser]
anyone? Sad(




Theme © iAndrew 2016 - Forum software by © MyBB