How to restrict modification to selected properties of an entity while persisting? - php

Let's suppose I have an entity class and a generic FormType corresponding to the entity
class Entry {
protected $start_time;
protected $end_time;
protected $confirmed; // Boolean
// ... Other fields and getters and setters
}
On the CRUD of this entity, I don't want to allow any modification on start_time or end_time if the entity has been confirmed or in the above case when $confirmed === true
On the twig file, I disable the fields I want to restrict, like the following:
{% if entity.approved == true %}
{{ form_row(entity.start_time), { 'attr' : { 'disabled' : 'disabled' } }) }}
{% endif %}
{# Sameway for another field #}
Now the problem is that this is a front end resolution which can be tampered very easily using web developer tools in web browsers now. But regardless what I am trying to achieve is not have those two fields changed once the entity in confirmed.
So, one way I tried was after the form was submitted, I check if the entity was confirmed and if it was was, I fetch the earlier state of the entity and set the value of the new one (which is about to be persisted) with the values from old one.
On Controller:
$confirmed = $entity->getConfirmed();
$form->handleRequest($request);
if($form->isSubmitted() && $editForm->isValid()) {
// The form was submitted
if($confirmed === true) { // if the entity was confirmed previously
$oldEntity = $em->getRepository('...')->find($entity->getId());
$entity->setStartTime($oldEntity->getStartTime());
$entity->setEndTime($oldEntity->getEndTime());
}
$em->persist($entity);
$em->flush();
}
The problem here was $oldEntity was exactly same as $entity. My guess is doctrine picked up that it already has the entity that is being asked and just returned me with the same object. Anyways, my attempt to solve this problem failed.
Any idea how to restrict/revert changes on selected properties while allowing changes on rest of the properties of the entity?
Update:
Modifying the form type to disable the field is not an option because I only want them to be read-only/disabled only if entity is confirmed and rest of time I want the form to be as it is.

You must add attribute 'disabled' => true in form builder, not only in twig.
If you don't want a user to modify the value of a field, you can set the disabled option to true. Any submitted value will be ignored.
Reference: http://symfony.com/doc/current/reference/forms/types/form.html#disabled
If you wish modify dynamically, use form events, example:
$builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) {
$form = $event->getForm();
$entity = $event->getData();
// if confirmed, disable start_time field
if ($entity->getConfirmed()) {
$config = $form->get('start_time')->getConfig();
$options = $config->getOptions();
// set disabled option to true
$options['disabled'] = true;
// replace origin field
$form->add(
'start_time',
$config->getType()->getName(),
$options
);
}
});

I think I understand your problem now #Starx, I didn't read very carefully at first, and your update helped.
Maybe you need to detach your Entity?
Check this link about (Entities in Session)[http://doctrine-orm.readthedocs.io/projects/doctrine-orm/en/latest/cookbook/entities-in-session.html]. Maybe storing the Entity in a session will work. Detaching as a separate Entity might work, and do a comparison on the detached Entity to your updated Entity.

I found two ways to solve this:
You can retrieve original data of the entity. It returns an array with old data of an entity which can be used to reset the data.
if($form->isSubmitted() && $editForm->isValid()) {
// The form was submitted
if($confirmed === true) { // if the entity was confirmed previously
$oldData = $em->getUnitOfWork()->getOriginalEntityData($entity);
$entity->setStartTime($oldData['start_time']);
$entity->setEndTime($oldData['end_time']);
}
$em->persist($entity);
$em->flush();
}
Using Form Events
The following is a Symfony 3 Solution, try maximkou's answer for Symfony 2.
class EntityType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
// ....
// ....
// Your form fields
$builder->addEventListener(FormEvents::POST_SET_DATA, array($this, 'onPreSetData'));
}
public function onPreSetData(FormEvent $event) {
/** #var YourEntity $entity */
$entity = $event->getData();
$form = $event->getForm();
if($entity instanceof YourEntity) {
if ($entity->getTimesheetEntry()->getTimeApproved() === true) {
$config = $form->get('start_time')->getConfig();
$options = $config->getOptions();
$options['disabled'] = true;
$form->add('start_time', get_class($config->getType()->getInnerType()), $options);
$config = $form->get('end_time')->getConfig();
$options = $config->getOptions();
$options['disabled'] = true;
$form->add('end_time', get_class($config->getType()->getInnerType()), $options);
}
}
}
}
Source

Related

Checking which form field value has changed Symfony 3

