2008年9月29日星期一

Re: [fw-mvc] Problem with Zend_Form and ajax select box.

I had the same problem a while back. The solution is to disable the
array vlidator in your first select:

$selectOne->setRegisterInArrayValidator(false)


Hope that helps,


Ernesto

On Mon, Sep 29, 2008 at 1:43 PM, Gabriel Baez <sheik316@gmail.com> wrote:
> I have a weird problem with my Zend Form,
>
> I have two select boxes, the second box is empty, I have a java script
> that populates the second select box when a option is selected on the
> first box.
> When I submit the form, zend form is giving an error that a value was not
> selected for the second box.
>
>
> This is what I see on FireBug when I submit the ajax form: as you can see
> there is data for ipoweraccount element
>
> build2.8.4.12
> build20.0.0.1
> company1
> datatotal+95M -rwxr--r--++++1+ipbuild++ipbuild+++++++54M+Aug++5+16:20+ip.jar
> -rwxr--r--++++1+ipbuild++ipbuild+++++++40M+Aug++5+17:30+ipower.ear
> data2
> env 2
> env23
> id181
> ipoweraccount
> 2
> jobERROR
> path/u01/home/ipbuild/PROD_Stage/SR284
> release_id61
> reltype1
> software_id1
>
> This is the responce I get from the server:
>
> {"ipoweraccount":["Please select a Account."]}
>
>
>
>
> This is what I currently have in my controller:
>
> public function relnotejobviewAction()
> {
> try {
> $auth = Zend_Auth::getInstance();
>
> if ($auth->hasIdentity()):
>
> $email = $auth->getIdentity();
> $id = $this->user->getuserid($email);
> $role = $this->user->checkrole($id);
>
> if($this->acl->isAllowed($role, 'admin', 'relnotejobview' )):
>
> $job_id =
> Zend_Filter::get($this->getRequest()->get('jobid'),'StripTags');
> $this->db->setFetchMode(Zend_Db::FETCH_OBJ);
> $select = $this->db->select()
> ->from(array('p' => 'SOFTWARE_DOC_QUE'),
>
>
> array('ID','TYPE_ID','SOFTWARE_ID','RELEASE_ID','HARDWARE_ID','ACCOUNT_ID','BUILD_ID',
>
> 'HARDWARE_ID2','ACCOUNT_ID2','PATH','BUILD_ID2','STATUS','COMPANY_ID',
>
>
> 'SOFTWARE_DATA','SOFTWARE_DATA2','CREATION_DATE','LAST_UPDATE'))
> ->where('p.ID= ?',$job_id);
>
>
> $result = $this->db->fetchRow($select);
> $this->view->date = $result->CREATION_DATE;
> $this->view->update = $result->LAST_UPDATE;
>
> $form = new RelJobForm();
> $form->job->setValue($result->STATUS);
> $form->company->setValue($result->COMPANY_ID);
> $form->reltype->setValue($result->TYPE_ID);
>
> $form->software_id->setValue($result->SOFTWARE_ID);
>
> $form->release_id->addMultiOptions($this->db->fetchPairs($this->db->select()->from(array('p'
> => 'SOFTWARE_RELEASES'),array('RELEASE_ID',
> 'RELEASE_NAME'))->where('p.SOFTWARE_ID = ?',$result->SOFTWARE_ID)));
>
> $form->release_id->setValue($result->RELEASE_ID);
> $form->env->setValue($result->HARDWARE_ID);
>
> //$form->ipoweraccount->addMultiOptions($this->db->fetchPairs($this->db->select()->from(array('p'
> => 'HARDWARE_ACCOUNTS'),array('ACCOUNT_ID',
> 'ACCOUNT'))->where('p.SOFTWARE_ID = ?',$result->SOFTWARE_ID)));
>
> $form->build->setValue($result->BUILD_ID);
> $form->data->setValue(stream_get_contents($result->SOFTWARE_DATA));
> $form->env2->setValue($result->HARDWARE_ID2);
> $form->path->setValue($result->PATH);
>
> $form->build2->setValue($result->BUILD_ID2);
> $form->id->setValue($result->ID);
> $form->data2->setValue(stream_get_contents($result->SOFTWARE_DATA2));
> $this->view->form = $form;
>
>
> $request = $this->getRequest();
>
>
> if($request->isPost()):
> $this->_helper->layout->disableLayout();
> $this->_helper->viewRenderer->setNoRender();
>
> $response = $form->processAjax($request->getPost());
> $this->getResponse()->setHeader('Content-Type',
> 'application/json')
> ->setBody($response);
>
>
> endif;
>
> else:
> $this->render('insufficient-permissions');
> endif;
> elseif(!$auth->hasIdentity()):
> $this->_redirect('/register');
> endif;
>
> }
> catch (Exception $e)
> {
> $msg = $e->getMessage();
> $this->log->info("Controller: Administrator Action: relnotejobview Error:
> $msg" );
> $this->render('error');
>
> }
>
> }
>
>
>
>
>
> This is the Form Class:
>
> class RelJobForm extends Zend_Form
> {
>
> protected $db;
>
> public function init()
> {
> $this->db = Zend_Registry::get('db');
>
> $this->db->setFetchMode(Zend_Db::FETCH_ASSOC);
> $this->setName('env_form');
> $this->setMethod('post');
> $allowedTags = array('p','strong','i','b','u');
>
> $allowedAttributes = array('class');
>
> $test = new Ems_Form_Element_Raw_RawText('test');
> $test->setValue('<h3>Job Information</h3>');
>
>
> $job = new Zend_Form_Element_Select('job');
> $job->setLabel('Job Status')
> ->setRequired(false)
> ->addFilter('StripTags')
> ->addFilter('StringTrim')
>
> ->addValidator('NotEmpty')
> ->setAttrib('id','job')
> ->addMultiOptions(array('' => '','PENDING' => 'Pending', 'ERROR'
> => 'Error', 'INPROGRESS' => 'In-Progress','COMPLETE' => 'Complete'))
>
> ->setAttrib('class','{required:true}')
> ->addErrorMessage('Please select and option')
> ->addDecorator('Label',array('tag' => 'div', 'class' =>
> 'element'));
>
>
> $company = new Zend_Form_Element_Select('company');
> $company->setLabel('Customer')
> ->setRequired(true)
> ->addFilter('StripTags')
>
> ->addFilter('Digits')
> ->addFilter('StringTrim')
> ->addValidator('NotEmpty')
> ->setAttrib('id','customer')
>
> ->setAttrib('class','{required:true}')
> ->addErrorMessage('Please select a Customer.')
> ->addMultiOptions(array (''=> ''))
>
>
> ->addMultiOptions($this->db->fetchPairs($this->db->select()->from(array('p'
> => 'COMPANY'),array('COMPANY_ID', 'COMPANY_NAME'))))
> ->addDecorator('Label',array('tag' => 'div', 'class' =>
> 'element'));
>
>
>
> $reltype = new Zend_Form_Element_Select('reltype');
> $reltype->setLabel('Release Type')
> ->setRequired(true)
> ->addFilter('StripTags')
>
> ->addFilter('Digits')
> ->addFilter('StringTrim')
> ->addValidator('NotEmpty')
> ->setAttrib('id','reltype')
>
> ->setAttrib('class','{required:true}')
> ->addMultiOptions(array('' => ''))
>
> ->addMultiOptions($this->db->fetchPairs($this->db->select()->from(array('p'
> => 'SOFTWARE_RELEASE_TYPE'),array('TYPE_ID', 'TYPE_NAME'))))
>
> ->addErrorMessage('Please select a Release Type.')
> ->addDecorator('Label',array('tag' => 'div', 'class' =>
> 'element'));
>
>
>
> $software = new Zend_Form_Element_Select('software_id');
> $software->setLabel('Software')
> ->setRequired(true)
> ->addFilter('StripTags')
>
> ->addFilter('Digits')
> ->addFilter('StringTrim')
> ->addValidator('NotEmpty')
> ->setAttrib('id','software_id')
>
> ->setAttrib('onchange','getData(this)')
> ->setAttrib('class','{required:true}')
> ->addMultiOptions(array (''=> ''))
>
>
> ->addMultiOptions($this->db->fetchPairs($this->db->select()->from(array('p'
> => 'SOFTWARE'),array('SOFTWARE_ID', 'SOFTWARE_NAME'))))
> ->addErrorMessage('Please select a Software.')
>
> ->addDecorator('Label',array('tag' => 'div', 'class' =>
> 'element'));
>
>
>
> $release = new Zend_Form_Element_Select('release_id');
>
> $release->setLabel('Release')
> ->setRequired(true)
> ->addFilter('StripTags')
> ->addFilter('Digits')
> ->addFilter('StringTrim')
>
> ->addValidator('NotEmpty')
> ->setAttrib('id','release_id')
> ->setAttrib('class','{required:true}')
> ->addErrorMessage('Please select a Release.')
>
> ->addDecorator('Label',array('tag' => 'div', 'class' =>
> 'element'));
>
> $java = new Ems_Form_Element_Raw_RawText('java');
>
> $java->setValue('<h3>Java Environment Information</h3>');
>
> $env = new Zend_Form_Element_Select('env');
> $env->setLabel('Environment')
>
> ->setRequired(true)
> ->addFilter('StripTags')
> ->addFilter('Int')
> ->addFilter('StringTrim')
> ->addValidator('NotEmpty')
>
> ->setAttrib('id','env')
> ->setAttrib('class','{required:true}')
> ->setAttrib('onchange','getIPOWERData(this)')
> ->addMultiOptions(array('' => ''))
>
>
> ->addMultiOptions($this->db->fetchPairs($this->db->select()->from(array('p'
> => 'HARDWARE'),array('HARDWARE_ID', 'NAME'))))
> ->addErrorMessage('Please select a Release.')
>
> ->addDecorator('Label',array('tag' => 'div', 'class' =>
> 'element'));
>
>
> $account = new Zend_Form_Element_Select('ipoweraccount');
>
> $account->setLabel('Account')
> ->setRequired(false)
> ->addFilter('StripTags')
> ->addFilter('Int')
> ->addFilter('StringTrim')
>
> ->addValidator('NotEmpty')
> ->setAttrib('id','ipoweraccount')
> ->setAttrib('class','{required:true}')
> ->addMultiOptions(array('' => ''))
>
> ->addErrorMessage('Please select a Account.')
> ->addDecorator('Label',array('tag' => 'div', 'class' =>
> 'element'));
>
> $build = new Zend_Form_Element_Text('build');
>
> $build->setLabel('Build #')
> ->setRequired(true)
> ->addFilter('StripTags')
> ->addFilter('StringTrim')
> ->addValidator('NotEmpty')
>
> ->setAttrib('id','build')
> ->setAttrib('class','{required:true}')
> ->addErrorMessage('Please enter a Build #.')
> ->addDecorator('Label',array('tag' => 'div', 'class' =>
> 'element'));
>
>
> $data = new Zend_Form_Element_Textarea('data');
> $data->setLabel('Data')
> ->setRequired(false)
> ->addFilter('StripTags')
> ->addFilter('StringTrim')
>
> ->addValidator('NotEmpty')
> ->setAttrib('id','data')
> ->setAttrib('rows','15')
> ->setAttrib('cols','25')
>
> ->setAttrib('class','{required:true}')
> ->addErrorMessage('Yea Ok.')
> ->addDecorator('Label',array('tag' => 'div', 'class' =>
> 'element'));
>
>
>
> $host = new Ems_Form_Element_Raw_RawText('host');
> $host->setValue('<h3>Host Code Environment Information</h3>');
>
> $env2 = new Zend_Form_Element_Select('env2');
>
> $env2->setLabel('Environment')
> ->setRequired(false)
> ->addFilter('StripTags')
> ->addFilter('Digits')
> ->addFilter('StringTrim')
>
> ->addValidator('NotEmpty')
> ->setAttrib('id','account')
> ->setAttrib('class','{required:true}')
> ->setAttrib('onchange','getHOSTCODEData(this)')
>
> ->addMultiOptions(array('' => ''))
>
> ->addMultiOptions($this->db->fetchPairs($this->db->select()->from(array('p'
> => 'HARDWARE'),array('HARDWARE_ID', 'NAME'))))
>
> ->addErrorMessage('Please select and Environment')
> ->addDecorator('Label',array('tag' => 'div', 'class' =>
> 'element'));
>
>
> $account2 = new Zend_Form_Element_Select('hostcodeaccount');
> $account2->setLabel('Account')
> ->setRequired(false)
> ->addFilter('StripTags')
>
> ->addFilter('Digits')
> ->addFilter('StringTrim')
> ->addValidator('NotEmpty')
> ->setAttrib('id','hostcodeaccount')
>
> ->setAttrib('class','{required:false}')
> ->addErrorMessage('Please select and Account.')
> ->addDecorator('Label',array('tag' => 'div', 'class' =>
> 'element'));
>
>
> $path = new Zend_Form_Element_Text('path');
> $path->setLabel('Host Code Path')
> ->setRequired(false)
> ->addFilter('StripTags')
> ->addFilter('StringTrim')
>
> ->addValidator('NotEmpty')
> ->setAttrib('id','path')
> ->setAttrib('class','{required:false}')
> ->addErrorMessage('Yea Ok.')
>
> ->addDecorator('Label',array('tag' => 'div', 'class' =>
> 'element'));
>
> $build2 = new Zend_Form_Element_Text('build2');
> $build2->setLabel('Build #')
>
> ->setRequired(false)
> ->addFilter('StripTags')
> ->addFilter('StringTrim')
> ->addValidator('NotEmpty')
> ->setAttrib('id','buildid2')
>
> ->setAttrib('class','{required:false}')
> ->addErrorMessage('Enter a Build #.')
> ->addDecorator('Label',array('tag' => 'div', 'class' =>
> 'element'));
>
>
> $data2 = new Zend_Form_Element_Textarea('data2');
> $data2->setLabel('Data')
> ->setRequired(false)
> ->addFilter('StripTags')
> ->addFilter('StringTrim')
>
> ->addValidator('NotEmpty')
> ->setAttrib('id','data2')
> ->setAttrib('rows','15')
> ->setAttrib('cols','25')
>
> ->setAttrib('class','{required:false}')
> ->addErrorMessage('Yea Ok.')
> ->addDecorator('Label',array('tag' => 'div', 'class' =>
> 'element'));
>
>
>
> $id = new Zend_Form_Element_Hidden('id');
> $id->setRequired(false)
> ->addFilter('Digits')
> ->addFilter('StringTrim')
> ->addValidator('NotEmpty')
>
> ->addDecorator('Label',array('tag' => 'div', 'class' =>
> 'element'));
>
> $submit = new Zend_Form_Element_Submit('submit');
> $submit->setAttrib('id', 'submitbutton')
>
> ->setAttrib('class','inputSubmit');
>
>
> $this->addElements(array($id,$test,$job,$company,$reltype,$software,$release,$java,$env,$account,$build,$data,$host,$env2,$account2,$path,$build2,$data2,
> $submit));
>
> }
> }
>
>
>
>
>
>
>
>
>
>
>
>

没有评论: