Zf2 multicheckbox at least one element is always required - zend-framework

I can't validate a zf2 form with multicheckbox because at least one checkbox is always required.
I found a lot of reference to this issue (for example here - https://github.com/zendframework/zf2/issues/4845), but i didn't found a solution for this.
Does anybody know how to solve this problem ?
UPDATE: I use a doctrine 2 objectmulticheckbox which is extended from zf2 multichechbox. As is commented below the override of getInputFilterSpecification method, will solve the problem with form validation, but the values will still remain in database (values populated by objectmulticheckbox).

I found a seemingly easier way to get around this issue by setting the input filter 'required' to false inside the controller, after the form is instantiated.
<?php
$form = new CampaignForm($multiCheckboxOptions); // Setting up checkbox in form class
$form->getInputFilter()->get('my_multi_checkbox')->setRequired(false);
?>

You can override the getInputFilterSpecification function on your form to set the field to not be required. For example:
public function getInputFilterSpecification() {
return array(
[...]
'the-multi-checkbox-field' => array(
'required' => false,
),
[...]
);
}

Ok i did a little hack to solve this problem.
So I added this code in the action controller:
$form->bind($client);
/** #var $request Request */
$request = $this->getRequest();
if ($request->isPost()) {
$form->setData($request->getPost());
if ($form->isValid()) {
/** #var $client Client */
$client = $form->getData();
// hack because of - https://github.com/zendframework/zf2/issues/4694
if($request->getPost('reportSettings') === null){
$client->setReportSettings(null); // set null to remove all associations with this client
}
And also as it is described in the first answere, in form should be rewritten getInputFilterSpecification method, for field that shouldn't be required.

Related

Symfony4 Forms - How do you conditionally disable a form field?

So what is the best way to have a form render effectively the same form over and over again, with conditionally disabled fields based on the Entity's property values?
I have an Invoice Entity and need a form for creating the invoice, and also the same form with various fields disabled at various stages of the invoicing process (generated, sent, paid etc).
I think the simplest answer is to disable them dynamically in the twig template via form_row options but surely this will affect server side validation of the form as it is not aware the field has been disabled?
What is the best way to disbale a field based on a value in the database?
EDIT 1:
Changed question from Dynamically disable a field in the twig template or seperate class for each form? to Symfony4 Forms - How do you conditionally disable a form field?
Thanks to #Cerad. The answer is in fact Form Events
In the form type (App\Form\InvoicesType for me), add a method call to the end of the builder:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$plus_thirty_days = new \DateTime('+28 days');
$builder
->add('client', EntityType::class, array(
'class' => Clients::class,
'choice_label' => 'name',
'disabled' => false,
) )
// the event that will handle the conditional field
->addEventListener(
FormEvents::PRE_SET_DATA,
array($this, 'onPreSetData')
);;
}
and then in the same class, create a public method named the same as the string in the array (onPreSetData for this example):
public function onPreSetData(FormEvent $event)
{
// get the form
$form = $event->getForm();
// get the data if 'reviewing' the information
/**
* #var Invoices
*/
$data = $event->getData();
// disable field if it has been populated with a client already
if ( $data->getClient() instanceof Clients )
$form->add('client', EntityType::class, array(
'class' => Clients::class,
'choice_label' => 'name',
'disabled' => true,
) );
}
From here you can update the field to be any valid FormType and specify any valid options as you would a normal form element in the From Builder and it will replace the previous one, laving it in the same original position in the form.

Symfony2 form unchecked checkbox not taken into account, why?

When I send a form with an unchecked checkbox, if the related entity property equals true, then it does not change to false.
The other way round (setting property to true when the form is sent with a checked checkbox) works fine, as well as all the forms other fields saving.
Here is how I build the form and declare the related property:
// --- Form creation function EntityType::buildForm() ---
$builder->add('secret', 'checkbox', array( 'required' => false ));
// --- Entity related property, Entity.php file ---
/** #ORM\Column(name="secret", type="boolean") */
protected $secret;
EDIT: The issue happens because the form is submitted using a PATCH request.
In Symfony, the Form::submit method is called by a Request Handler with this line:
$form->submit($data, 'PATCH' !== $method);
As a result the Form::submit $clearMissing parameter is set to false in the case of a PATCH request, thus leaving the non-sent fields to their old value.
But I do not know how to solve the problem. If I explicitely pass a JSON {secret: false} to the Symfony framework when the checkbox is not checked, it will interpret it as the "false" string and consider that a true value, thus considering the checkbox checked...
NB. I have exactly the same issue with an array of checkboxes using a choice field type (with multiple and extended to true) linked to a Doctrine Simple Array property: as soon as a given checkbox has been sent once as checked, it is impossible to set back the related property to false with subsequent unchecked submissions.
Non of above-mentioned didn't help me.
So, I am using this...
Explanation
Resolution for this issue when "PATCH" method was used, was to add additional hidden "timestamp" field inside of a form type and to have it next to the checkbox of issue in twig file. This is needed to pass something along with the checkbox, that would definitely change - time-stamp will change.
Next thing was to use PRE_SUBMIT event and to wait for form field to arrive and if it not set, I would set it manually... Works fine, and I don't mind extra code...
FormType
$builder
...
->add('some_checkbox')
->add('time_stamp', 'hidden', ['mapped' => false, 'data' => time()])
...
Twig
{{ form_widget(form.time_stamp) }}
{{ form_widget(form.some_checkbox) }}
PRE_SUBMIT event in builder
$builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) use($options) {
$data = $event->getData();
$form = $event->getForm();
if (!$data) {
return;
}
/* Note that PATCH method is added as an option for "createForm"
* method, inside of your controller
*/
if ($options["method"]=="PATCH" && !isset($data['some_checkbox'])) {
$form->getData()->setSomeCheckbox(false);//adding missing checkbox, since it didn't arrive through submit.. grrr
}
});
The issue happens because the form is submitted using a PATCH request.
This has lead to open this Symfony issue.
As explained, one workaround is to explicitely send a specific reserved value (for instance the string '__false') when the checkbox is unchecked (instead of sending nothing), and replace this value by 'null' using a custom data transformer in the form type:
// MyEntityFormType.php -- buildForm method
$builder->add('mycheckbox', ...);
$builder->get('mycheckbox')
->addViewTransformer(new CallbackTransformer(
function ($normalizedFormat) {
return $normalizedFormat;
},
function ($submittedFormat) {
return ( $submittedFormat === '__false' ) ? null : $submittedFormat;
}
));
The case with the 'choice' field can't be solved the same way. It is actually a bug of Symfony, dealt with in this issue.
What version of Symfony are you using?
There should exist some code dedicated to the situation you're writing about, in vendor/symfony/symfony/src/Symfony/Component/Form/Form.php, in Form::submit():
// Treat false as NULL to support binding false to checkboxes.
// Don't convert NULL to a string here in order to determine later
// whether an empty value has been submitted or whether no value has
// been submitted at all. This is important for processing checkboxes
// and radio buttons with empty values.
if (false === $submittedData) {
$submittedData = null;
} elseif (is_scalar($submittedData)) {
$submittedData = (string) $submittedData;
}
Located at lines 525-534 for me. Could you check this works properly for you?
Another lead would be a custom form subscriber that do not work exactly as intended - by overwriting the provided value.
It's probably because the field isn't required on you schema. you can provide a default value to the checkbox with the following:
$builder->add('secret', 'checkbox', array(
'required' => false,
'empty_data' => false
));
See here or here
This solution works for me.
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('isActive', CheckboxType::class, array(
'required' => false
))
$builder->addEventListener(FormEvents::POST_SUBMIT, function(FormEvent $e {
$entity = $e->getData();
$form = $e->getForm();
$isActive = empty($_POST[$form->getName()]['isActive']) ? false : true;
$entity->setIsActive($isActive);
});
}
Another possibility is to add a hidden element and make this with Javascript. It will fail in 0.1 % of the people that use a browser without javascript.
This is a simple example for a multiple checkboxes FormType element:
->add('ranges', ChoiceType::class, array(
'label' => 'Range',
'multiple' => true,
'expanded' => true,
'choices' => array(
null => 'None',
'B1' => 'My range',
)
))
<script>
$(document).ready(function() {
function updateDynRanges(object) {
if (object.prop("checked")) {
document.getElementById('ranges_0').checked = 0;
} else {
document.getElementById('ranges_0').checked = 1;
}
}
// On page onload
$('#ranges_1').change(function() {
updateDynRanges($(this));
});
updateDynRanges($('ranges_1'));
}
</script>
If after testing works you can just add a visibility:false to the second checkbox.
Twig template:
{{ form_label(form.dynamicRanges) }}<br>
{{ form_widget(form.dynamicRanges[1]) }}
<div class="hidden">{{ form_widget(form.ranges[0]) }}</div>
Looks like an ugly workaround, but I just wanted to compete with the other ugly suggested workarounds, in this case mostly updating the twig template.

Symfony2 autocomplete form bundle

I use this bundle: GenemuFormBundle
I install it due to all information on this site.
But it still dont work.
Here is my type form:
$builder
->add('PermitsCompany', 'genemu_jqueryautocompleter_entity', array(
'route_name' => 'ajax_company',
'class' => 'MainCoreBundle:Company',
'property'=>'name'
))
;
Here is my routing:
ajax_company:
defaults: { _controller: MainAdminBundle:Permits:ajaxCompany}
pattern: /ajax_company/
type: annotation
And here is my controller:
/**
* #Route("/ajax_company", name="ajax_company")
*/
public function ajaxCompanyAction(Request $request)
{
$permits = $this->getDoctrine()->getRepository('MainCoreBundle:Company')->findAll();
$json = array();
foreach ($permits as $permit) {
$json[] = array(
'label' => $permit->getName(),
'value' => $permit->getId()
);
}
$response = new Response(json_encode($json));
$response->headers->set('Content-Type', 'application/json');
return $response;
}
I have no idea what I am doing wrong. I have no error. But autocomplete did not work.
When i go to route /ajax_company/ i can see values from data base like here:
[{"property":"Company 1","value":1},{"property":"Company 2","value":2},{"Company":"Company 3","value":3},{"property":"Company 4","value":4}]
Did I add forget something in twig? I have only form_widget
Try including form_javascript or form_stylesheet, in your twig template.
From https://github.com/genemu/GenemuFormBundle#template:
Template
You use GenemuFormBundle and you seen that it does not work! Maybe you
have forgotten form_javascript or form_stylesheet.
The principle is to separate the javascript, stylesheet and html. This
allows better integration of web pages.

Symfony2 - Dynamic form choices - validation remove

I have a drop down form element. Initially it starts out empty but it is populated with values via javascript after the user has made some interactions. Thats all working ok. However when I submit it always returns a validation error This value is not valid..
If I add the items to the choices list in the form code it will validate OK however I am trying to populate it dynamically and pre adding the items to the choices list is not going to work.
The problem I think is because the form is validating against an empty list of items. I don't want it to validate against a list at all. I have set validation required to false. I switched the chocie type to text and that always passes validation.
This will only validate against empty rows or items added to choice list
$builder->add('verified_city', 'choice', array(
'required' => false
));
Similar question here that was not answered.
Validating dynamically loaded choices in Symfony 2
Say you don't know what all the available choices are. It could be loaded in from a external web source?
after much time messing around trying to find it. You basically need to add a PRE_BIND listener. You add some extra choices just before you bind the values ready for validation.
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormEvent;
public function buildForm(FormBuilderInterface $builder, array $options)
{
// .. create form code at the top
$ff = $builder->getFormFactory();
// function to add 'template' choice field dynamically
$func = function (FormEvent $e) use ($ff) {
$data = $e->getData();
$form = $e->getForm();
if ($form->has('verified_city')) {
$form->remove('verified_city');
}
// this helps determine what the list of available cities are that we can use
if ($data instanceof \Portal\PriceWatchBundle\Entity\PriceWatch) {
$country = ($data->getVerifiedCountry()) ? $data->getVerifiedCountry() : null;
}
else{
$country = $data['verified_country'];
}
// here u can populate choices in a manner u do it in loadChoices use your service in here
$choices = array('', '','Manchester' => 'Manchester', 'Leeds' => 'Leeds');
#if (/* some conditions etc */)
#{
# $choices = array('3' => '3', '4' => '4');
#}
$form->add($ff->createNamed('verified_city', 'choice', null, compact('choices')));
};
// Register the function above as EventListener on PreSet and PreBind
// This is called when form first init - not needed in this example
#$builder->addEventListener(FormEvents::PRE_SET_DATA, $func);
// called just before validation
$builder->addEventListener(FormEvents::PRE_BIND, $func);
}
The validation is handled by the Validator component: http://symfony.com/doc/current/book/validation.html.
The required option in the Form layer is used to control the HTML5 required attribute, so it won't change anything for you, and that is normal.
What you should do here is to configure the Validation layer according to the documentation linked above.
Found a better solution which I posted here: Disable backend validation for choice field in Symfony 2 Type
Old answer:
Just spent a few hours dealing with that problem. This choice - type is really annoying. My solution is similar to yours, maybe a little shorter. Of course it's a hack but what can you do...
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('place', 'choice'); //don't validate that
//... more form fields
//before submit remove the field and set the submitted choice as
//"static" choices to make "ChoiceToValueTransformer" happy
$builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) {
$data = $event->getData();
$form = $event->getForm();
if ($form->has('place')) {
$form->remove('place');
}
$form->add('place', 'choice', array(
'choices' => array($data['place']=>'Whatever'),
));
});
}
Add this inside buildForm method in your form type class so that you can validate an input field value rather a choice from a select field value;
$builder->addEventListener(
FormEvents::PRE_SUBMIT,
function (FormEvent $event) {
$form = $event->getForm();
if ($form->has('verified_city')) {
$form->remove('verified_city');
$form->add(
'verified_city',
'text',
['required' => false]
)
}
}
);
Update in Validations.yml
Kindly update the Validation.yml file in the below format : setting the group names in the each field
password:
- NotBlank: { message: Please enter password ,groups: [Default]}
Update in Form Type
/**
* #param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver) {
$resolver->setDefaults(array(
'data_class' => 'RegistrationBundle\Entity\sf_members',
'validation_groups' => function(FormInterface $form){
$data = $form->getData();
$member_id = $data->getMemberId();
// Block of code;
// starts Here :
if( condition == 'edit profile') {
return array('edit');
} else
{
return array('Default');
}
},
Update in Entity
/**
* #var string
*
* #ORM\Column(name="password", type="text")
* #Assert\Regex(
* pattern="/(?i)^(?=.[a-zA-Z])(?=.\d).{8,}$/",
* match=true,
* message="Your password must be at least 8 characters, including at least one number and one letter",
* groups={"Default","edit"}
* )
*/
private $password;