I need to check inside the FormType which field has changed. Is there any method to do it? I've searched for a while, then tried to get edited entities field in few ways (with form events too) to catch the edited fields, but no simple result.
Is there any way to do it easy, or I need to be more creative in making such thing? The best it would be, if I can get an example with entity type, but any clue would be great.
P.S. I cant do it on client-side - I must do it on server side for particular reason.
Done with this:
https://stackoverflow.com/a/33923626/8732955
Suppose we want to check the "status" field in our ImportantObject, code needs to look like that
if($form->isSubmitted() && $form->isValid())
{
$uow = $em->getUnitOfWork();
$uow->computeChangeSets();
$changeSet = $uow->getEntityChangeSet($importantObject);
if(isset($changeSet['status'])){
//do something with that knowledge
}
}
Old post but interesting question.
How I solved it to check a relation between entities but it also works for a single field value. Easier than dealing with doctrine listeners.
Imagine you have a user with multiple tags and a form with checkboxes to add or remove tags
In the controller, create a new variable that contains the value to monitor :
$oldValue = '';
foreach ( $user->getTags() as $tag )
$oldValue .= $tag->getId().";";
Give it to the formType as an option
$form = $this->get('form.factory')->create(userType::class, $user,
['oldValue' => $oldValue ]);
In the formType, create an hidden field
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
public function buildForm(FormBuilderInterface $builder, array $options)
....
$oldValue = $options['oldValue'];
$builder
->add('oldValue', HiddenType::class, [
'data' => $oldValue,
'mapped' => false,
]);
...
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'pathToEntity',
'oldValue' => null,
));
}
Back in the controller get your old field value :
if ( $form->isSubmitted() && $form->isValid() )
{
// Stuff
$em->flush();
// Check changes :
$oldValue = $form->get('oldValue')->getData();
$oldValues = explode(';', $oldValue);
$newValues = $user->getTags();
Compare arrays and finish the stuff...

Error added to Symfony 3 form element is overwriten/deleted

I have Symfony form with PRE_SUBMIT listener.
The listener checks string format for one field.
class TestEntType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('someText', TextType::class, ['error_bubbling' => false]);
$builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) {
$data = $event->getData();
$form = $event->getForm();
...
if (false === $this->isFormattedCorrectly($data['someText'])) {
$form->get('someText')
->addError(new FormError('Text format should be...'));
}
});
}
...
}
If the listener is written this way - form is valid, even if I force adding error every single time.
It seems that error is somewhere overwritten or deleted.
But if I make small change, and convert $form->get('someText')->addError(..) to $form->addError(..), form isn't valid any more.
If I dump (string)$form->getErrors(true) in the controller, I get ERROR: Text format should be...
So, the error isn't lost in this situation.
But the problem is that this error isn't bounded to someText field!
This is the controller:
class TestEntController extends Controller
{
/**
* #Route("/test-ent", name="test-ent")
* #Method("POST")
*/
public function testAction(Request $request)
{
$testEnt = new TestEnt();
$form = $this->createForm(TestEntType::class, $testEnt);
$data = json_decode($request->getContent(), true);
if ($data === null) {
//...
}
$form->submit($data);
if (!$form->isValid()) {
// ...throw $this->throwApiProblemValidationException($form);
}
$em = $this->getDoctrine()->getManager();
$em->persist($testEnt);
$em->flush();
return new JsonResponse($testEnt);
}
}
Has someone idea where error disappears when I bound it to the specific field?
Is possible to bound error to the specific field in PRE_SUBMIT form listener?
Is there some smarter way to check is string formatted correctly?
I was following directions from this SO question Add error to Symfony 2 form element
EDIT: It seems that everything normally works if I listen for FormEvents:: SUBMIT event. In that case, error isn't lost.
To figure out what is happening, I'll have to in detail investigate how Symfony form works...

Add a required form field based on submitted data in Symfony2

