Symfony ajax field validation
I was searching for a method to validate my form fields in symfony via jquery and ajax. I saw ppl suggesting to use jquery validation and other external libraries but I just figured out that there is a quicker way.
You could for example set up an event listener on your every field with the .each() jquery function and send an ajax request to symfony with a json object containing the field in question and the value the user provided.
On the symfony side there could be this:
$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');
What do you think about this idea? Is there a better one?
Okay this method works, but every time you send in a request symfony builds up the whole form, so I created an abstract class between the form class and the Base Form class:
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) {
}
}
After this you can do this in your 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');
}
}
and symfony doesn't generate the whole form so you can save some cpu cycles.
精彩评论