Welcome Guest, Not a member yet? Register   Sign In
Form binding from model + set_value on postback
#1

[eluser]moopa[/eluser]
I have a form that contains user profile data which is bound from a model.

On page load, I wish to bind name, birthday, town to the relevant form fields. On postback I wish to maintain state from the posted values instead.

Having played with set_value etc, I cannot figure out an elegant solution to this and wondered if anyone with prior experience could offer some advice on the best way to achieve this.

Thanks in advance,

Moopa
#2

[eluser]Colin Williams[/eluser]
Controler
Code:
function edit_profile($id)
{
   $data['user'] = $this->user_model->get($id);

   // Form validation in here somewhere

   $this->load->view('edit_profile', $date);
}

View
Code:
<input name="username" value="<?php print set_value('username', $user->username) ?>" />
#3

[eluser]moopa[/eluser]
[quote author="Colin Williams" date="1259635367"]Controler
Code:
function edit_profile($id)
{
   $data['user'] = $this->user_model->get($id);

   // Form validation in here somewhere

   $this->load->view('edit_profile', $date);
}

View
Code:
<input name="username" value="<?php print set_value('username', $user->username) ?>" />
[/quote]

Thanks for your response. I am doing a similar thing to your post but username is only set from the post data on post back. Are you proposing that the value of username is bound from $data['username'] and set on page load?

Thank you,

Moopa
#4

[eluser]Colin Williams[/eluser]
The variable $user in the view is provided via the controller and is fetched from the user model. Your process for getting the data and how it is structured probably differs, but you should be able to extrapolate from the basic example using your own APIs
#5

[eluser]moopa[/eluser]
[quote author="Colin Williams" date="1259636067"]The variable $user in the view is provided via the controller and is fetched from the user model. Your process for getting the data and how it is structured probably differs, but you should be able to extrapolate from the basic example using your own APIs[/quote]

Hello again.

Your example is quite similar to mine. I think my question probably needs refactoring Wink
Of recent years, I have been using .net and I am used to using the IsPostback method of the page class. e.g. if(!Page.IsPostback) { // Bind data }

I may misunderstand you, but on every postback via your proposed method, will I not be requesting user data from the model? I guess my question should be, is there an elegant way to handle page state or should I be posting a hidden field to determine postback? (Sorry. It has been a while since I used php).

Thanks
#6

[eluser]Colin Williams[/eluser]
Why are you trying to "determine postback"? That logic happens with the set_value() function.

Quote:Permits you to set the value of an input form or textarea. You must supply the field name via the first parameter of the function. The second (optional) parameter allows you to set a default value for the form.

Quote:I may misunderstand you, but on every postback via your proposed method, will I not be requesting user data from the model?

Yes, you will be getting the data on every request with my method. To avoid that, you could add a condition in the controller.

Code:
function edit_profile($id)
{
   if ( ! count($_POST))
   {
      $data['user'] = $this->user_model->get($id);
   }

   // Form validation in here somewhere

   $this->load->view('edit_profile', $date);
}

But that would introduce issues accessing the variable in the view. I see no reason not to get the user object on every request.
#7

[eluser]moopa[/eluser]
[quote author="Colin Williams" date="1259637430"]
Yes, you will be getting the data on every request with my method. To avoid that, you could add a condition in the controller.

Code:
function edit_profile($id)
{
   if ( ! count($_POST))
   {
      $data['user'] = $this->user_model->get($id);
   }

   // Form validation in here somewhere

   $this->load->view('edit_profile', $date);
}

But that would introduce issues accessing the variable in the view. I see no reason not to get the user object on every request.[/quote]

Thanks for your continued input. This was the answer I was after as I wanted a comparative to what I am used to doing in M$ world and if it was valid using Codeigniter. I can see now that the slight performance hit from hitting the DAL is probably the way to go here.

Best regards.




Theme © iAndrew 2016 - Forum software by © MyBB