您可以使用 json dataType 发布表单并返回包含有关表单是否有效或包含错误的信息的 json 响应。我假设您使用 jQuery 发布表单,因为您使用的是 jQuery-UI。
例如
// apps\myApp\modules\myModule\actions\action.class.php
public function executeEdit(sfWebRequest $request)
{
$this->folderForm = new FolderForm(array(), array('user_template' => $user_template));
if ($request->isMethod(sfRequest::POST) && $request->isXmlHttpRequest()) {
$this->folderForm->bind($request->getParameter($form->getName()));
$response = array();
if ($this->folderForm->isValid()) {
$folder = $this->folderForm->save();
$response['errors'] = array();
} else {
foreach ($this->folderForm->getErrors() as $name => $error) {
$response['errors'][] = array(
'field' => $name,
'message' => $error->getMessage(),
);
}
}
$this->getResponse()->setContentType('application/json');
return $this->renderText(json_encode($response));
}
}
然后在你的 javascript
$.post('/myModule/edit/id/' + id, $('my-form').serialize(), function (j) {
var length = j.errors.length;
if (length) {
for (var i = 0; i < length; i++) {
console.log(j.errors[i]);
// Show error
}
} else {
// Show success notification
}
}, 'json');