gpt4 book ai didi

jquery - Symfony ajax 字段验证

转载 作者:行者123 更新时间:2023-12-01 01:19:42 25 4
gpt4 key购买 nike

我正在寻找一种通过 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');

您对这个想法有何看法?还有更好的吗?

最佳答案

好的,这个方法有效,但是每次您发送请求时,symfony 都会构建整个表单,因此我在表单类和基本表单类之间创建了一个抽象类:

    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) {

}
}

之后您可以在 Controller 中执行此操作:

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 周期。

关于jquery - Symfony ajax 字段验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5001452/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com