我正在寻找一种通过jquery和ajax在symfony中验证表单字段的方法。我看到ppl建议使用jquery验证和其他外部库,但我只是发现有更快的方法。
例如,您可以使用.each()jquery函数在每个字段上设置一个事件侦听器,并使用包含相关字段和用户提供的值的json对象向symfony发送ajax请求。
在symfony方面可能会出现这样的情况:
$jsonValues = $request->getParameter('json_values');
$field = array_keys($jsonValues);
$field = $field[0];
$this->form = new $this->formName();
$vs = $this->form->getValidatorSchema();
try {
$toValidate = $vs[$field]->clean($jsonValues[$field]);
} catch (sfValidatorError $e) {
return $this->renderText($e->getMessage());
}
return $this->renderText('ok');
您如何看待这个想法?还有更好的吗?
答案 0 :(得分:3)
好的,这个方法有效,但每次发送请求时,symfony都会构建整个表单,所以我在表单类和Base Form类之间创建了一个抽象类:
abstract class wsExtensionForm extends BaseForm {
protected $widgetClass = 'input-widget';
public function configure() {
$user = sfContext::getInstance()->getUser();
$ftw = $user->hasAttribute('fieldToValidate') ? $user->getAttribute('fieldToValidate') : false;
if ($ftw) {
$this->generateValidator($ftw, $this->validatorsConfig[$ftw]);
$user->getAttributeHolder()->remove('fieldToValidate');
} else {
$this->generateWidgets($this->widgetsConfig, $this->widgetClass);
$this->generateValidators($this->validatorsConfig);
$this->generateLabels($this->labelsConfig);
}
}
protected function generateWidget($wConfig) {
}
protected function generateValidator($id, $vConfig) {
if (!is_array($vConfig)){
if ($vConfig == 'sfValidatorChoice') {
$methodName = 'get'.ucfirst($id).'Choices';
$this->validatorSchema[$id] = new $vConfig(array('choices' => array_keys($this->$methodName()), 'multiple' => false), array());
} else {
$this->validatorSchema[$id] = new $vConfig();
}
}
}
protected function generateHelp($hConfig) {
}
protected function generateWidgets($wsConfig, $class) {
foreach($wsConfig as $id => $widget) {
if (!is_array($widget)) {
if ($widget == 'sfWidgetFormInputText') {
$this->widgetSchema[$id] = new $widget(array(), array('class' => $class));
} elseif($widget == 'sfWidgetFormChoice') {
$methodName = 'get'.ucfirst($id).'Choices';
$this->widgetSchema[$id] = new $widget(array('choices' => $this->$methodName(), 'multiple' => false, 'expanded' => false), array('class' => $class));
}
}
}
}
protected function generateValidators($vsConfig) {
foreach ($vsConfig as $id => $validator) {
if (!is_array($validator)){
if ($validator == 'sfValidatorChoice') {
$methodName = 'get'.ucfirst($id).'Choices';
$this->validatorSchema[$id] = new $validator(array('choices' => array_keys($this->$methodName()), 'multiple' => false), array());
} else {
$this->validatorSchema[$id] = new $validator();
}
}
}
}
protected function generateLabels($lsConfig) {
$this->widgetSchema->setLabels($lsConfig);
}
protected function generateHelps($hsConfig) {
}
}
在此之后,您可以在控制器中执行此操作:
public function executeProc(sfWebRequest $request) {
// checking if request parameters are OK
$this->initAction($request->getParameter('form_category'), $request->getParameter('form_id'));
if ($request->isXmlHttpRequest()) {
$field = $request->getParameter('field');
$field = explode('_', $field);
$field = array_reverse($field);
$field = $field[0];
$fieldValue = $request->getParameter('fieldValue');
$this->getUser()->setAttribute('fieldToValidate', $field);
$this->form = new $this->formName();
$vs = $this->form->getValidatorSchema();
try {
$toValidate = $vs[$field]->clean($fieldValue);
} catch (sfValidatorError $e) {
return $this->renderText($e->getMessage());
}
return $this->renderText('ok');
} else {
$this->form = new $this->formName();
if ($request->isMethod('post')) {
$this->form->bind($request->getParameter($this->nameFormat));
if ($this->form->isValid()) {
exit('valid');
}
} else {
$this->forward404();
}
$this->form->getWidgetSchema()->setNameFormat($this->nameFormat.'[%s]');
$this->setTemplate('show');
}
}
并且symfony不会生成整个表单,因此您可以节省一些cpu周期。