2014-09-03 9 views
0

어떻게이 오류를 해결할 수 있습니까? 나는 구글과 젠드 Framework2의 일부 워드 프로세서에서 솔루션을 찾으려고했지만 운이 없어요, 내가 도움이 될 어떤 도움이 필요합니다. 난 그냥 데이터베이스를 사용하지 않고 양식을 표시하고 싶습니다. 여기 내 IndexController.php의Zend Framework2 치명적인 오류 : 39 행의 C : dir module Application view layout layout.phtml에있는 객체가 아닌 객체의 setAttribute() 함수를 호출하십시오.

namespace Application\Form; 

use Zend\Captcha; 
use Zend\Form\Element; 
use Zend\Form\Form; 

class scheduleRequestForm extends Form 
{ 
    public function __construct() /*$name = null*/ 
    { 
     parent::__construct('application\form'); 

     $this->setAttribute('method', 'post'); 

     $this->add(array( 
      'name' => 'firstname', 
      'type' => 'Zend\Form\Element\Text', 
      'attributes' => array( 
       'placeholder' => 'Type something...', 
       'required' => 'required', 
      ), 
      'options' => array( 
       'label' => 'First name', 
      ), 
     )); 

     $this->add(array( 
      'name' => 'lastname', 
      'type' => 'Zend\Form\Element\Text', 
      'attributes' => array( 
       'placeholder' => 'Type something...', 
       'required' => 'required', 
      ), 
      'options' => array( 
       'label' => 'Last Name', 
      ), 
     )); 

     $this->add(array( 
      'name' => 'csrf', 
      'type' => 'Zend\Form\Element\Csrf', 
     ));   
    } 
} 

여기 Validator.php

<?php 
namespace Application\Form; 

use Zend\InputFilter\Factory as InputFactory; 
use Zend\InputFilter\InputFilter; 
use Zend\InputFilter\InputFilterAwareInterface; 
use Zend\InputFilter\InputFilterInterface; 

class scheduleRequestFormValidator implements InputFilterAwareInterface 
{ 
    protected $inputFilter; 

    public function setInputFilter(InputFilterInterface $inputFilter) 
    { 
     throw new \Exception("Not used"); 
    } 

    public function getInputFilter() 
    { 
     if (!$this->inputFilter) 
     { 
      $inputFilter = new InputFilter(); 
      $factory = new InputFactory(); 


     $inputFilter->add($factory->createInput([ 
      'name' => 'firstname', 
      'required' => true, 
      'filters' => array( 
       array('name' => 'StripTags'), 
       array('name' => 'StringTrim'), 
      ), 
      'validators' => array( 
      ), 
     ])); 

     $inputFilter->add($factory->createInput([ 
      'name' => 'lastname', 
      'required' => true, 
      'filters' => array( 
       array('name' => 'StripTags'), 
       array('name' => 'StringTrim'), 
      ), 
      'validators' => array( 
      ), 
     ])); 

      $this->inputFilter = $inputFilter; 
     } 

     return $this->inputFilter; 
    } 
} 

의 여기 scheduleRequestForm.php의 내 코드의

나에게 도와주세요

namespace Application\Controller; 

use Zend\Mvc\Controller\AbstractActionController; 
use Zend\View\Model\ViewModel; 
use Application\Form\ScheduleRequestForm; 
use Application\Form\Form\Validator; 
use Application\Form\Model\linel; 

class IndexController extends AbstractActionController 
{ 

    public function indexAction() 
    { 
     $form = new scheduleRequestForm(); 
     $request = $this->getRequest(); 

     if($request->isPost()) 
     { 
      $user = new linel(); 

      $formValidator = new scheduleRequestFormValidator(); 
      { 
       $form->setInputFilter($formValidator->getInputFilter()); 
       $form->setData($request->getPost()); 
      } 

      if($form->isValid()){ 
      { 
       $user->exchangeArray($form->getData()); 
      } 
     } 

     return ['form' => $form]; 
    } 
} 
} 

여기 내보기 스크립트 레이아웃입니다 .phtml

,210
<?php 
$form = $this->form; 
$form->setAttribute('action', $this->url('home', array('action'=>'add'))); 

echo $this->formLabel($form->get('firstname')); 
echo $this->formElement($form->get('firstname')); 
echo $this->formElementErrors($form->get('firstname')); 
echo $this->formLabel($form->get('lastname')); 
echo $this->formElement($form->get('lastname')); 
echo $this->formElementErrors($form->get('lastname')); 
echo $this->form()->closeTag(); 
?> 

답변

1

컨트롤러 작업은 적어도 ViewModel

public function indexAction() 
{ 
    // [...] your other stuff 

    $viewModel = new \Zend\View\Model\ViewModel(); 
    $viewModel->setVariable('form', $form); 
    $viewModel->setTemplate('/path/to/viewmodel/template/file.phtml'); 

    return $viewModel; 
} 

은 또한 당신의 $this->form 변수가 레이아웃에 존재하지 않는 것을 알 수 반환하도록 설계 -> 당신의 ViewModel 포인트가 어디 템플릿 파일에 존재한다. 이 뷰 템플릿은 내가 지금 그것을 얻을, $this->content

템플릿 파일

<?php 
    $form = $this->form; 
    $form->setAttribute('action', $this->url('home', array('action'=>'add'))); 

    echo $this->formLabel($form->get('firstname')); 
    echo $this->formElement($form->get('firstname')); 
    echo $this->formElementErrors($form->get('firstname')); 
    echo $this->formLabel($form->get('lastname')); 
    echo $this->formElement($form->get('lastname')); 
    echo $this->formElementErrors($form->get('lastname')); 
    echo $this->form()->closeTag(); 
?> 

레이아웃 파일과 레이아웃에

<html> 
    <head> 
     <title>Test</title> 
    </head> 
    <body> 
     <div> 
      <?php echo $this->content; ?> 
     </div> 
    </body> 
</html> 
+0

덕분에 주입 :) 난에서 전체 양식을 넣어 index.phtml 템플릿과 content; ?> 표시 –