2017-02-20 15 views
0

지금 zend framework 2에서 간단한 폼을 검증하려고했습니다. 이 항목을 고려하여 설명서와 많은 게시물을 확인했지만 해결 방법을 찾지 못했습니다. ZF2 간단한 폼 유효성 확인

나는 아주 간단한 양식을 가지고

namespace Application\Form; 

use Zend\InputFilter\InputFilter; 
class AlimentInputFilter extends InputFilter { 
    public function init() 
    { 
    $this->add([ 
     'name'  => AlimentForm::year, 
     'required' => true, 
     'validators' => array(
      array(
       'name' => 'Between', 
       'options' => array(
        'min' => 1900, 
        'max' => 3000, 
      ), 
     ), 
     ), 
    ]); 
} 
} 

그리고 마지막으로 내 컨트롤러에서 나는 양식을

public function alimentAction(){ 
    $form = new AlimentForm(); 

    $form->setInputFilter(new AlimentInputFilter()); 

    $request = $this->getRequest(); 
    if ($request->isPost()) { 
     $form->setData($request->getPost()); 

     if ($form->isValid()) { 
      $year = $form->get('year')->getValue(); 
      $number = $form->get('number')->getValue(); 

      return array('result' => array(
       "msg" => "In the Year ".$year." you get ".$number." Points" 
      )); 
     } 
    } 
    return array('form' => $form); 
} 

이 유효성을 검사 할 : 나는 정의 InputFilter를 만들어

class AlimentForm extends Form 
{ 
public function __construct($name = null) 
{ 
    parent::__construct('aliment'); 

    $this->add(array(
     'required'=>true, 
     'name' => 'year', 
     'type' => 'Text', 
     'options' => array(
      'label' => 'Jahr', 
     ), 
    )); 
    $this->add(array(
     'required'=>true, 
     'name' => 'number', 
     'type' => 'Text', 
     'options' => array(
      'label' => 'Number', 
     ), 
    )); 
    $this->add(array(
     'name' => 'submit', 
     'type' => 'Submit', 
     'attributes' => array(
      'value' => 'Go', 
      'id' => 'submitbutton', 
     ), 
    )); 
} 
} 

그렇게 어렵지는 않을 것입니다. 그러나 그물에서 발견 된 형태를 검증하는 모든 다른 방법들로부터, 저는 약간 혼란 스럽습니다. 에드 ...

내가 무엇이 누락 되었습니까?

안녕하세요. 미리 감사드립니다. 미국.

답변

0

좋아, 문제를 해결했습니다.

난 일년 수가 특성을 갖는 음식물 형태의 모델을 만들고 그 모델 내의 입력 필터 정의 : I 설정할 수 있었다 제어기의 동작 방법

use Zend\InputFilter\InputFilter; 
use Zend\InputFilter\InputFilterAwareInterface; 
use Zend\InputFilter\InputFilterInterface; 

class Aliment implements InputFilterAwareInterface 
{ 
    public $year; 
    public $number; 

    public function exchangeArray($data){ 
     $this->year    = (!empty($data['year']))?$data['year']:null; 
     $this->number   = (!empty($data['number']))?$data['number']:null; 
    } 

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

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


      $inputFilter->add(array(
       'name'  => 'year', 
       'required' => true, 
       'filters' => array(
        array('name' => 'StripTags'), 
        array('name' => 'StringTrim'), 
       ), 
       'validators' => array(
        array(
         'name' => 'Between', 
         'options' => array(
          'min' => 1900, 
          'max' => 3000 
         ) 
        ) 
       ), 
      )); 

      $inputFilter->add(array(
       'name'  => 'number', 
       'required' => true, 
       'filters' => array(
        array('name' => 'StripTags'), 
        array('name' => 'StringTrim'), 
       ), 
       'validators' => array(
        array(
         'name' => 'Between', 
         'options' => array(
          'min' => 1, 
          'max' => 10 
         ) 
        ) 
       ), 
      )); 

      $this->inputFilter = $inputFilter; 
     } 

     return $this->inputFilter; 
    } 

} 

을 폼의 InputFilter와 voila! 그것은 효과가있다!

public function alimentAction(){ 
    $form = new AlimentForm(); 

    $request = $this->getRequest(); 
    if ($request->isPost()) { 
     $aliment = new \Application\Model\Aliment; 
     $form->setInputFilter($aliment->getInputFilter()); 
     $form->setData($request->getPost()); 


     if ($form->isValid()) { 
      $aliment->exchangeArray($form->getData()); 
      $year   = $form->get('year')->getValue(); 
      return array(
       'result' => array(
        //your result 
       ) 
      ); 
     } 
    } 
    return array('form' => $form); 
} 

양식의 유효성을 올바르게 검사하고 해당 오류 메시지를 반환합니다.

나는 그것이 유효성 검사와 비슷한 문제가있는 누군가를 돕는다.