CodeIgniter Forums
set_value not working? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: set_value not working? (/thread-43134.html)



set_value not working? - El Forum - 07-01-2011

[eluser]MTWIGG[/eluser]
I am creating my first form based on the example http://ellislab.com/codeigniter/user-guide/libraries/form_validation.html (my autoload has $autoload['helper'] = array('url'); which is the main difference)

I have just one text field and whilst the validation works, set_value() is not working. The view is...

<?php echo validation_errors(); ?>
<?php echo form_open('forgotten'); ?>
Please enter your email. Your password will be sent to you.<br/>
Email Address: &lt;input type="text" name="email" value="" size="100" value="&lt;?php echo set_value('email'); ?&gt;" /&gt;
<div>&lt;input type="submit" value="Submit" /&gt;&lt;/div>
&lt;/form&gt;

... and the controller is ...

&lt;?php
class Forgotten extends CI_Controller
{
function Forgotten()
{
parent::__construct();
$this->load->helper('form');
}

public function index()
{
$this->load->library('form_validation');
$this->form_validation->set_rules('email', 'Email Address', 'required|valid_email');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('forgotten_body');
}
else
{
$this->load->view('forgottensent_body');
}
}
}
?&gt;


set_value not working? - El Forum - 07-01-2011

[eluser]Henrique Luz[/eluser]
Hi,

Please, show the error here.


set_value not working? - El Forum - 07-01-2011

[eluser]MTWIGG[/eluser]
Sorry, I should have been clearer.

There is no PHP etc. error message. When the form displays the validation error 'The Email Address field must contain a valid email address' the email text field is always blank.


set_value not working? - El Forum - 07-01-2011

[eluser]Henrique Luz[/eluser]
The error occurs because you're validating the data before sending them to the view for the controller.

Then the email field is always blank, and thus, the method form_validation->run always returns false.
Try something like that in your controller:
Code:
&lt;?php
class Forgotten extends CI_Controller
{
  function Forgotten()
  {
      parent::__construct();
      $this->load->helper('form');
  }

  public function index(){
      $this->load->view('help/forgottensent_body');  
  }
  
  public function validate(){
        $this->load->library('form_validation');        
        $this->form_validation->set_rules('email', 'Email Address', 'required|valid_email');
          
      
        if($this->form_validation->run() == FALSE)
        {
            //ERROR
        }
        else{
          $this->load->view('forgottensent_body');
        }    
              
     }
}
?&gt;

And your view:

Code:
&lt;form action='validate' method='post'&gt;
    Please enter your email. Your password will be sent to you.<br>
    Email Address:
    &lt;input type="text" name="email" value="" size="100" value=”&lt;?php echo set_value('email'); ?&gt;” /&gt;
    <div>
    &lt;input type="submit" value="Submit" /&gt;
    </div>
&lt;/form&gt;



set_value not working? - El Forum - 07-01-2011

[eluser]MTWIGG[/eluser]
[quote author="Henrique Luz" date="1309545631"]The error occurs because you're validating the data before sending them to the view for the controller.[/quote]

Why should validating the data affect the data? The order of the functions in index() that I have used matches the demo at http://ellislab.com/codeigniter/user-guide/libraries/form_validation.html

[quote author="Henrique Luz" date="1309545631"]Then the email field is always blank, and thus, the method form_validation->run always returns false.[/quote]

form_validation->run does not always return false, it only returns false when the email is invalid, if it is valid then it returns true.


set_value not working? - El Forum - 07-01-2011

[eluser]Henrique Luz[/eluser]
Look,

When you set
Code:
$this->form_validation->set_rules('email', 'Email Address', 'REQUIRED|valid_email')
The email field must always be filled before validation.

When you call validation inside index function, this field is still blank.

So the validation returns FALSE, in this case. Ok?


set_value not working? - El Forum - 07-07-2011

[eluser]CodeIgniteMe[/eluser]
The error is in your HTML, notice you have two "value" attributes in your email field. The browser only recognizes the first attribute and ignores any duplicate. remove the first "value" attribute and try again


set_value not working? - El Forum - 07-07-2011

[eluser]MTWIGG[/eluser]
Ooops! Thanks for finding my silly mistake!


set_value not working? - El Forum - 07-07-2011

[eluser]CodeIgniteMe[/eluser]
No prob.