我在 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 是真还是假,验证总是会触发错误消息。
有谁知道可能出了什么问题?