formbuilder

How to customize form field based on user roles in Symfony2/3?

China☆狼群 提交于 2019-12-05 15:50:20
问题 Is there a correct way to customize a form depending on the role of the user that requests it? My scenario is pretty simple: I need to hide some fields if the user has not the ROLE_ADMIN granted. I tried to avoid the field display on Twig, but {% if is_granted('ROLE_ADMIN') %} {{form_row(form.field)}} {% endif %} not works, because the form builder bypass this check. Symfony version: 2.8.2 EDIT Thanks to the @Rooneyl suggestion I've found the solution: At first, you need to add the 'role' key

Create array input field with form builder symfony2

对着背影说爱祢 提交于 2019-12-05 04:42:19
I'm having a trouble with using Form builder in Symfony2. To be exact, I need input field that is html array, but I can't create it with createFormBuilder->add. Here is what I tried: $attributesForm = $this->createFormBuilder() ->add('attribute[0]', 'text') ... And so on, but I get the following exception: The name "attribute[0]" contains illegal characters. Names should start with a letter, >digit or underscore and only contain letters, digits, numbers, underscores ("_"), hyphens >("-") and colons (":"). Is there any nice solution or I have to create fields manually? Thanks in advance! EDIT:

Angular 2 Formbuilder with Observables as default values

狂风中的少年 提交于 2019-12-05 04:32:08
I have a problem with the default value of an Angular 2 Form (formbuilder): My default values are observables (which I'm retrieving from a server), so I can't implement them like this: export class UserComponent implements OnInit{ userForm: ControlGroup; userData: any; // Initialise the observable var ngOnInit():any { this.userData = this._dataService.getAllData() // My Observable .subscribe( data => { this.userData = data; } ); this.userForm = this._formBuilder.group({ // below the default value 'username': [this.userData.username, Validators.compose([ this.usernameValid ])] } Someone an idea

how to check the user role inside form builder in Symfony2?

妖精的绣舞 提交于 2019-12-05 01:17:00
Okay, i'm trying to check if an user has a specific role, i did this however, when i do this: public function buildForm(FormBuilder $builder, array $options) { $builder ->add('nombre',null,array('label' => 'Usuario')) ->add('email') ->add('password', 'repeated', array( 'type' => 'password', 'invalid_message' => 'Los campos deben coincidir', 'first_name' => 'password', 'second_name' => 'confirmar password', 'options' => array('required' => false) )) ->add('cliente', 'entity', array( 'class' => 'ClientesBundle:Cliente', 'empty_value' => 'Company', 'required' => false, 'empty_data' => null) **)**

fields_for form builder object is nil

你。 提交于 2019-12-04 22:13:37
Any way to access a nested form_bulder.object? ## controller @project = Project.new @project.tasks.build form_for(@project) do |f| f.object.nil? ## returns false fields_for :tasks do |builder| builder.object.nil? ## returns true end end You must have accepts_nested_attributes_for in the Project model in order for the object to be passed. class Project < ActiveRecord::Base has_many :tasks accepts_nested_attributes_for :tasks ## this is required end fields_for requires that the method tasks_attributes= exists. accepts_nested_attributes_for :tasks creates this method for you, but you can also

How to add a repeated form in a loop symfony2 for the same entity

旧时模样 提交于 2019-12-04 21:37:24
I want to build a questionnaire form. When I use the following code, I can only see the last question of my table that contains 18 questions (and the answer field). I can't use a collection because my questionnaire is going to be more complicated, some questions with multiple answers, some others in true/false, etc. I simplified the code to fix this problem first. //Get question array collection $questions = $questionnaire->getQuestions(); $formBuilderQuestionnaire = $this->createFormBuilder(); //Make a loop for each question foreach($questions as $question) { //Create an answer form $answer =

Symfony form not saving entity with ManyToMany relation

纵然是瞬间 提交于 2019-12-04 12:56:23
I have problem saving entity trough form with ManyToMany relations. I can not save fields that are on "mappedBy" side of relation. Code below is not saving anything to database and not trowing any errors: // Entity/Pet /** * @var \Doctrine\Common\Collections\Collection * * @ORM\ManyToMany(targetEntity="AppBundle\Entity\Customer", mappedBy="pet", cascade={"persist"}) */ private $customer; /** * Set customer * * @param \AppBundle\Entity\Customer $customer * @return Pet */ public function setCustomer($customer) { $this->customer = $customer; return $this; } // Entity/Customer /** * @var Pet * *

Implicit argument passing of super from method defined by define_method() is not supported

情到浓时终转凉″ 提交于 2019-12-04 00:41:36
In " Agile Web Development with Rails " (third edition) page 537 - 541 it has "Custom Form Builders" code as follows: class TaggedBuilder < ActionView::Helpers::FormBuilder # <p> # <label for="product_description">Description</label><br/> # <%= form.text_area 'description' %> #</p> def self.create_tagged_field(method_name) define_method(method_name) do |label, *args| @template.content_tag("p" , @template.content_tag("label" , label.to_s.humanize, :for => "#{@object_name}_#{label}") + "<br/>" + super) end end field_helpers.each do |name| create_tagged_field(name) end end This code doesn't work

How to set a CSS ID attribute to a Symfony2 form input

我们两清 提交于 2019-12-03 16:13:26
问题 This question is similar to another question. There the solution for setting the CSS class was to add it into the 3rd parameter of a call to FormBuilder::add(): ->add('title', null, array('attr' => array('class'=>'span2'))) Unfortunately, this does not work for setting the CSS id. When I do ->add('title', null, array('attr' => array('id'=>'title-field'))) ... this is ignored. The ID remains something like namespace_formtype_field. How can I set the CSS ID, if at all? 回答1: You can do it when

Symfony 2 basic GET form generated URL

霸气de小男生 提交于 2019-12-03 13:38:58
问题 I have been trying to create an extremely basic symfony form (used for search functionality) with only one input. It uses GET method on submit. It seems to work as expected, however it generates an extremely ugly and unnecessarily long URL. I have been trying to 'clean' the URL up for a quite a while now, I was wondering if someone ran into the same problem and knows how to fix it? Form $form = $this->createFormBuilder($search) ->setMethod('GET') ->add('q', 'text') ->add('search', 'submit') -