CodeIgniter Forums
how to make form validation work here ? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: how to make form validation work here ? (/showthread.php?tid=24592)



how to make form validation work here ? - El Forum - 11-13-2009

[eluser]amira_fcis[/eluser]
i tried to make update user information,in case of user didn't fill the required fields the form_avalidation will report an error.here is the problem
when the user didn't fill the required fields nothing happened i donot know why ?!!!!
here is the function

Code:
function updateUser()
          {
            $this->load->database();
            $this->load->library('form_validation');
            $this->load->helper(array('form', 'url'));
            $this->load->model('User');
        
           $userServices=new UserServices();
           $userDAO = new UserDAO();
        
        $this->form_validation->set_rules('username', 'Username', 'required|min_length[5]|max_length[12]');
        $this->form_validation->set_rules('firstname', 'First Name', 'required|min_length[5]|max_length[12]');
        $this->form_validation->set_rules('lastname', 'Last Name', 'required|min_length[5]|max_length[12]');
        $this->form_validation->set_rules('title', 'Title', 'min_length[2]|max_length[5]');
        $this->form_validation->set_rules('email', 'Email', 'required|valid_email');
        $this->form_validation->set_rules('password', 'Password', 'required|matches[confirmpassword]');
        $this->form_validation->set_rules('confirmpassword', 'Password Confirmation', 'required');
        
        $this->form_validation->set_rules('city', 'City', 'required');
        $this->form_validation->set_rules('phoneno', 'Phone No', 'min_length[5]|max_length[12]');
        $this->form_validation->set_rules('cellphoneno', 'Cell Phone No', 'required|min_length[5]|max_length[12]');

            
    if ($this->input->post('submit') && ! $this->form_validation->run())
        {
        
        [b]
        $this->User->setUserID($this->input->post('userid'));
        $data['users']=$userDAO->retrieveUser( $this->User,$this);
        [/b]
        $this->load->view('Edit_form',$data);    
            
        }

note--> the bold code i guess it causes the problem because i tried before without this 2 lines and it worked probably but here i need to retrieve user information again with any errors that user made.
here is the form
Code:
<html>
<head>
<title>Edit Form</title>
</head>
<body>


<?php echo validation_errors(); ?>

<?php echo form_open('EditUser/updateUser'); ?>

<?
if($users)
{
foreach( $users as $user)
         {

?>
<input type="hidden" name="userid" value="<?echo $user->getUserID() ;?>" size="50" />

    <?php echo form_error('username'); ?>

User Name    &lt;input type="text" name="username" value="&lt;?echo $user-&gt;getUserName() ;?&gt;"/><br><br>
    &lt;?php echo form_error('firstname'); ?&gt;
First Name   &lt;input type="text" name="firstname" value="&lt;?echo $user-&gt;getUserFName() ;?&gt;"/><br><br>
    &lt;?php echo form_error('lastname'); ?&gt;
Last Name    &lt;input type="text" name="lastname" value="&lt;?echo $user-&gt;getUserLName() ;?&gt;" /><br><br>
    &lt;?php echo form_error('title'); ?&gt;
Title        &lt;input type="text" name="title" value="&lt;?echo $user-&gt;getUserTitle() ;?&gt;" /><br><br>
    &lt;?php echo form_error('email'); ?&gt;
Email        &lt;input type="text" name="email" value="&lt;?echo $user-&gt;getUserEmail() ;?&gt;" /><br><br>
    &lt;?php echo form_error('password'); ?&gt;
Password     &lt;input type="password" name="password" value="&lt;?echo $user-&gt;getUserPassword() ;?&gt;" /><br><br>
    &lt;?php echo form_error('confirmpassword'); ?&gt;
Confirm Password   &lt;input type="password" name="confirmpassword" value="" /&gt;&lt;br><br>

Address            &lt;input type="text" name="address" value="&lt;?echo $user-&gt;getUserAddress() ;?&gt;"  /><br><br>
    &lt;?php echo form_error('city'); ?&gt;
City               &lt;input type="text" name="city" value="&lt;?echo $user-&gt;getUserCity() ;?&gt;" /><br><br>
    &lt;?php echo form_error('phoneno'); ?&gt;
Phone No           &lt;input type="text" name="phoneno" value="&lt;?echo $user-&gt;getUserPhoneNo() ;?&gt;"/><br><br>
    &lt;?php echo form_error('cellphoneno'); ?&gt;
Cell-Phone No      &lt;input type="text" name="cellphoneno" value="&lt;?echo $user-&gt;getUserCellPhoneNo() ;?&gt;"/><br><br>
&lt;?
}
}
?&gt;

&lt;input type="submit" name="submit" value="Update" /&gt;

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


i'll be soooooooooo greatfull for any help


how to make form validation work here ? - El Forum - 11-13-2009

[eluser]Vikeoar[/eluser]
Shouldn't your validation run look something like:

Code:
if($this->form_validation->run() == FALSE)
    {
        //reload your edit view here is validation fails
    }
    else
    {
        //process your update if the validation is successful
    }


When I'm updating data from a db I have two methods in my controller "edit": this pulls the data into my initial edit view. Then "update" in my controller does the work on updating the database. "update" contains my validation code. I also use the set_value() function to re-set the edit page if there is a validation error. Check here: repopulating forms

I don't see that you're using that anywhere in your form.


how to make form validation work here ? - El Forum - 11-13-2009

[eluser]amira_fcis[/eluser]
thanks Vikeoar for ur concern,
i think
Code:
if (!$this->form_validation->run())
is the same as if i do that::
Code:
if ($this->form_validation->run()==FALSE)