Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 689921
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T02:22:57+00:00 2026-05-14T02:22:57+00:00

I’m new to cakephp and trying to write a simple app with it, however

  • 0

I’m new to cakephp and trying to write a simple app with it, however I’m stuck with some form validation issues.

I have a model named “Person” which hasMany “PersonSkill” objects. To add a “PersonSkill” to a person, I have set it up to call a url like this:

http://localhost/myapp/person_skills/add/person_id:3

I have been passing through the person_id because I want to display the name of the person we are adding the skills for.

My issue is if the validation fails, the person_id parameter is not persisted to the next request, so the person’s name is not displayed.

The add method on the controller looks like this:

function add() {        
    if (!empty($this->data)) {          
        if ($this->PersonSkill->save($this->data)) {
            $this->Session->setFlash('Your person has been saved.');
            $this->redirect(array('action' => 'view', 'id' => $this->PersonSkill->id));
        }       
    } else {
        $this->Person->id = $this->params['named']['person_id'];
        $this->set('person', $this->Person->read());        
    }
}   

In my person_skill add.ctp I set a hidden field which holds the person_id, eg:

echo $form->input('person_id', array('type'=>'hidden','value'=>$person['Person']['id']));

Is there a way to persist the person_id url parameter when form validation fails, or is there a better way to do this that I’m missing completely?

Any advice would be greatly appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T02:22:57+00:00Added an answer on May 14, 2026 at 2:22 am

    The FormHelper::create() method allows you to configure the URL in the action attribute of the form tag it creates.

    You want to make sure the current URL is used so you are posting to the same URL and if validation fails, the person_id named parameter is still there.

    Try something like this:

    echo $form->create('PersonSkill', array('url' => $this->params['named']));
    

    CakePHP should merge the named params, i.e. array(‘person_id’ => 3) with the current controller and action and return the same URL as you are on.

    By the way, you also want to read the person details and set them available in the view if $this->data is not empty, so I’d lose the else statement in the controller and just have:

    function add() {        
        if (!empty($this->data)) {          
            if ($this->PersonSkill->save($this->data)) {
                $this->Session->setFlash('Your person has been saved.');
                $this->redirect(array(
                    'action' => 'view',
                    'id' => $this->PersonSkill->id
                ));
            }       
        }
        $this->Person->id = $this->params['named']['person_id'];
        $this->set('person', $this->Person->read());        
    }   
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to loop through a bunch of documents I have to put
I have some data like this: 1 2 3 4 5 9 2 6
I want use html5's new tag to play a wav file (currently only supported
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Seemingly simple, but I cannot find anything relevant on the web. What is the
this is what i have right now Drawing an RSS feed into the php,
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.