本文整理汇总了PHP中Symfony\Component\Form\FormBuilder::addEventListener方法的典型用法代码示例。如果您正苦于以下问题:PHP FormBuilder::addEventListener方法的具体用法?PHP FormBuilder::addEventListener怎么用?PHP FormBuilder::addEventListener使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Symfony\Component\Form\FormBuilder
的用法示例。
在下文中一共展示了FormBuilder::addEventListener方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: fb
/**
* @return FormBuilder
*/
public final function fb()
{
if ($this->formBuilder == null) {
$contentEmptyForm = new PreferenceEmptyForm();
$this->formBuilder = $this->container->get('form.factory')->createBuilder($contentEmptyForm);
$this->formBuilder->addEventListener(FormEvents::POST_SUBMIT, array(&$this, 'formEventPostSubmit'));
}
return $this->formBuilder;
}
示例2: buildForm
public function buildForm(FormBuilder $builder, array $options)
{
$factory = $builder->getFormFactory();
// throw new \Exception('El valor de region es '.$regionId);
$builder->add('nombre')->add('apellido');
if (null == $options['data']->getId()) {
$builder->add('login')->add('password', 'password', array('required' => true));
} else {
$builder->add('login', null, array('read_only' => true))->add('password', 'password', array('required' => false));
}
$builder->add('rut')->add('cargo')->add('profesion')->add('especialidades')->add('email')->add('email2')->add('codigoPostal')->add('direccion')->add('telefono')->add('telefono2')->add('celular')->add('empresa')->add('paginaWeb')->add('tipoUsuario', null, array('required' => true, 'preferred_choices' => array(5)));
/*
if (null == $options['data']->getRegion()) {
$builder->add('comuna', null , array('required' => false, 'empty_value' => '...', 'choices' => array()));
} else {
$builder->add('comuna', 'entity', array(
'required' => false,
'class' => 'TipddyMasleadsBundle:Comunas',
'empty_value' => 'Seleccione una comuna',
'query_builder' => function(EntityRepository $repository) use ($regionId) {
return $repository->createQueryBuilder('c')
->where('c.region = :region')
->setParameter('region', $regionId)
->orderBy('c.id', 'ASC');
}));
}
*/
$builder->add('region', null, array('empty_value' => 'Seleccione', 'required' => false));
/* Probando Eventos en Formularios */
$factory = $builder->getFormFactory();
$refreshComuna = function ($form, $region) use($factory) {
$form->add($factory->createNamed('entity', 'comuna', null, array('class' => 'TipddyMasleadsBundle:Comunas', 'empty_value' => $region == null ? '...' : 'Seleccione', 'label' => 'register.comuna.label', 'query_builder' => function (EntityRepository $repository) use($region) {
return $repository->createQueryBuilder('c')->where('c.region = :region')->setParameter('region', $region)->orderBy('c.id', 'ASC');
})));
};
$builder->addEventListener(FormEvents::PRE_SET_DATA, function (DataEvent $event) use($refreshComuna) {
$form = $event->getForm();
$data = $event->getData();
if ($data == null) {
$refreshComuna($form, null);
}
if ($data instanceof Usuarios) {
$refreshComuna($form, $data->getRegion());
}
});
$builder->addEventListener(FormEvents::PRE_BIND, function (DataEvent $event) use($refreshComuna) {
$form = $event->getForm();
$data = $event->getData();
if (array_key_exists('region', $data)) {
$refreshComuna($form, $data['region']);
}
});
}
示例3: buildForm
/**
* @see Symfony\Component\Form\AbstractType::buildForm()
*/
public function buildForm(FormBuilder $builder, array $options)
{
$builder->add('username', 'text')->add('password', 'password')->add('_target_path', 'hidden');
$request = $this->request;
/* Note: since the Security component's form login listener intercepts
* the POST request, this form will never really be bound to the
* request; however, we can match the expected behavior by checking the
* session for an authentication error and last username.
*/
$builder->addEventListener(FormEvents::SET_DATA, function (FilterDataEvent $event) use($request) {
if ($request->attributes->has(SecurityContextInterface::AUTHENTICATION_ERROR)) {
$error = $request->attributes->get(SecurityContextInterface::AUTHENTICATION_ERROR);
} else {
$error = $request->getSession()->get(SecurityContextInterface::AUTHENTICATION_ERROR);
}
if ($error) {
$event->getForm()->addError(new FormError($error->getMessage()));
}
$event->setData(array_replace((array) $event->getData(), array('username' => $request->getSession()->get(SecurityContextInterface::LAST_USERNAME))));
});
}
示例4: setUserFormValidation
/**
* Validate the user form.
*
* Use a custom validator to check:
* * Passwords are identical
* * Username is unique
* * Email is unique
* * Displaynames are unique
*
* @param FormBuilder $form
* @param boolean $addusername
*
* @return \Symfony\Component\Form\FormBuilder
*/
private function setUserFormValidation(FormBuilder $form, $addusername = false)
{
$users = $this->users();
$form->addEventListener(FormEvents::POST_SUBMIT, function (FormEvent $event) use($addusername, $users) {
$form = $event->getForm();
$id = $form['id']->getData();
$pass1 = $form['password']->getData();
$pass2 = $form['password_confirmation']->getData();
// If adding a new user (empty $id) or if the password is not empty (indicating we want to change it),
// then make sure it's at least 6 characters long.
if ((empty($id) || !empty($pass1)) && strlen($pass1) < 6) {
$error = new FormError(Trans::__('page.edit-users.error.password-short'));
$form['password']->addError($error);
}
// Passwords must be identical.
if ($pass1 != $pass2) {
$form['password_confirmation']->addError(new FormError(Trans::__('page.edit-users.error.password-mismatch')));
}
if ($addusername) {
// Password must be different from username
$username = strtolower($form['username']->getData());
if (!empty($username) && strtolower($pass1) === $username) {
$form['password']->addError(new FormError(Trans::__('page.edit-users.error.password-different-username')));
}
// Password must not be contained in the display name
$displayname = strtolower($form['displayname']->getData());
if (!empty($displayname) && strrpos($displayname, strtolower($pass1)) !== false) {
$form['password']->addError(new FormError(Trans::__('page.edit-users.error.password-different-displayname')));
}
// Usernames must be unique.
if (!$users->checkAvailability('username', $form['username']->getData(), $id)) {
$form['username']->addError(new FormError(Trans::__('page.edit-users.error.username-used')));
}
}
// Email addresses must be unique.
if (!$users->checkAvailability('email', $form['email']->getData(), $id)) {
$form['email']->addError(new FormError(Trans::__('page.edit-users.error.email-used')));
}
// Displaynames must be unique.
if (!$users->checkAvailability('displayname', $form['displayname']->getData(), $id)) {
$form['displayname']->addError(new FormError(Trans::__('page.edit-users.error.displayname-used')));
}
});
return $form;
}
示例5: setUserFormValidation
/**
* Validate the user form.
*
* Use a custom validator to check:
* * Passwords are identical
* * Username is unique
* * Email is unique
* * Displaynames are unique
*
* @param Application $app
* @param \Symfony\Component\Form\FormBuilder $form
* @param boolean $addusername
*
* @return \Symfony\Component\Form\FormBuilder
*/
private function setUserFormValidation(Application $app, FormBuilder $form, $addusername = false)
{
$form->addEventListener(FormEvents::POST_SUBMIT, function (FormEvent $event) use($app, $addusername) {
$form = $event->getForm();
$id = $form['id']->getData();
$pass1 = $form['password']->getData();
$pass2 = $form['password_confirmation']->getData();
// If adding a new user (empty $id) or if the password is not empty (indicating we want to change it),
// then make sure it's at least 6 characters long.
if ((empty($id) || !empty($pass1)) && strlen($pass1) < 6) {
// screw it. Let's just not translate this message for now. Damn you, stupid non-cooperative
// translation thingy. $error = new FormError("This value is too short. It should have {{ limit }}
// characters or more.", array('{{ limit }}' => 6), 2);
$error = new FormError(Trans::__('page.edit-users.error.password-short'));
$form['password']->addError($error);
}
// Passwords must be identical.
if ($pass1 != $pass2) {
$form['password_confirmation']->addError(new FormError(Trans::__('page.edit-users.error.password-mismatch')));
}
if ($addusername) {
// Usernames must be unique.
if (!$app['users']->checkAvailability('username', $form['username']->getData(), $id)) {
$form['username']->addError(new FormError(Trans::__('page.edit-users.error.username-used')));
}
}
// Issue 3491 : Password must be different from username
$username = $form['username']->getData();
if (!empty($username) && $pass1 === $username) {
$form['password']->addError(new FormError(Trans::__('page.edit-users.error.password-different-username')));
}
// Email addresses must be unique.
if (!$app['users']->checkAvailability('email', $form['email']->getData(), $id)) {
$form['email']->addError(new FormError(Trans::__('page.edit-users.error.email-used')));
}
// Displaynames must be unique.
if (!$app['users']->checkAvailability('displayname', $form['displayname']->getData(), $id)) {
$form['displayname']->addError(new FormError(Trans::__('page.edit-users.error.displayname-used')));
}
});
return $form;
}