CodeIgniter Forums
add/edit validation with an Object - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: add/edit validation with an Object (/showthread.php?tid=3430)



add/edit validation with an Object - El Forum - 09-30-2007

[eluser]ericsodt[/eluser]
I am running into an issue where I am trying to edit a form (thats for both an add and editing). I am running into an issue with passing my data as an object and was hoping someone might be able to help me out. Here's the code
CONTROLLER
Code:
if(is_numeric(intval($this->uri->segment(3)))){
                 $data['eventObj'] = $this->event->getById($this->uri->segment(3));
                 return $this->load->view('addEditEvent_view', $data);

here is what my form looks like:

addEditEvent_view.php
Code:
<table class="enterCriteria" width="30%">
  <tr>
   <td>Display Name:</td><td>&lt;input type="text" id="displayName" name="displayName" size="25" value="&lt;?=$this-&gt;validation->displayName;?&gt;"  maxlength="50"></td><td class="error">&lt;?=$this->validation->displayName_error; ?&gt; </td>  
...
...

As you can see in the form I am using the validation class, but am stumped in how I can pass my event Object into the view and have the view use it...

Could anyone help me resove this?


add/edit validation with an Object - El Forum - 10-01-2007

[eluser]ericsodt[/eluser]
Can anyone help me out with this? I find it hard to believe that no one uses objects to populate a form... anyone???


add/edit validation with an Object - El Forum - 10-01-2007

[eluser]alpar[/eluser]
you can set up values directly to the validation object. something like: $this->validation->value = $this->event->value; you can use a foreach if you named all values the same, as in the example.


add/edit validation with an Object - El Forum - 10-01-2007

[eluser]ericsodt[/eluser]
[quote author="alpar" date="1191285527"]you can set up values directly to the validation object. something like: $this->validation->value = $this->event->value; you can use a foreach if you named all values the same, as in the example.[/quote]

Where would I do this? I would think in the controller, but where?
Code:
$rules['displayName']         = "required";
            $rules['eventLocation']     = "required";
            $rules['desc']                 = "required";
            $rules['email']             = "required|valid_email";
            $rules['startDate']         = "required";
            $rules['endDate']            = "required";        
            
            $this->validation->set_rules($rules);        
            
            $fields['displayName']         = 'Host Name';
            $fields['eventLocation']     = 'Event Location';
            $fields['desc']             = 'Event Description';
            $fields['email']             = 'Email Address';
            $fields['startDate']        = 'Start Date';
            $fields['endDate']            = 'End Date';

This is how I've set my fields. Using this, can you explain what you mean??


add/edit validation with an Object - El Forum - 10-01-2007

[eluser]Crafter[/eluser]
Pass your object into an array, then the array into the view

Code:
$user->name = 'Tom Jones';
$user->song = 'Pussycat';

$view_data['user'] = $user;

The view loader extracts the array elements as local variables. You can the use them as objects again.
Code:
<h1> Song List </h1>
The singer &lt;?php echo $user->name; ?&gt; sings the song &lt;?php echo $user->song; ?&gt;<br />
<hr />

EDIT:
DISCLAIMER : I I'm no Tom Jones fan. I don't know what came over me!


add/edit validation with an Object - El Forum - 10-02-2007

[eluser]alpar[/eluser]
Code:
$rules['displayName']         = "required";
            $rules['eventLocation']     = "required";
            $rules['desc']                 = "required";
            $rules['email']             = "required|valid_email";
            $rules['startDate']         = "required";
            $rules['endDate']            = "required";        
            
            $this->validation->set_rules($rules);        
            
            $fields['displayName']         = 'Host Name';
            $fields['eventLocation']     = 'Event Location';
            $fields['desc']             = 'Event Description';
            $fields['email']             = 'Email Address';
            $fields['startDate']        = 'Start Date';
            $fields['endDate']            = 'End Date';

            $this->validation->set_fields($fields);
          
            if (empty($_POST))
            {
               // load some defaults
               $this->validation->displayName = $data['eventObj']->...;
            }

and you use just $this->validation->displayName in the view, you dont have to pass $data to it