Wednesday, September 29, 2010

Re: Redirection based on validation results.

Well if you want to break MVC, you could instantiate the Controller
class and use the redirect function in your model, e.g.,

App::Import('Core', 'Controller');
$controller = new Controller();
$controller->redirect('http://mysite.com/playerPage');

On Sep 29, 1:00 pm, Anthony <anthony.c.fra...@gmail.com> wrote:
> > It sounds like you are saving a record right? When you are done with
> > your $this->Model->save(), the id of the record will be in $this-
>
> > >Model->id and you can redirect from there in the controller.
>
> Correct, this part I have done.  When a new player is saved the
> redirect takes you to the action that displays information about the
> single user based on the newly created ID.
>
>
>
> > Why would you want to redirect during validation? Validation is
> > generally for the saving and updating of records, are you trying to
> > interrupt that? Or am I misreading your question?
>
> It could be a mix of miss-reading and me not know exactly the path I
> need to use to accomplish my goal.  I'm basically checking that the
> player about to be added is unique to the server. If they exist on the
> server instead of adding another I want to redirect to the existing
> users page, the one found by the validation function.   In the
> validation I do a query with conditions setting the first_name,
> last_name, and server_id.  If the user exists on that server I don't
> want to add another one, and I don't want to display an error, instead
> I want to take them to my action for viewing players.
>
>         var $validate = array(
>                 'first_name' => array(
>                         'notempty' => array(
>                                 'rule' => array('notempty'),
>                         ),
>                 ),
>                 'last_name' => array(
>                         'notempty' => array(
>                                 'rule' => array('notempty'),
>                         ),
>                 ),
>                 'server_id' => array(
>                                 'rule' => array('onePlayerPerServer'),
>                                 'message' => "User already exists on that server.",
>                 ),
>         );
>
>         function onePlayerPerServer() {
>
>                 $player= $this->find('first', array(
>                         'conditions' => array(
>                                 'Player.first_name' => $this->data['Player']['first_name'],
>                                 'Player.last_name' => $this->data['Player']['last_name'],
>                                 'Player.server_id' => $this->data['Player']['server_id'],
>                         ),
>                 ));
>
>                 if (!empty($player)) {
>                         return false; //player doesn't exist.
>                 }
>                 else {
>                         return $player['Player']['id'];  //player found, return their ID
>                 }
>         }
>
> Since validate is a success on 0 and fails on anything else I was
> hoping to pass back the id that was found. But I'm not sure if my
> thought process will work in the way I'm wanting.
>
> Does this make more sense?
>
> On Sep 29, 11:10 am, "j.blotus" <j.blo...@gmail.com> wrote:
>
>
>
> > It sounds like you are saving a record right? When you are done with
> > your $this->Model->save(), the id of the record will be in $this-
>
> > >Model->id and you can redirect from there in the controller.
>
> > Why would you want to redirect during validation? Validation is
> > generally for the saving and updating of records, are you trying to
> > interrupt that? Or am I misreading your question?
>
> > On Sep 29, 11:27 am, Anthony <anthony.c.fra...@gmail.com> wrote:
>
> > > Each server has many players and players belong to a server.
>
> > > Each player may have the same first and last name but only one per
> > > server.
>
> > > I have a custom validation that is working correctly when the form is
> > > submitted.
>
> > > My thoughts were to kill two birds with one stone and have the
> > > validation function return 0 for nothing found or the player id if
> > > found.  Everything works perfectly with this setup for simply
> > > validating when the form is submitted.
>
> > > I've seen the handbook on redirecting from the controller based on
> > > over all success.  However, I want to redirect to the players page if
> > > a user was found. Is it possible to get the value returned by a
> > > validation function or will I just have to run the query again?
>
> > > If that's the case, I should check the validation at the controller,
> > > read the returned validation results, and check it for my custom rule
> > > failure?
>
> > > Is this how I should go about this or is my focus preventing me from
> > > seeing the bigger picture? Thanks for any assistance and insights.

Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions.

You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en

No comments: