Form Validation

#11
[eluser]$ilovephp[/eluser]
[quote author="invision" date="1271688433"]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.[/quote]

What do you mean get the real value of the email field? Did you mean the $_POST in PHP? Or, are you planning to repopulate the fields after error occurred upon submission of the form?

to retrieve the data submitted by a user, we use $this->input->post('field_name');

to repopulate the field with the data supplied by the user, you may use $this->validation->field_name.

So, in applying the $this->validation->field_name, we add something to the code i just gave you

Code:
$email = array
         ('name'=> 'email',
          'id' => 'email',
          'value'=>$this->validation->email
         );

echo form_input($email);

you're welcome, by the way

#12
[eluser]invision[/eluser]
Oh wow, you're a legend sir.
That's exactly what I'm after. I'm off to spend the afternoon rejigging this form. If you're ever in Scotland, have a beer/refreshment of your choice, on me.

#13
[eluser]invision[/eluser]
Back again Smile

Yes, I'd need to store the form value in $email so I can email it later in the script.
I'd also want to store it in the field too.

Just to confirm, my Controller would have:

Code:
...
$fields['email'] = array
         ('name'=> 'email',
          'id' => 'email',
          'value'=>$this->validation->email
         );
$this->validation->set_fields($fields);
...

and my View would have:
Code:
<?php
  if($this->validation->email_error != '')  {
    $email['style'] = 'border:2px solid red';
  }
  ?>
  
  <p><label for="email">E-mail</label><br />&lt;?php echo form_input($email); ?&gt;</p>

Does this make sense?



Thanks again for your help

#14
[eluser]invision[/eluser]
Hi,

Apologies for the barriage of posts here.

I've decided to try it out on a simpler bit of code.

view
Code:
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;My Form&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;

&lt;?php echo validation_errors('<div class="error">', '</div>'); ?&gt;

&lt;?php echo form_open('form'); ?&gt;
  
<h5>Username</h5>
&lt;input type="text" name="cname" value="&lt;?php echo set_value('cname'); ?&gt;" size="50" /&gt;
&lt;?php echo form_error('cname', '<div class="error">', '</div>'); ?&gt;

<h5>Password</h5>
&lt;input type="text" name="password" value="&lt;?php echo set_value('password'); ?&gt;" size="50" /&gt;

<h5>Password Confirm</h5>
&lt;input type="text" name="passconf" value="&lt;?php echo set_value('passconf'); ?&gt;" size="50" /&gt;

<h5>Email Address</h5>
&lt;input type="text" name="email" value="&lt;?php echo set_value('email'); ?&gt;" size="50" /&gt;

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

&lt;/form&gt;

&lt;/body&gt;
&lt;/html&gt;

controller
Code:
&lt;?php

class Form extends Controller {
    
    function index()
    {
        $this->load->helper(array('form', 'url'));
        
        $this->load->library('form_validation');
    $this->form_validation->set_error_delimiters('<div class="error">', '</div>');
            
        $this->form_validation->set_rules('cname', 'Company Name', 'required');
        $this->form_validation->set_rules('password', 'Password', 'required');
        $this->form_validation->set_rules('passconf', 'Password Confirmation', 'required');
        $this->form_validation->set_rules('email', 'Email', 'required');
            
        if ($this->form_validation->run() == FALSE)
        {
            $this->load->view('myform');
        }
        else
        {
            $this->load->view('formsuccess');
        }
    }
}
?&gt;


What is the best way to place a red border around any invalid fields?

#15
[eluser]Bart v B[/eluser]
Youre raping CI Wink

There is no need to do that in that way..

Just add an form_error() where you want to echo the error..

Code:
<p>
    &lt;?php echo form_error('email');?&gt;
    <label for="email">E-mail</label><br />&lt;?php echo form_input($email); ?&gt;
  </p>


in youre controller:
Code:
$this->form_validation->set_rules('email', 'email', 'trim|required|valid_email|strip_tags');
$this->form_validation->set_error_delimiters('<div class="error">','</div>');

And at last you want some color on the error that were doing with CSS:
[code]
.error {
color:#ff0000;
}
[code]

That's all Smile

#16
[eluser]Bart v B[/eluser]
i was posting before i saw youre last repley.

Just add in youre CSS

Code:
.error {
border:1px; dashed color:#ff0000;
}

#17
[eluser]invision[/eluser]
Thanks for the reply kind gent.

Aaaah. Yes, but will that add the border to the field itself?

Sorry, I didn't mean to attack CI in that way. I'm new here. I thought she said she was cool with it Wink

#18
[eluser]Bart v B[/eluser]
[quote author="invision" date="1271699439"]Thanks for the reply kind gent.

Aaaah. Yes, but will that add the border to the field itself?

Sorry, I didn't mean to attack CI in that way. I'm new here. I thought she said she was cool with it Wink[/quote]

Hmmm.. i think that you must do then some magic with javascript.
Need to go now, i ll try to do that later..

#19
[eluser]invision[/eluser]
Cool man.

Appreciate.

In the mean time, I'll clean up all the other bits of the form Smile

#20
[eluser]$ilovephp[/eluser]
[quote author="Bart v B" date="1271698992"]i was posting before i saw youre last repley.

Just add in youre CSS

Code:
.error {
border:1px; dashed color:#ff0000;
}
[/quote]

You're Right! Why I haven't think this kind of solution... maybe i am too carried with the power of CI... hehe


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.