CodeIgniter Forums

Full Version: Form Validation
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3

El Forum

[eluser]invision[/eluser]
Hi,

I'm playing about with some very basic form validaton for a web site.

However, currently when the fields are empty and I submit my form, the message outputted is:

Code:
The cname field is required.

The telno field is required.

The email field is required.

This happens because I use <?php echo $this->validation->error_string; ?> in my form view.
Is there a way I could change this, so instead of saying each error, it would put a red border on the input element required?

Would this be in the Validation class somewhere or the Form Helper?



Controller:
Code:
class Createaccount extends Controller {

  function Createaccount()
  {
      parent::Controller();  
  }
  
  function index()
  {  
  $data[‘page_data’] = $this->MPages->getPageBySlug(‘createaccount’);
  $data[‘title’] = $data[‘page_data’][‘title’];
  $data[‘body’] = $data[‘page_data’][‘body’];
  
  #Validations
  $rules[‘cname’] = “required”;
  $rules[‘telno’] = “required”;
  $rules[‘email’] = “required|valid_email”;

$this->validation->set_rules($rules);
  
  #Input and textarea field attributes
  $data[“cname”] = array(‘name’ => ‘cname’, ‘id’ => ‘cname’);
  $data[“telno”] = array(‘name’ => ‘telno’, ‘id’ => ‘telno’);
  $data[‘email’] = array(‘name’ => ‘email’, ‘id’ => ‘email’);

if ($this->validation->run() == FALSE)
      {
  //$this->load->view(‘form_view’, $data);
  $data[‘main’] = ‘public_createaccount’;
    $this->load->vars($data);  
    $this->load->view(‘template’);
      }
      else
      {
        $fname = $this->input->post(‘fname’);
        $email = $this->input->post(‘email’);
        $comments = $this->input->post(‘comments’);
        
        $message = “$fname ($email) had this to say:\n\n$comments”;
        $this->email->from($email, $fname);
        $this->email->to(‘[email protected]’);
        
        $this->email->subject(‘Account Creation’);
        $this->email->message($message);
        
        $this->email->send();
        
        $this->load->view(‘account_success’);
      }

}
  
}
[b]View:[/b]
[code]
<?php echo $this->validation->error_string; ?>
  <?php echo form_open(‘createaccount/index’); ?>
  <label for=“cname”>Company Name: </label>
&lt;?php echo form_input($cname); ?&gt;
  <label for=“telno”>Telephone No.: </label>
&lt;?php echo form_input($telno); ?&gt;
  <label for=“faxno”>Fax No.</label>
&lt;?php echo form_input($faxno); ?&gt;
  <label for=“email”>E-mail</label>
&lt;?php echo form_input($email); ?&gt;
  &lt;?php echo form_submit(‘submit’, ‘Submit’); ?&gt;
  &lt;?php echo form_close(); ?&gt;



Many thanks for any pointers.

El Forum

[eluser]theshiftexchange[/eluser]
FYI - yes you can do it. I dont have the code in front of me - but you would do something like

if ?php echo $this->validation->error_string; = something
then
do html code showing the red box

and do this for each field...

El Forum

[eluser]invision[/eluser]
Aaaah. That sounds about right.

So this would go in my VIEW file on each element?
e.g.
Code:
&lt;input id="email" if &lt;?php echo $this-&gt;validation->error_string; = 'email' { echo 'style="border:2px solid red"'; } ?&gt; />
Would this be right?

If you did have example code or even a link to an example it would be fantastic.



Thankyou.

El Forum

[eluser]$ilovephp[/eluser]
[quote author="invision" date="1271685207"]Aaaah. That sounds about right.

So this would go in my VIEW file on each element?
e.g.
Code:
&lt;input id="email" if &lt;?php echo $this-&gt;validation->error_string; = 'email' { echo 'style="border:2px solid red"'; } ?&gt; />
Would this be right?

If you did have example code or even a link to an example it would be fantastic.



Thankyou.[/quote]

try this code instead (although, i am not sure):
Code:
&lt;input id="email" &lt;?php if ($this-&gt;validation->email_error != '') { echo 'style="border:2px solid red"'; } ?&gt; />

$this->validation->fieldname_error is the way of individually identifying errors in your fields. Don't forget to add a suffix '_error'.

I hope that works.

El Forum

[eluser]invision[/eluser]
Many thanks for the reply $ilovephp.

Currently I'm using this for generating input fields:
Code:
&lt;?php echo form_input($email); ?&gt;
Would I have to rejig your code above to get it to work correctly?


Thank you.

El Forum

[eluser]invision[/eluser]
The Validation library looks good to me. Hopefully it can resolve this for me. I will post back.

El Forum

[eluser]$ilovephp[/eluser]
[quote author="invision" date="1271686244"]Many thanks for the reply $ilovephp.

Currently I'm using this for generating input fields:
Code:
&lt;?php echo form_input($email); ?&gt;
Would I have to rejig your code above to get it to work correctly?


Thank you.[/quote]

The only thing we need to edit in your input field is the style attribute. In your case, you are using the Form Helper of CI. You may want to consider the following codes:

Code:
$email = array(
              'name'        => 'email',
              'id'          => 'email'
            );
if($this->validation->email_error != '')
{
$email['style'] = 'border:2px solid red';
}

echo form_input($email);

not so sure but why not give it a try... Wink

El Forum

[eluser]invision[/eluser]
Hi again.

Unfortunately it doesn't seem to like that.

Straight away before form submission, I get:
Code:
A PHP Error was encountered
Severity: Notice

Message: Undefined property: email_error

Filename: views/public_createaccount.php

Line Number: 9

El Forum

[eluser]$ilovephp[/eluser]
[quote author="invision" date="1271687656"]Hi again.

Unfortunately it doesn't seem to like that.

Straight away before form submission, I get:
Code:
A PHP Error was encountered
Severity: Notice

Message: Undefined property: email_error

Filename: views/public_createaccount.php

Line Number: 9
[/quote]

Ooops.. i forgot to include this code:
Code:
$fields['email']= 'Email Address';
$this->validation->set_fields($fields);
insert that code right after the setting of rules in your controller file(that is, therefore, before the '$this->validation->run()').

El Forum

[eluser]invision[/eluser]
Way-heeeeeey!!!!

That worked perfectly.

So if I wanted it to take the real value of the email field, how can I do this?

And would I just do this for the other fields on my page, instead of using the array?


Thank you once more for your help and patience.
Pages: 1 2 3