sfFormをActionのバリデータとして使う
バリデータ用のformクラス作成。バリデートに関する実装はこちら
class validateForm extends sfForm { public function configure() { $this->setValidators(array( 'param' => new sfValidatorString(array('required' => true), array('required' => 'is required.')), 'hoge' => new sfValidatorString(array('required' => true, 'max_length' => 5), array('max_length' => 'is too long.')), )); // fieldに無いパラメータを許可する $this->validatorSchema->setOption('allow_extra_fields', true); } }
アクション側にて、上記作成したformクラスにリクエストのパラメータをbindする
public function executeXXX(sfWebRequest $request) { $form = new validateForm(null, null, false); // csrf_tokenチェックをしない $form->bind($request->getParameterHolder()->getAll()); if (!$form->isValid()) { foreach ($form->getErrorSchema()->getErrors() as $key => $err) { $errors[$key] = $err->getMessage(); } return $this->renderText(json_encode($errors)); } else { return $this->renderText("OK"); } }
// Error時の出力 例)?hoge=123456¶m= {"hoge":"is too long.","param":"is required."}
[symfony 1.4]