Re: Form from another model in a view

2011-11-14 Thread Diogo
That's exactly my line of thinking. I might use a comments controller
if it's being used on more than one model. Other than that, I think
it's not worth all the work. Thanks!

On Nov 14, 2:32 pm, phpMagpie  wrote:
> I don't think there is a right and wrong way to be honest, I have done this
> in the Comment controller and in my BlogPost controller, if comments are
> used across many models then probably best to centralize logic in comments
> controller.
>
> I don't worry about field by field validation errors for comments because
> there are only 2-3 fields, instead I use:
> if ($this->BlogPost->Comment->save($this->data)) {
>   $this->Session->setFlash('Comment added.', 'default', null,
> 'comment');} else {
>
>   $this->Session->setFlash('Comment not added, correct errors and
> resubmit.', 'default', array('class'=>'error'), 'comment');
>
> }
>
> And echo $this->Session->flash('comment'); in the view
>
> HTH, Paul.

-- 
Our newest site for the community: CakePHP Video Tutorials 
http://tv.cakephp.org 
Check out the new CakePHP Questions site http://ask.cakephp.org and help others 
with their CakePHP related questions.


To unsubscribe from this group, send email to
cake-php+unsubscr...@googlegroups.com For more options, visit this group at 
http://groups.google.com/group/cake-php


Re: Form from another model in a view

2011-11-14 Thread phpMagpie
I don't think there is a right and wrong way to be honest, I have done this 
in the Comment controller and in my BlogPost controller, if comments are 
used across many models then probably best to centralize logic in comments 
controller.

I don't worry about field by field validation errors for comments because 
there are only 2-3 fields, instead I use:
if ($this->BlogPost->Comment->save($this->data)) {
  $this->Session->setFlash('Comment added.', 'default', null, 
'comment');
} else {
  $this->Session->setFlash('Comment not added, correct errors and 
resubmit.', 'default', array('class'=>'error'), 'comment');
}

And echo $this->Session->flash('comment'); in the view 

HTH, Paul.

-- 
Our newest site for the community: CakePHP Video Tutorials 
http://tv.cakephp.org 
Check out the new CakePHP Questions site http://ask.cakephp.org and help others 
with their CakePHP related questions.


To unsubscribe from this group, send email to
cake-php+unsubscr...@googlegroups.com For more options, visit this group at 
http://groups.google.com/group/cake-php


Form from another model in a view

2011-11-14 Thread Diogo
So I'm trying to extend the Blog tutorial adding some comments:

Post hasMany Comments

I want to display the add comment form in the same view as the 'post
view'. Thing is I don't know the best way to get this approach. I
thought about three ways:

Creating a function in Comments Controller to handle the data.
Creating a function in Post Controller to handle the data.
Deal with the data in the same function that deals with the post
views.
The main problem with the two first 'solutions' is that the validation
errors doesn't show up in the form unless I do some messy hacking of
saving the invalidated field in a session variable and then parsing
the variable on the beforeFilter callback, like this:

function beforeFilter () {
if ($this->Session->check('comment_error')) {
$this->Post->Comment->validationErrors = $this->Session-
>read('comment_error');
$this->Session->delete('comment_error');
}
}

What I basically do is adapt the invalidated fields to the actual view
and allow it to show properly. This works really well, but it seems so
ugly to me. What would be the best approach?

Another related question: should a controller reflect a view? I mean
on that example, I thought about only having a Comment Model and
dealing with all the data in the controller where's the form to add a
comment (even though it's in the Post Controller).

-- 
Our newest site for the community: CakePHP Video Tutorials 
http://tv.cakephp.org 
Check out the new CakePHP Questions site http://ask.cakephp.org and help others 
with their CakePHP related questions.


To unsubscribe from this group, send email to
cake-php+unsubscr...@googlegroups.com For more options, visit this group at 
http://groups.google.com/group/cake-php