symfony-2.1

Use a conditional statement when creating a form

拟墨画扇 提交于 2019-11-28 18:03:28
I would like to use a conditional statement when creating a form in Symfony. I am using a choice widget in general case. If the user selects the option " Other ", I would like to display an additional text box widget . I suppose this can be done in javascript, but how can I still persist the data from 2 widgets into the same property in my entity? I have this so far: $builder->add('menu', 'choice', array( 'choices' => array('Option 1' => 'Option 1', 'Other' => 'Other'), 'required' => false, )); //How to add text box if choice == Other ???? I was planing to use a DataTransfomer, but on 2

doctrine2 findby two columns OR condition

我的未来我决定 提交于 2019-11-28 13:48:40
My action: $matches_request = $em->getRepository('Bundle:ChanceMatch')->findByRequestUser(1); $matches_reply = $em->getRepository('Bundle:ChanceMatch')->findByReplyUser(1); Is it possible to join the querys with an or condition with getRepository, eg. $matches_reply = $em->getRepository('FrontendChancesBundle:ChanceMatch')->findBy(array('requestUser' => 1, 'replyUser' => 1); //this of course gives me the a result when `requestUser` and `replyUser` is `1`. My table id | requestUser | replyUser .... 12 | 1 | 2 13 | 5 | 1 My query should return the id 12 & 13 . Thanks for help! You can use

Autoloading a class in Symfony 2.1

拜拜、爱过 提交于 2019-11-28 13:32:40
I'm porting a Symfony 1.2 project to Symfony 2.x. I'm currently running the latest 2.1.0-dev release. From my old project I have a class called Tools which has some simple functions for things like munging arrays into strings and generating slugs from strings. I'd like to use this class in my new project but I'm unclear how to use this class outside of a bundle. I've looked at various answers here which recommend changing app/autoload.php but my autoload.php looks different to the ones in the answers, maybe something has changed here between 2.0 and 2.1. I'd like to keep my class in my src or

Set default value on Datetime field in symfony2 form

别说谁变了你拦得住时间么 提交于 2019-11-28 09:35:05
I have a form containing several fields. One of them is a Datetime field. How to define a default value for that field? I've tried setting a value on the related entity, in controller, in constructor and __construct : $myEntity = new MyEntity(); $myEntity->setMyDate(new \DateTime()); $form = $this->createForm(new AddMyEntity(), $myEntity); Not working. Tried to define the $data variable in the buildForm : $builder->add('myDate', 'date', array( 'format' => \IntlDateFormatter::SHORT, 'input' => 'datetime', 'widget' => 'single_text', 'data' => new \DateTime("now")); Not working either. Any ideas,

Symfony2: How to deploy in subdirectory (Apache)

落花浮王杯 提交于 2019-11-28 09:33:55
What is the best way to deploy symfony2 application in a subdirectory or an alias ? Lets say that my application will run under: http://domain/symfonytest symfonytest is an alias to some directory in my filesystem. Is there some CLI operation that I can use ? When I try to configure it manually I see a problem with routing. Requets to http://domain/symfonytest/demo are seen by router as /symfonytest/demo Is there a way to tell router to ignore /symfonytest prefix for the whole application ? meze If you are using Symfony < 2.3 you could follow this approach: Just add RewriteBase in your

Symfony2.1 using form with method GET

痴心易碎 提交于 2019-11-28 09:24:38
I need help on using Symfony2.1 forms with method=GET and a clean URL space. I am creating a "filter" which I'd like to set in the URL so that people can bookmark their links. So, very simply the code: $form = $this->createFormBuilder($defaultData) ->add('from', 'date', array('required' => false, 'widget' => 'single_text', 'format' => 'dd.MM.yyyy')) I render the form widget and all is fine. However when I submit the form, it produces very ugly GET parameters: /app_dev.php/de/event?form%5Bfrom%5D=17.11.2012 This is because the input name is of course form[from] So to clean the URL space, I made

How can I add a violation to a collection?

笑着哭i 提交于 2019-11-28 09:17:23
My form looks like this: public function buildForm(FormBuilderInterface $builder, array $options) { $factory = $builder->getFormFactory(); $builder->add('name'); $builder->add('description'); $builder->add('manufacturers', null, array( 'required' => false )); $builder->add('departments', 'collection', array( 'type' => new Department )); } I have a class validator on the entity the form represents which calls: if (!$valid) { $this->context->addViolationAtSubPath('departments', $constraint->message); } Which will only add a 'global' error to the form, not an error at the sub path. I assume this

How would you add a file upload to a Symfony2 DataFixture?

╄→гoц情女王★ 提交于 2019-11-28 08:24:35
I can't seem to wrap my head around how I would go about adding a file upload to a DataFixture. I'm trying to upload an image for the dummy content my fixtures load. This seems like something that would be useful to know. Fred John Although this question has been asked 1 year ago it appears that there is not a lot of information out there on how to upload a file via doctrine data fixture. I could only find this post. I've been looking and I've taken a slightly different approach than ornj's. (Might have to do with Symfony's updates.) I first had to use Symfony\Component\HttpFoundation\File

Symfony 2.1 set locale

有些话、适合烂在心里 提交于 2019-11-28 07:50:44
Does anybody know how to set the locale in Symfony2.1? I am trying with: $this->get('session')->set('_locale', 'en_US'); and $this->get('request')->setLocale('en_US'); but none of those has any effect, the devbar tells me: Session Attributes: No session attributes Anyway, it is always the fallback locale that is used, as defined in config.yml (PS: I am trying to set up the translation system as described here Even though the Symfony 2.1 states that you can simply set the locale via the Request or Session objects, I never managed to have it working, setting the locale simply has no effect. So I

Set locale in Symfony 2.1

谁说我不能喝 提交于 2019-11-28 07:04:40
I am trying to have a language switcher on my symfony 2.1 website. I followed the official documentation , set the translation files but setting the locale with $request->setLocale('en_US'); doesn't seem to work. After some research, I found this question which provides a beginning of an answer with this listener technique . However, I still don't manage to have it working, I'm not so sure about my listener declaration, is something wrong with it? My controller: public function englishAction(Request $request) { $this->get('session')->set('_locale', 'en_US'); return $this->redirect($request-