• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Form validation and set_value repopulation problem

#1
[eluser]KSShooter[/eluser]
Just working through the basic tutorial within the CI documentation. I am using CI 2.1.2, freshly downloaded today. I followed the basic tutorial, and then started adding form validation to test that, then form re-population.

In my form I have:

Code:
<h2>Create a news item</h2>

&lt;?php echo form_open('news/create') ?&gt;

<label for="title">Title</label>
&lt;input id="title" type="input" name="title" value="&lt;?php echo set_value('title'); ?&gt;" size="128" /&gt;
&lt;?php echo form_error('title'); ?&gt;

<br>
    <label for="ntext">Text</label>
    &lt;textarea name="ntext" &lt;?php echo set_value('ntext'); ?&gt; &gt;&lt;/textarea>  
    &lt;?php echo form_error('ntext'); ?&gt;
<p>
&lt;input type="submit" name="submit" value="Create news item" /&gt;

&lt;/form&gt;

In my controller I have:


Code:
class News extends CI_Controller {
public function __construct() {
  parent::__construct();
  $this->load->model('news_model');
}
...

public function create()
{
  $this->load->helper('form');
  $this->load->library('form_validation');
  $this->form_validation->set_error_delimiters('<strong><font color="red">', '</font></strong>');
  
  $data['title'] = 'Create a news item';
//  $this->form_validation->set_rules('title', 'Title', 'trim|required|min_length[5]|max_length[128]|xss_clean|callback_title_check');
  $this->form_validation->set_rules('title', 'Title', 'trim|required|callback_title_check');
  $this->form_validation->set_rules('ntext', 'NText', 'trim|required|callback_ntext_check');
  
  if ($this->form_validation->run() == FALSE) {
   $this->load->view('templates/header', $data);
   $this->load->view('news/create');
   $this->load->view('templates/footer', $data);
  }
  else {
   $this->news_model->set_news();
   $this->load->view('news/success');
  }  
}

public function title_check($str)
{
  if ($str == 'tester') {
   $this->form_validation->set_message('title_check', 'The %s field cannot be "tester"');
   return FALSE;
  }
  else {
   return TRUE;
  }
}

public function ntext_check($str)
{
  if ($str == 'tester') {
   $this->form_validation->set_message('ntext_check', 'The %s field cannot be "tester"');
   return FALSE;
  }
  else {
   return TRUE;
  }
}
...

At this point - everything works as expected.
1. Will post a new record into the database2
2. Will error out and display errors (I modified the error block to have errors by each field)
3. Will redisplay the screen showing the errors

HOWEVER, any validation error that is encountered will not redisplay the 'ntext' field (the last one on the screen). Anything entered there just disappears. I HAVE created a callback validation, and can enter the trigger error, and it will receive the appropriate error as expected, so I know the form is passing the data in. However, it never re-populates the field. Is this something because the field is a "textarea" instead of an "input"? Or is there something else going on here? I know there's no "value" attribute for a textarea field, so I've followed some web examples that say "just echo it out". That doesn't seem to work here.

So - what am I doing wrong?

#2
[eluser]CroNiX[/eluser]
Code:
&lt;textarea name="ntext"&gt;&lt;?php echo set_value('ntext'); ?&gt;&lt;/textarea&gt;

The value for textarea goes between the open and close textarea tags. If you look at your HTML source you will probably see it's putting it within the open tag itself, which isn't correct.

#3
[eluser]KSShooter[/eluser]
[quote author="CroNiX" date="1347319201"]
Code:
&lt;textarea name="ntext"&gt;&lt;?php echo set_value('ntext'); ?&gt;&lt;/textarea&gt;

The value for textarea goes between the open and close textarea tags. If you look at your HTML source you will probably see it's putting it within the open tag itself, which isn't correct.[/quote]

I apologize for such a stupid request. I'd been staring at it for a couple of hours and simply hadn't seen that problem. One of those things where you've banged your head on the wall for a while, ask someone to come over and look at it, and as soon as they get there , you say "never mind" because you've finally seen it. In this case - I never got to "never mind".

Thanks for the reply - off to see what other trouble I can run into.

#4
[eluser]CroNiX[/eluser]
It's not stupid. We all overlook simple things like that sometimes. Glad to help Smile


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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