无论ZF2控制器中的结果如何,Formvalidation.io都会为远程调用设置错误

时间:2015-03-07 08:53:01

标签: php zend-framework2 formvalidation-plugin

我在ZF2应用程序中设置了Formvalidation.io [http://formvalidation.io]远程验证器。但是,无论JSON响应如何,它都会一直触发错误消息。

设定:

<script>
$(document).ready(function() {
    $('#create-genre-form').formValidation({
        framework: 'bootstrap',
        icon: {
            valid: 'glyphicon glyphicon-ok',
            invalid: 'glyphicon glyphicon-remove',
            validating: 'glyphicon glyphicon-refresh'
        },
        fields: {
            'genre[name]': {
                icon: false,
                threshold: 3,
                verbose: false,
                validators: {
                    notEmpty: {
                        message: 'The name is required and can\'t be empty'
                    },
                    remote: {
                        url: '<?php echo $this->serverUrl(), $this->url('genre', array('action' => 'available')); ?>',
                        type: 'POST',
                        dataType: 'jsonp',
                        validKey: 'is_valid',
                        message: 'A genre with that name already exists'
                    }
                }
            },
        }

    })
});

在我的控制器中:

/**
 * Method called to set positions based on the relative position in the view.
 * 
 * @return ViewModel
 */
public function availableAction()
{
    $available = FALSE;

    // Get your ObjectManager from the ServiceManager
    $objectManager = $this->getServiceLocator()->get('Doctrine\ORM\EntityManager');

    // Determine the param to obtain
    $param = $array["genre"]["name"];

    // Obtain the name to be validated from the parameters
    $name = $this->params()->fromPost($param)["genre"]["name"];

    // Setup the NoObjectExists filter
    $options = array('object_repository' => $objectManager->getRepository(Genre::class), 'fields' => 'name');
    $filter = new NoObjectExists($options);

    // Check if Genre with given name exists
    if ($filter->isValid($name))
    {
        $available = TRUE;
    }

    $data = array(
        'is_valid' => $available
    );

    return $this->getResponse()->setContent(Json::encode($data));

}

该方法工作正常,但无论is_valid为true还是false,验证始终会触发错误消息。

有人知道可能出现什么问题吗?

0 个答案:

没有答案