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 688945
In Process

The Archive Base Latest Questions

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

I am having problems getting validation to work for a form built using Zend_Form.

  • 0

I am having problems getting validation to work for a form built using Zend_Form.

The idea is this: I have two dropdown. One is a list of players. The other is a list of free agents who play the same position as the player. I am using an onChange javascript callback to run some Ajax code that replaces the free agent list dropdown with a new one at the position of the player they’ve selected from the player dropdown.

Now, perhaps this is the wrong way, but I built the form by creating an instance of Zend_Form and then creating all these setX methods that add elements to the form. My reasoning was that I wanted to display certain elements in specific places on the page, not just output $this->form on my template.

The problem appears to be when I get the form post back, the validator seems to not know about the validation rule I set up for the free agent drop down. Here’s some relevant code to look at. I’m a relative ZF n00b so feel free to tell me I am not doing things the ZF way if it leaps out at you.

The action in the controller:

public function indexAction()
{
    if ($this->getRequest()->isPost()) {
        $form = new Baseball_Form_Transactions();

        if ($form->isValid($this->_request->getPost())) {
            $data = $this->_request->getPost();
            $leagueInfo = Doctrine::getTable('League')->findOneByShortName($data['shortLeagueName'])->toArray();

            // Create the request top drop an existing player       
            $transactionInfo = array(
                'league_id' => $leagueInfo['id'],
                'team_id' => $data['teamId'],
                'player_id' => $data['players'],
                'type' => 'drop',
                'target_team_id' => 0,
                'transaction_date' => date('Y-m-d H:m:s')
            );
            $transaction = new Transaction();
            $transaction->fromArray($transactionInfo);
            $transaction->save();

            // Now we do the request to add a player
            $transactionInfo['team_id'] = 0;
            $transactionInfo['player_id'] = $data['freeAgents'];
            $transactionInfo['target_team_id'] = $data['teamId'];
            $transactionInfo['type'] = 'add';
            $transaction = new Transaction();
            $transaction->fromArray($transactionInfo);
            $transaction->save();
            $this->_flashMessenger->addMessage('Added transaction');
        }
    }

    $options = array(
        'teamId' => $this->teamId,
        'position' => 'C',
        'leagueShortName' => $this->league
    );

    $this->transactionForm->setMyPlayers($options);
    $this->transactionForm->setFreeAgents($options);
    $this->transactionForm->setTeamId($options);
    $this->transactionForm->setShortLeagueName($options);
    $this->view->transactionForm = $this->transactionForm;
    $this->view->messages = $this->_flashMessenger->getMessages();
    $transaction = new Transaction();
    $this->view->transactions = $transaction->byTeam($options);
}

Next we have the form itself

public function setMyPlayers($options)
{
    $data = Doctrine::getTable('Team')->find($options['teamId']);
    $players = array();

    foreach ($data->Players->toArray() as $player) {
        $players[$player['id']] = "{$player['position']} - {$player['first_name']} {$player['last_name']}";
    }

    $playersSelect = new Zend_Form_Element_Select(
        'players', 
        array(
            'required' => true,
            'label' => 'Players',
            'multiOptions' => $players,
        )
    );

    $this->addElement($playersSelect);
}

public function setFreeAgents($options)
{
    $q = Doctrine_Query::create()
        ->select('CONCAT(p.first_name, " ", p.last_name) as full_name, p.id, p.position')
        ->from('Player p')
        ->leftJoin('p.Teams t')
        ->leftJoin('t.League l ON l.short_name = ?', $options['leagueShortName'])
        ->where('t.id IS NULL')
        ->andWhere('p.position = ?', $options['position'])
        ->orderBy('p.last_name');
    $q->setHydrationMode(Doctrine_Core::HYDRATE_ARRAY);
    $data = $q->execute();
    $freeAgents = array();

    foreach ($data as $player) {
        $freeAgents[$player['id']] = $player['full_name'];
    }

    $freeAgentsSelect = new Zend_Form_Element_Select(
        'freeAgents',
        array(
            'label' => 'Free Agents',
            'multiOptions' => $freeAgents,
            'size' => 15
        )
    );
    $freeAgentsSelect->setRequired(true);
    $this->addElement($freeAgentsSelect);
}

public function setShortLeagueName($options)
{
    $shortLeagueNameHidden = new Zend_Form_Element_Hidden(
        'shortLeagueName',
        array('value' => $options['leagueShortName'])
    );
    $this->addElement($shortLeagueNameHidden);
}

public function setTeamId($options)
{
    $teamIdHidden = new Zend_Form_Element_Hidden(
        'teamId',
        array('value' => $options['teamId'])
    );
    $this->addElement($teamIdHidden);
}

There is no init or __construct() method in the form.

My problem seems simple enough: reject the form contents as invalid if they have not selected someone from the free agent list. Right now, it sails through as valid. I’ve spent some considerable time searching online for an answer, and haven’t been able to find it.

Thanks in advance for any help.

  • 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:16:55+00:00Added an answer on May 14, 2026 at 2:16 am

    You need to set up the form’s fields every request. try this:

    public function indexAction()
    {
        $form = new Baseball_Form_Transactions();
        $options = array(
            'teamId' => $this->teamId,
            'position' => 'C',
            'leagueShortName' => $this->league
        );
    
        $form->setMyPlayers($options);
        $form->setFreeAgents($options);
        $form->setTeamId($options);
        $form->setShortLeagueName($options);
        $this->transactionForm = $form;
    
        if ($this->getRequest()->isPost()) {
    
            if ($form->isValid($this->_request->getPost())) {
                $data = $form->getValues();
                $leagueInfo = Doctrine::getTable('League')->findOneByShortName($data['shortLeagueName'])->toArray();
    
                // Create the request top drop an existing player       
                $transactionInfo = array(
                    'league_id' => $leagueInfo['id'],
                    'team_id' => $data['teamId'],
                    'player_id' => $data['players'],
                    'type' => 'drop',
                    'target_team_id' => 0,
                    'transaction_date' => date('Y-m-d H:m:s')
                );
                $transaction = new Transaction();
                $transaction->fromArray($transactionInfo);
                $transaction->save();
    
                // Now we do the request to add a player
                $transactionInfo['team_id'] = 0;
                $transactionInfo['player_id'] = $data['freeAgents'];
                $transactionInfo['target_team_id'] = $data['teamId'];
                $transactionInfo['type'] = 'add';
                $transaction = new Transaction();
                $transaction->fromArray($transactionInfo);
                $transaction->save();
                $this->_flashMessenger->addMessage('Added transaction');
            }
        }
    
        $this->view->transactionForm = $form;
        $this->view->messages = $this->_flashMessenger->getMessages();
        $transaction = new Transaction();
        $this->view->transactions = $transaction->byTeam($options);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 445k
  • Answers 445k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I see no reason as to why adding \s to… May 15, 2026 at 6:47 pm
  • Editorial Team
    Editorial Team added an answer jQuery is for client-side Javascript. You grab POST values with… May 15, 2026 at 6:47 pm
  • Editorial Team
    Editorial Team added an answer Here is how I accomplished it in the end: Private… May 15, 2026 at 6:47 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.