Php Formvalidation.io为远程调用设置错误,无论ZF2控制器中的结果如何
我在ZF2应用程序中设置了Formvalidation.io[远程验证器。但是,不管JSON响应如何,它都会不断触发错误消息 设置:Php Formvalidation.io为远程调用设置错误,无论ZF2控制器中的结果如何,php,zend-framework2,formvalidation-plugin,Php,Zend Framework2,Formvalidation Plugin,我在ZF2应用程序中设置了Formvalidation.io[远程验证器。但是,不管JSON响应如何,它都会不断触发错误消息 设置: <script> $(document).ready(function() { $('#create-genre-form').formValidation({ framework: 'bootstrap', icon: { valid: 'glyphicon glyphicon-ok',
<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'
}
}
},
}
})
});
该方法工作正常,但是不管有效值是真是假,验证总是触发错误消息
有人知道可能有什么问题吗?你能发布你的正确和错误的Json响应吗?你的docblock提示表明该方法将返回一个
ViewModel
,但随后你返回响应对象。你可以试着直接返回你的$data
数组,因为它将根据你的默认策略转换成一个模型您还可以考虑返回一个JsonModel
/**
* 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));
}