I have a form that contains a drop down and a checkbox. The checkbox controls whether or not the dropdown is "disabled" (grayed out) from the initial rendering of the form. I have JS that when you uncheck the box it enables the field and now allows a user to change the value. That all works.
When you uncheck the box AND change the value in the drop down Symfony only persists the value for the checkbox and ignores the new value in the dropdown. I assume that's because symfony knew the box was disabled when the form was initially rendered and ignores any new values posted from changing that value.
I checked the profiler and it is submitting both the new value for the checkbox (1) and also the new value for the dropdown in the post fields.
The doctrine tab of the profiler only shows the checkbox value being updated but nothing for the new dropdown value.
Hopefully someone can point me in the right direction. Hopefully some simple option that tells symfony to update everything whether or not it was initially rendered as a disabled field.
You will need to use readonly attribute, and use JS or jQuery to change the status of the field. I did this in Symfony 4
->add('someInput', TextType::class, array(
'attr' => array(
'readonly' => true,
),
));
then use JS to modify it dynamically based on some value
if( someCondition ){
$(input).prop('readonly', false).attr('required',true);
} else{
$(input).prop('readonly', true).attr('required',false);
}
Related
Case:
I have an array of answers which I want to show in the view as radio inputs. When one of the answers is already answered it must be [checked] and if it's [checked] a textarea shows, so far so good.
Next I want to check another radio input and I want the current selected to be deselected and its textarea hidden, then the new selected radio input should be [checked] and it's textarea should show.
I use the FormBuilder and FormArray and have following issues.
I can't use index without intrapolation, I see many examples where the index is used without.
If I select another radio input, first my data disappears and it's not checked and on the second click it get's checked but now both are checked.
- I don't have access to the checked event, I can show it in the view with {{tempVar.checked}} as you can see above, if I use a template variable #tempVar, but I don't have access to it in the textarea below *ngIf="tempVar.checked". If I do use it in the ngIf I get the following error
Expression has changed after it was checked. Previous value: 'false'. Current value: 'true'.
Questions:
Is this the right approach?
An example of a Reactive Dynamic form with FormBuilder and FormArray with Radio inputs
Here is my code
https://gist.github.com/webwizart/121c85a0f316f47977cc0c99455af7bf
I would think using the tags should be reserved for unique identifiers? I wouldn't be surprised if dom renderer will update all elements with the same id.
Not sure if this will help, but you could use an answer view model instead (containing the same data) but that also has a 'checked' property
This way you can be sure that it will behave as you would expect.
e.g.: (in your component.ts)
let answers = this.question.Question.PossibleAnswers
.map(a =>
{ return Object.assign({}, a, { checked: false });
}
);
then in html you use: 'answer.checked' instead of 'radio.checked'
ps: also you could use ng-container instead of span, so you don't have an extra DOM imprint in your html
I have a page , where i need to show 3 drop down in same line.
Iam generating these drop downs using zend form.
Now the drop down is coming one line after one line.
How can we make it in same line
Zend form comes with decorators. If decorators are set (which is used for template theming purpose) with '' or any other Html tag then there is a possibility that the element will be displayed in new line. Try with removing decorators.
Or you can generate select element dyanamicly on .phtml (view file) by using code
$this->formSelect("ID", "default value", array('style' => 'width:60px', 'class' => 'progDiv'), $options);
Where $options is an array of select box options like `$options = array(''=>'', 1=>'option1', 2=>'option2');`
If this does not solve your query please try to post your code
Thanks
I'm displaying a choice field in Symfony 2.0.4 with multiple checkboxes. Symfony always displays the first checkbox as checked and I would like to disable this behavior. I've tried setting the underlying entity's field to an empty array before building the form but this didn't have any effect. Is this a glitch in the Symfony version or is there a special way of achieving this?
EDIT:
By choice field with multiple checkboxes I mean a choice field having the expanded and multiple options set to true.
It seems that this was my stupid mistake. I was overriding the form theme and in the choice_widget_expanded block I was adding the checked flag for the first element of the choice array.
You need to add in your form class :
$builder->add('yourField', 'choice', array(
'choices' => $choices
'required' => false,
....
));
You can find more information in the ChoiceField type documentation http://symfony.com/doc/2.0/reference/forms/types/choice.html
Also check that you don't have any JavaScript which is checking the first value.
Im trying to preserve the user inputs from dynamic menu dropdown lists - I have an number of drowpdowns and a user input text field , when the user submits the form after selecting the options from the dropdowns.
I would like to be able to preserve the last choices made so the user does not have to reselect the options again when re posting the form with another value in the text field, i would also like this to work on errors as well ?
Im using ZF to validate the form.
i have tried the follwing code in the value attr of the option:
<option value="<?php if ($_POST && errors) {
echo htmlentities($_POST['CategoryID'], ENT_COMPAT, 'UTF-8');
}?>">Main Category</option>
But does not seem to work ?
I have a static options "Main Category" ect. which is what the form defaults to after submiting
can anyone help me on this one ??
Thanks in advance
I would highly recommend using Zend_Form. If that is not possible, I would next use Zend_View Helpers to build your HTML manually. Then you can use the formSelect in your view like this:
echo $this->formSelect('CategoryId', $selected, $attribs, array(
'main' => 'Main Category'
// ... other options
));
Where $selected variable equals to one of the following: posted value(s), default value(s), or is null and $attribs variable is simply attributes for the select element.
I have a form in Zend_Form that needs some checkboxes and I'd like them to be regular old checkboxes. You know, you give em a name and a value. If they are checked your post data contains name=>value.
Zend_Form is generating two inputs fields. One, the checkbox with a value=1 and the second a hidden input with a value=2. Both have the same name. I understand in theory how Zend expects the checkbox to work, but that's not how I expect it to work and it's not how I want it to work. How do I get my old fashion HTML checkbox back?
I have tried using $this->createElement, $this->addElement and creating a Zend_Form_Element_Checkbox manually. None allow me to set the checkbox's value and all generate the hidden input.
The final and REALLY correct answer is to add an option to the element :
$this->addElement('checkbox', 'my_element', array(
'label' => 'My Element Label',
'name' => 'my_element_name',
'disableHidden' => true
));
Zend_Form_Element_MultiCheckbox is what you're looking for.
The standard Checkbox element is meant to represent "yes/no" scenarios.
You could extend Zend library and add your own custom form element to render it just like you expect it. I did it for having a date field and it worked just fine.
I wonder why that does not work for you. You can set the values to anything you want (setCheckedValue() and setUncheckedValue()). So the only difference to normal checkbox is
if (null == $this->_getParam('checkbox', null)) {
//vs.
if ($unchecked == $this->_getParam('checkbox')) {
What exactly are you trying to do?