I have a form with a status select. If a certain status is selected and the form is submitted it should reload and require an additional field.
I have read Dynamic generation for submitted Forms and almost every other post on the internet and about this topic and tried different event combinations (and got different errors) but I still struggle to make this to work correctly.
This is what I have so far:
FormType
private function addProcessAfterField(FormInterface $form)
{
$form->add('processAfterDate', 'date', array('required' => true));
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('status', 'entity', array(
'class' => 'Acme\Bundle\ApplicationBundle\Entity\LeadStatusCode',
'choices' => $this->allowedTypes
));
$builder->addEventListener(FormEvents::PRE_SET_DATA, function(FormEvent $event){
$form = $event->getForm();
$data = $event->getData();
if ($data->getStatus()->getId() == LeadStatusCode::INTERESTED_LATER) {
$this->addProcessAfterField($form);
}
});
$builder->get('status')->addEventListener(FormEvents::POST_SUBMIT, function(FormEvent $event){
$data = $event->getData();
if ($data == LeadStatusCode::INTERESTED_LATER && !$event->getForm()->getParent()->getData()->getProcessAfterDate()) {
$this->addProcessAfterField($event->getForm()->getParent());
}
});
$builder->add('comment', 'textarea', array('mapped' => false));
$builder->add('Update', 'submit');
}
Error:
ContextErrorException: Catchable Fatal Error: Argument 1 passed to Proxies\__CG__\Acme\Bundle\ApplicationBundle\Entity\Lead::setProcessAfterDate() must be an instance of DateTime, null given, called in /var/www/application.dev/vendor/symfony/symfony/src/Symfony/Component/PropertyAccess/PropertyAccessor.php on line 360 and defined in /var/www/application.dev/app/cache/dev/doctrine/orm/Proxies/__CG__AcmeBundleApplicationBundleEntityLead.php line 447
As already mentioned I tried different event combinations, one was almost working but then the date was never persisted to the entity so I added the \DateTime type-hint to the setProcessAfterDate() method. I am not sure if I don`t understand the event system correctly or if the error lies somewhere else.
Well, it might not be the best way to solve it, but to make long story short:
$form->handleRequest($request);
if($form->isValid()) // check if the basic version of the form is ok
{
$form = $this->createForm(new XXXXForm(), $form->getData()); // you recreate the form with the data that was submitted, so you rebuild the form with new data
if($form->isValid())
{
// ok
}
// not ok
}
Then inside buildForm function, you base the "required" attribute value of fields based on what you want:
'required' => $this->getCheckRequired($options)
private function getCheckRequired($options) // checks whether field should be required based on data bound to the form
{
if($options && isset($options['data'])
{
switch $options['data']->getStatus():
// whatever
;
}
return false;
}
As I said, this is not the best solution, and it doesn't fix your approach, but rather proposes a different one, but it does the job

Symfony2 form - how overwrite field with default value

I have a form with one default value:
class GearType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('options')
->add('model', 'choice', array('choices' => $this->getModelChoices(), 'data' => 2));
}
one of the requirements is form can be pre-populated by re-sellers by passing parameters in URL. It is also nice feature for potential customers to copy and paste link to email, communicators, etc.
I did it this way:
/**
* #Route("/car/gear")
* #Template()
*/
public function gearAction(Request $request)
{
$form = $this->createForm(new GearType());
if ($request->isMethod('POST')) {
$form->bind($request);
if ($form->isValid()) {
return 'is valid';
}
} else {
$get = $this->getRequest()->query->all();
if (!empty($get)) {
$normalizer = new GetSetMethodNormalizer();
$form->setData($normalizer->denormalize($get, new Gear())); # look here
}
}
return array('form' => $form->createView());
}
unfortunately field 'options' has always default value, instead value passed as a parameter.
I have tried to change line # look here into
$gear = $normalizer->denormalize($get, new Gear());
$form = $this->createForm(new GearType(), $gear);
but no result.
It seems that solution is passing additional parameter to GearType object. I do not like this solution. Does anyone know better way?
Add this snippet, and modifiy between the [ ] as appropriate
$form->bind($request);
if ( [ passed parameters from querystring ] ){ //// New Code
$form->getData()->setOptions( [ processed parameter ]); //// New Code
} //// New Code
if ($form->isValid()) {
return 'is valid';
}
The reason for the field options always having default value may be the actual query. Instead of denormalizing and setting the data directly, modify else fragment to:
} else {
$form = $this->createForm(new GearType(), new Gear(), array(
'validation_groups' => array('not-validating')
));
$form->bind($request);
}
The form will validate only against validations associated with the not-validating group, which will avoid showing the common required alerts if the form is built form GET.
Docs about 'validations-groups': http://symfony.com/doc/current/book/forms.html#validation-groups
The question is similar to: Entity form field and validation in Symfony2?

How to add some extra data to a symfony 2 form

I have a form for my entity called Book and I have a type to display a form in my view. In this type I have some fields that are mapped to properties in my entity.
Now I want to add another field which is not mapped in my entity and supply some initial data for that field during form creation.
My Type looks like this
// BookBundle\Type\Book
public function buildForm(FormBuilderInterface $builder, array $options = null)
{
$builder->add('title');
$builder->add('another_field', null, array(
'mapped' => false
));
}
The form is created like this
$book = $repository->find(1);
$form = $this->createForm(new BookType(), $book);
How can I supply some initial data now during form creation? Or how do I have to change that creation of the form to add initial data to the another_field field?
I also have a form that has fields that mostly match a previously defined entity, but one of the form fields has mapped set to false.
To get around this in the controller, you can give it some initial data pretty easily like this:
$product = new Product(); // or load with Doctrine/Propel
$initialData = "John Doe, this field is not actually mapped to Product";
$form = $this->createForm(new ProductType(), $product);
$form->get('nonMappedField')->setData($initialData);
simple as that. Then when you're processing the form data to get ready to save it, you can access the non-mapped data with:
$form->get('nonMappedField')->getData();
One suggestion might be to add a constructor argument (or setter) on your BookType that includes the "another_field" data, and in the add arguments, set the 'data' parameter:
class BookType
{
private $anotherFieldValue;
public function __construct($anotherFieldValue)
{
$this->anotherFieldValue = $anotherFieldValue;
}
public function buildForm(FormBuilderInterface $builder, array $options = null)
{
$builder->add('another_field', 'hidden', array(
'property_path' => false,
'data' => $this->anotherFieldValue
));
}
}
Then construct:
$this->createForm(new BookType('blahblah'), $book);
You can change the request parameters like this to support the form with additional data:
$type = new BookType();
$data = $this->getRequest()->request->get($type->getName());
$data = array_merge($data, array(
'additional_field' => 'value'
));
$this->getRequest()->request->set($type->getName(), $data);
This way your form will fill in the correct values for your field at rendering. If you want to supply many fields this may be an option.

Categories