Question

Hi I have three dropdown, the first one is filled in my controller, the second one needs to be populated in ajax with params from the first one and the third needs the second. When my form valid all the fields It prints the following error in both dropdown The input was not found in the haystack

This is my ajax call

  $("#department_id").change(function(){
    $.ajax({
        type: "POST",
        url: "<?php echo $this->url('zfcadmin/getprovinces'); ?>",
        data: "dept="+$(this).val(),
        success: function(msg){ 
            $("#province_id").html(msg);
    }
 });
});

This is my Controller what am I doing wrong any help?

public function getProvincesByIDAction()
{
    $request = $this->getRequest();
    $response = $this->getResponse();
    if ($request->isPost()) {
        $response->setStatusCode(200);
        $codDpto=$request->getPost('dept');
        $data=$this->getTable()->getProvinces($codDpto);
        $buffer="<option value=''>Seleccione una Provincia</option>";
        foreach ($data as $prov) {
            $buffer.=<<<EOT
             <option value='{$prov->codProv}'>{$prov->name}</option>
          EOT;
        }
           $response->setContent($buffer);
        $headers = $response->getHeaders();
    }
    return $response;
}

Any help?

Thanks

Was it helpful?

Solution 2

When you submit the form it is recreated server side. This means that the value_options array for each select element will not contain the value that has been posted (as the options were loaded via AJAX)

So the error "The input was not found in the haystack" could be thought of as "The option you selected was not an option in the select element"

In order to validate the posted values you will need to ensure that you add these options before validation and form population on the server side (i.e before $form->setData($request->getPost())) when the form is posted.

OTHER TIPS

You can set value of "disable_inarray_validator" to "true" in the options of the form element

'options' => array( 'label' => 'Some Label', 'value_options' => array(), 'disable_inarray_validator' => true, ),

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top