Proper way to update class object in db using symfony2 + doctrine + form?

I have a simple class:
class Type
{
/**
* #ORM\Column(type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\Column(type="string", length=15)
*/
private $name;
...
}
And have a some 'type' objects in database.
So, if i want to change one of them, i create new controller rule (like /types/edit/{id}) and new action:
public function typesEditViewAction($id)
{
...
$editedType = new Type();
$form = $this->createFormBuilder($editedType)
->add('name', 'text')
->add('id', 'hidden', array('data' => $id))
->getForm();
// send form to twig template
...
}
After that, i create another controller rule (like /types/do_edit) and action:
public function typesEditAction(Request $request)
{
...
$editedType = new Type();
$form = $this->createFormBuilder($editedType)
->add('name', 'text')
->add('id', 'hidden')
->getForm();
$form->bind($request); // <--- ERROR THERE !!!
// change 'type' object in db
...
}
And i found a small problem there.
Сlass 'Type' doesn't have аuto-generated setter setId() and on binding i got error.
Neither the property "id" nor one of the methods "setId()", "__set()" or "__call()" exist and have public access in class "Lan\CsmBundle\Entity\Type".
Now, i remove 'id' field from symfony2 form object ($form) and transmit it manually to template.
At second controller's action i have $form object and 'id'-field apart.
I don't know a 'proper'-way for doing that (updating 'type' class). Please help.
Symfony has an integrated ParamConverter which automatically fetches your entity from database and throws an Exception ( which you can catch in a listener ) if the entity is not found.
You can easily handle GET and POST requests in one controller method.
make sure you have the public getters and setters for your properties in your entity.
I added annotations to make the routing clearer and still have a working example.
use Vendor\YourBundle\Entity\Type;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
// ...
/**
* #Route("/edit/{id}", requirements={"id" = "\d+"})
* #Method({"GET", "POST"})
*/
public function editAction(Request $request, Type $type)
{
$form = $this->createFormBuilder($type)
->add('name', 'text')
->add('id', 'hidden')
->getForm()
;
if ($request->isMethod('POST')) {
$form->bind($request);
if ($form->isValid())
{
$em = $this->getDoctrine()->getEntityManager();
$em->flush(); // entity is already persisted and managed by doctrine.
// return success response
}
}
// return the form ( will include the errors if validation failed )
}
I strongly suggest you should create a form type to further simplify your controller.
For anyone else stumbling on this where you added the ID field to your FormType because the frontend needed it you can just set the ID column to "not-mapped" like so:
->add('my_field', 'hidden', ['mapped'=>false])
and it prevents the ID value trying to get used by the form processing method.