symfony-forms

Get translated country name from a 2 digit country code in Symfony2/Twig?

允我心安 提交于 2019-12-03 03:42:10
问题 I'm using the Symfony2 country Field Type, it works well and country names are translated. I am storing the two-digit country code in the column country of my entity. How can I display the full, translated country name? This is how I added the field to the form: $builder ->add('country', 'country', array( 'label' => 'Paese', 'preferred_choices' => array('IT') )); And then in my controller: $user = $this->getDoctrine()->getRepository('AcmeHelloBundle:User'); $countryCode = $user->getCountry();

Doctrine does not update a simple array type field

巧了我就是萌 提交于 2019-12-03 02:16:50
Short Story (Edit) It is possible to store an array instead of a mapped association. In Symfony2, this is fairly easy using the collection Field Type . For example, using this technique, you could store an array of text fields that populate an array events field. However, to update an array, there is a trick, and this trick is beautifully explained by @Vadim Ashikhman in the accepted answer. Long Story Sometimes it is useful and more efficient to store an array instead of a mapped association. However, once created, it remains complicated to update this Array if the size of that array does not

Symfony2 : accessing entity fields in Twig with an entity field type

我的未来我决定 提交于 2019-12-03 02:12:03
Here is my FormType : public function buildForm(FormBuilder $builder, array $options) { $builder ->add('user', 'entity', array( 'class' => 'UserBundle:User', 'expanded' => true, 'property' => 'name', )); } Is there a way to access user's fields in the view (Twig) ? I'd like to do something like this : {% for u in form.user %} {{ form_widget(u) }} {{ form_label(u) }} {% if u.moneyLeft > 0 %} <span>{{ u.name }} : {{ u.moneyLeft }} €</span> {% endif %} {% endfor %} ... where moneyLeft and name are fields from User entity. In Symfony 2.5 - you can accomplish this by accessing the data from each

Simple check if form field has errors in Twig template

﹥>﹥吖頭↗ 提交于 2019-12-03 01:26:45
问题 In Twig template I check if a field has an error like this: {% if form.points.get('errors') is not empty %} Is there any method like: {% if form.points.hasErrors() %} to do it simpler? It's not a big difference, but if I can't do it easier why not. 回答1: That method does not exist. I typically do {% if form.points.vars.errors|length %} . 回答2: better way I found, is to use this kind of code {% if not form.vars.valid %} <div class="alert alert-error"> {{ form_errors(form) }} </div> {% endif %}

Adding a field specific error from the controller in symfony2

假装没事ソ 提交于 2019-12-02 22:03:57
I have some complex validation going on with my symfony form, and I need to be able to assign an error to a specific field from my controller. Right now, I have global errors working like this: $error = new formerror("There is an error with the form"); $form->addError($error); But that creates a global error, not one bound to a specific field. Is there a way to throw an error on a specific field from my controller? MrGlass Thanks to some help over IRC (thanks @fkrauthan!) I came up with an answer. Every field in SF2 is actually an instance of form. What you need to do is access the form object

Symfony Generator Forms, Doctrine, and M:N Relationships

旧城冷巷雨未停 提交于 2019-12-02 17:26:17
问题 I have a basic M:N setup with three tables: candidate, position, and candidate_position. Here's a screenshot of the ERD from MySQL Workbench Now, moving on from that let's talk about forms. In the default world of symfony generator, you'd have a separate CRUD interface for all three of these tables. However, I don't want to have a CRUD interface for candidate_position . What I want, is for the create and edit actions of the Candidate interface to contain a multi-choice field (select list,

Simple check if form field has errors in Twig template

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-02 14:49:47
In Twig template I check if a field has an error like this: {% if form.points.get('errors') is not empty %} Is there any method like: {% if form.points.hasErrors() %} to do it simpler? It's not a big difference, but if I can't do it easier why not. That method does not exist. I typically do {% if form.points.vars.errors|length %} . better way I found, is to use this kind of code {% if not form.vars.valid %} <div class="alert alert-error"> {{ form_errors(form) }} </div> {% endif %} You can also check for errors when overriding field rendering: {% block field_row %} {% spaceless %} <div class=

Symfony - Form requested url not found

╄→гoц情女王★ 提交于 2019-12-02 12:36:22
问题 Im working on a symfony project. Im battleling with a form that won't redirect to its own page. The action attribute is set to "" and method set to post. In that case it should call the same page but I'm ending on a 404 page. Here's the code of my page in the action file: public function executeDetail(sfWebRequest $request) { if($request->isMethod(sfRequest::POST)) { if(!$this->getUser()->isAuthenticated()) $this->redirect('@user_login'); $formData = $request->getParameter($this->form-

Get options of an entity Field in twig

这一生的挚爱 提交于 2019-12-02 12:20:32
I have an entity field that is passed to twig view, It is rendered and works fine. Is it possible to get the <option> values of that field and render them somewhere else ( I need another select with the same values of that entity field >. say this is my entity field {{ form_widget(entity_field) }} I want something like this <select id="just_another_select"> {% for value in entity_field.values %} <- how to get entity_field.values <option > value </option> {% endfor %} Yes you can get all your options by accesing your field vars see this {{ dump(form.YourFieldName.vars['choices']) }} 来源: https:/

Symfony 3 - Form model data loses property values which are not represented by fields

别等时光非礼了梦想. 提交于 2019-12-02 10:28:51
问题 I have a controller action method which should handle a two-splitted form. Each form handles just a few properties of my Entity Workflow . After submitting the first form I can create and render the second form without problems. Now the problem: After submitting the second form, the information of all values set in the first form are gone which means that when calling submit (or handleRequest does not make any difference here) the entity object only holds data of the properties set in the