2012-11-01 3 views
0

Zend Framework 2를 사용하고 있으며 종속 드롭 다운이 필요합니다. 사용자가 카테고리 (예 : cat_id)를 선택하면 시스템은 올바른 요소로 하위 카테고리 (sca_id)를 채 웁니다.ZF2 요소 사용 선택

내가 할 수있는이 같은 응용 프로그램을 만들어 :

나의 양식은 다음과 같습니다 내가 선택할 수 있기 때문에 내가 거기 value_options를 기입하지

$this->add(array(
     'name' => 'cat_id', 
     'type' => 'Zend\Form\Element\Select', 
     'options' => array(
      'label' => 'Categoria', 
      'value_options' => array(
       '' => '', 
      ), 
     ), 
    )); 
    $this->add(array(
     'name' => 'sca_id', 
     'type' => 'Zend\Form\Element\Select', 
     'options' => array(
      'label' => 'Sub Categoria', 
      'style' => 'display:none;', // Esse campo soh eh exibido qndo uma categoria for escolhida 
      'value_options' => array(
       '' => '', 
      ), 
     ), 
    )); 

주 나의 컨트롤러 내가 $.ajax이 액션에서 요소를 움켜 잡고 sca_을 채우기 위해 할

$form = new ProdutoForm('frm'); 
    $form->setAttribute('action', $this->url()->fromRoute('catalogo-admin', array(...))); 
    // Alimenta as comboboxes... 
    $form->get('cat_id')->setValueOptions($this->getCategoriaService()->listarCategoriasSelect()); 

CAT_ID의 변경 이벤트에 : 서비스 관리자는 사용 가능한입니다 신분증.

잘 작동합니다.

문제는 내 검증에 :

$this->add(array(
     'name' => 'cat_id', 
     'require' => true, 
     'filters' => array(
      array('name' => 'Int'), 
     ), 
    )); 
    $this->add(array(
     'name' => 'sca_id', 
     'require' => true, 
     'filters' => array(
      array('name' => 'Int'), 
     ), 
    )); 

내가이 말을 계속 내 양식을 제출하면 : 모두 드롭 다운에 대한 The input was not found in the haystack을 ... 내가 잘못 무엇

?

추가 질문 : 내 드롭 다운을 채우기위한 더 좋은 방법이 있습니까?

Ps :이 질문은 Disable notInArray Validator Zend Framework 2이 내게 비슷한 질문을하지만 내 문제를 자세히 설명하고 싶습니다.

답변

1

글쎄, 내 양식의 유효성을 검사하기 전에 선택 요소를 채워야한다는 것을 알았습니다!

// SaveAction 
$request = $this->getRequest(); 
if ($request->isPost()) 
{ 
    $form = new ProdutoForm(); 

    // Alimenta as comboboxes... 
    $form->get('cat_id')->setValueOptions($this->getCategoriaService()->listarCategoriasSelect()); 
    $form->get('sca_id')->setValueOptions($this->getSubCategoriaService()->listarSubCategoriasSelect()); 

    // If the form doesn't define an input filter by default, inject one. 
    $form->setInputFilter(new ProdutoFormFilter()); 

    // Get the data. 
    $form->setData($request->getPost()); 

    // Validate the form 
    if ($form->isValid()) 
    { 
     // Valid! 
    }else{ 
     // Invalid... 
    } 

그 코드는 훌륭하게 작동합니다. 이제 내 양식이 완벽하게 검증됩니다!