• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to structure this

#1
[eluser]MattKern[/eluser]
Hi

I worked thru the tutorials and while expanding on those I ran into a problem.

This is the blog video tutorial

Code:
function comments()
{
//all the code that pulls the blog comments out of the table
$this->load->view('comment_view',$data);
}

function comment_insert()
{
//code to insert new comments into the table
redirect('blog/comments/'.$_POST['entry_id']);
}

This all works swell but now I am trying to add validation and I am getting all messed up.

I assume I add the actual validation code to the comment_insert function, but then I have to add the display validation messages to the comments function, right? Do I lose my validation data when redirecting from comment_insert to comments?

Also, in comment_view I get a bunch of notices about undefined properties and variables. I assume this is because I have validation code in comment_insert.

My knee jerk reaction is to move everything to the comment function and just check if the form has been submitted before I validate, but I am trying to learn how to write cleaner code.

Thanks

Matt

#2
[eluser]MattKern[/eluser]
OK, I got the error notices worked out. The rules are working and the data is being entered into the table when the data is valid and is redirected when the data is not valid, so that all works.

But, I still am not sure how to get the validation messages to appear in my form when the actual validation occurs in a different function.

Thanks

matt

#3
[eluser]Glen Swinfield[/eluser]
You can't if you are redirecting because validation->error_string is destroyed on redirect.

You should not redirect if validation fails, just set $data again and load comment_view from the insert method.

This means that your comment_view view file would need to include $this->validation->var_name in the value="" attribute of your form fields, and therefore you would need to set $this->validation in the constructor of the controller (or in each individual action) to prevent an "object doesn't exist" error. - When the form is displayed for the first time the fields are empty and no error message, if validation fails the view is included again but this time pre-populated with the previously entered data, and with the error message, the url would be http://www.yoursite.com/controller_name/comment_insert - it would then post to itself and the cycle continues until successful validation causes a redirect.

#4
[eluser]MattKern[/eluser]
Thanks codePat for the answer.

Initially I had tried something like that, but since I am trying to implement a simple blog here and the comment form is at the bottom of the comments page, I need to load the article and comments again in the comment_insert function

Code:
function comments()
{
//all the code that pulls the blog article and comments out of the table
$this->load->view('comment_view',$data);
}

function comment_insert()
{
//validation

//if it fails
//all the code that pulls the blog article and comments out of the table <- duplicated code!
$this->load->view('comment_view',$data);

//if it succeeds
redirect('blog/comments/'.$_POST['entry_id']);
}

Seems like I am completely duplicating my "get my contents" code.

Am I missing something?

Thanks for your time.

Matt

#5
[eluser]Glen Swinfield[/eluser]
Could you call $this->get_my_contents() action from the insert() action if validation fails - so you don't have to duplicate code?

You can call other controller actions from within actions.

#6
[eluser]MattKern[/eluser]
Wow, thanks codepat

A simple answer to a dumb question.

I guess part of learning how frameworks work is when to let the framework take over and when to actually do it yourself.

I did have to duplicate this in my view mode

Code:
$fields['author'] = 'Name';
  $fields['body'] = 'Comment';
  $this->validation->set_fields($fields);


Whats worse? Calling code for no reason or just ignoring/turning off the error notices?

I assume ignoring warnings is better...

Other than that, got it all working fine now.

Thanks again

Matt

#7
[eluser]Glen Swinfield[/eluser]
Quote:Whats worse? Calling code for no reason or just ignoring/turning off the error notices?

Some people don't like using @$unset_var because they feel like its an error they are ignoring. But really it's not. It is a valid coding practice. PHP will always behave in the same way when it reaches and unset variable preceeded by '@' so how is this any different from using any other predictable behaviour in PHP?. Answer: it's not.

When I'm not using CI, and in fact when I'm using checkboxes on forms in CI I use

Code:
@$_POST['form_element']

quite a lot to pre-populate forms if they haven't been sent and It's fine. FYI I usually use
Code:
&lt;input type="checkbox" name="cbox1" value="checked" &lt;?php echo @$_POST['cbox1']; ?&gt; /&gt;
for checkboxes when using CI because I don't really like using the form helpers.

This said though I usually set up my validation class in the constructor, then, when the form is loaded for the first time $validation is initialized but contains no values. This means that for some actions $this->validation->set_fields() is called for no reason so I suppose how sensible this is depends on how often it's called for no reason compared to the amount of code you are removing by calling it? It probably needs assessing on a case by case basis. ;-)


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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