php - Symfony Callback Validation with Yaml -
my validation not being called when defined using validation.yml. using php or annotation works fine.
namespace appbundle\form; use symfony\component\validator\context\executioncontextinterface; class loginform { private $login; private $password; public function __construct($login, $password) { $this->login = $login; $this->password = $password; } public function validate(executioncontextinterface $context) { $context->buildviolation('error') ->atpath('login') ->addviolation(); } }
this validation.yml
appbundle\form\loginform: constraints: - callback: [validate]
and controller
class logincontroller extends controller { public function loginaction(request $request) { if ($request->ismethod('post')) { $login = $request->request->get('loginform_login'); $password = $request->request->get('loginform_password'); $form = new loginform($login, $password); $errors = $this->get('validator')->validate($form); if (count($errors) > 0) { return $this->render('login/error.html.twig'); } return $this->render('login/ok.html.twig'); } return $this->render('login/login.html.twig'); } }
the above code returns 'ok', no matter login or password type. clue?
you need put validation.yml
file in resources/config
directory of bundle.
as of symfony 2.7 symfony load files resources/config/validation
directory.
quoting official docs:
as of symfony 2.7, xml , yaml constraint files located in resources/config/validation sub-directory of bundle loaded. prior 2.7, resources/config/validation.yml (or .xml) loaded.
some examples of valid paths:
- src/appbundle/resources/config/validation.yml
- src/appbundle/resources/config/validation/foo.yml
Comments
Post a Comment