I’m developing a form in zf2 and I want to calculate a value based upon user input and set it in a field after the form has validated. In the form, there is a firstName field and a lastName field; and I want to use the validated input to calculate a value to populate in a fullName field.
I assume I want to set the value something like this, but haven’t found the right code for setting the "element" that gets sent to the database:
public function addAction()
{
$objectManager = $this->getServiceLocator()->get('Doctrine\ORM\EntityManager');
$form = new AddMemberForm($objectManager);
$member = new Member();
$form->bind($member);
$request = $this->getRequest();
if ($request->isPost()) {
$form->setData($request->getPost());
if ($form->isValid()) {
// develop full name string and populate the field
$calculatedName = $_POST['firstName'] . " " . $_POST['lastName'];
$member->setValue('memberFullName', $calculatedName);
$this->getEntityManager()->persist($member);
$this->getEntityManager()->flush();
return $this->redirect()->toRoute('admin-members');
}
}
return array('form' => $form);
}
Doctrine's built-in Lifecycle Callbacks are perfectly fits for handling such requirement and I strongly recommend to use them.
You just need to correctly annotate the entity.
For example:
<?php
/**
* Member Entity
*/
namespace YourNamespace\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\Event\LifecycleEventArgs;
/**
* #ORM\Table(name="members")
* #ORM\Entity
* #ORM\HasLifecycleCallbacks
*/
class Member
{
// After all of your entity properies, getters and setters... Put the method below
/**
* Calculate full name on pre persist.
*
* #ORM\PrePersist
* #return void
*/
public function onPrePersist(LifecycleEventArgs $args)
{
$this->memberFullName = $this->getFirstName().' '.$this->getLastName();
}
}
With this way, memberFullName property of the entity will be automatically populated using first and last names on entity level just before persisting.
Now you can remove the lines below from your action:
// Remove this lines
$calculatedName = $_POST['firstName'] . " " . $_POST['lastName'];
$member->setValue('memberFullName', $calculatedName);
Foozy’s excellent answer provides a solution that works for an add action, and the response to my comment directed me to the following solution that works for both add and edit actions:
<?php
/**
* Member Entity
*/
namespace YourNamespace\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\Event\PreFlushEventArgs;
/**
* #ORM\Table(name="members")
* #ORM\Entity
*/
class Member
{
// After all of your entity properies, getters and setters... Put the method below
/**
* Calculate full name on pre flush.
*
* #ORM\PreFlush
* #return void
*/
public function onPreFlush(PreFlushEventArgs $args)
{
$this->memberFullName = $this->getFirstName().' '.$this->getLastName();
}
}
Related
I want to implement an eval in TYPO3 for a specific field in a TCA that prevents the record to be saved in the backend if the BE user enters a restricted value (like if doesn't enter anything or the input contains specific characters). So far i can only change the value in the eval function if it's not allowed, but i want the record NOT to be saved if the BE user clicks on 'save' while the field has restricted value. Is this even possible via an eval?
Edit: A cheap way to do this would be setting $value to NULL in the eval function if the input is of a restricted value, but that's definitely not the elegant way to do things as it throws an SQL error that might confuse the BE user.
So i basically need a way to prevent TYPO3 to persist the repository... Or to set the record back to the state it was in before the BE user clicked on 'save'...
Edit2: This is what i have... Nothing to exciting, just an IPv4 evaluation. But again, it changes only the value to something else, it DOESN'T prevent the creation or editing of the record if the input was not an IPv4.
<?php
namespace Cjk\Icingaconfgen\Evaluation;
use TYPO3\CMS\Core\Messaging\FlashMessage;
use TYPO3\CMS\Core\Messaging\FlashMessageService;
use TYPO3\CMS\Core\Utility\GeneralUtility;
/**
* Class for field value validation/evaluation to be used in 'eval' of TCA
*/
class IPv4Evaluation
{
/**
* #param string $value
* #param string $is_in
* #param bool $set
* #return string
*/
public function evaluateFieldValue($value, $is_in, &$set)
{
if (!filter_var($value, FILTER_VALIDATE_IP)){
$value = 'Fehlerhafte Eingabe (IPv4)';
/** #var FlashMessage $message */
$message = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Messaging\\FlashMessage',
'Fehlerhafte Eingabe: .conf Datei wird nicht erstellt/editiert. Neue services können nicht hinzugefügt oder editiert werden.',
\TYPO3\CMS\Core\Messaging\FlashMessage::ERROR,
TRUE
);
/** #var $flashMessageService FlashMessageService */
$flashMessageService = GeneralUtility::makeInstance(FlashMessageService::class);
$flashMessageService->getMessageQueueByIdentifier()->enqueue($message);
}
return $value;
}
}
So basically, and correct me if i'm wrong, the eval happens BETWEEN the input of the BE user (right after he clicks on the save button) and the persisting of the record. So there has to be a way to prevent the persisting of the new data, not only to change it at will.
I hope this makes my question more clear now, i don't know what else to write to explain it.
I show you a example of a validation in TCA. If validation failed record can't saved.
File myextension/Classes/Validation/Validator/MinReferencesValidator.php
<?php
namespace Vendor\Myextension\Validation\Validator;
/**
* Validator for min references
*/
class MinReferencesValidator extends \TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator {
/**
* #var array
*/
protected $supportedOptions = [
'min' => [0, 'The minimum references to accept', 'integer'],
];
/**
* The given value is valid if it contains more then min items
*
* #param mixed $value The value that should be validated
* #return void
*/
public function isValid($value) {
if (!is_object($value)) {
$this->addError(
$this->translateErrorMessage(
'LLL:EXT:myextension/Resources/Private/Language/locallang.xlf:validator_object_notvalid',
'myextension'
), 1489870657);
return;
}
$minimum = $this->options['min'];
$countItems = count($value);
if ($countItems < $minimum) {
$this->addError(
$this->translateErrorMessage(
'LLL:EXT: myextension/Resources/Private/Language/locallang.xlf:validator_min_references',
'myextension',
[
$minimum
]
), 1489872300, [$minimum]);
return;
}
}
}
File myextension/Classes/Domain/Model/Youritem.php
/**
* Image of supplier (image reference)
*
* #var \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\TYPO3\CMS\Extbase\Domain\Model\FileReference>
* #validate NotEmpty, \Vendor\Yourextension\Validation\Validator\MinReferencesValidator(min=1)
*/
protected $images;
I'm trying to make an API Rest in Symfony2 using Parse as cloud database.
If I try to retrieve the Parse users it works fine and returns the expected data.
Local url example: http://www.foo.local/app_dev.php/getUsers/
Here is the code I use in the Users controller (I use annotations in order to set the routes in the controller):
namespace Foo\ApiBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use FOS\RestBundle\Controller\Annotations\View;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Request;
use Parse\ParseClient;
use Parse\ParseObject;
use Parse\ParseQuery;
use Parse\ParseUser;
class UsersController extends Controller
{
/**
* #return array
* #View()
* #Route("/getUsers/")
*/
public function getUsersAction(Request $request) {
ParseClient::initialize(<my Parse keys>);
$query = ParseUser::query();
$results = $query->find();
return array('users' => $results);
}
}
However if I try the same with my Products ParseObjects, I get the following error message:
error code="500" message="Internal Server Error" exception
class="Doctrine\Common\Annotations\AnnotationException"
message="[Semantical Error] The annotation "#returns" in method
Parse\ParseFile::getData() was never imported. Did you maybe forget to
add a "use" statement for this annotation?"
Local url example: http://www.foo.local/app_dev.php/getProducts/
The Products controller code:
namespace Foo\ApiBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use FOS\RestBundle\Controller\Annotations\View;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Request;
use Parse\ParseClient;
use Parse\ParseObject;
use Parse\ParseQuery;
use Parse\ParseUser;
use Parse\ParseFile;
class ProductsController extends Controller
{
/**
* #return array
* #View()
* #Route("/getProducts/")
*/
public function getProductsAction(Request $request) {
ParseClient::initialize(<my Parse keys>);
$query = new ParseQuery("Products");
$results = $query->find();
return array('products' => $results);
}
}
If instead of returning $results I return other dummy data, like return array('products' => 'fooProducts'), I no longer get the error message.
Also if I make a var_dump of the $results variable, I get the expected array of ParseObjects.
Here is my routing.yml file in case there is something wrong with it:
api:
resource: "#FooApiBundle/Controller/"
type: annotation
prefix: /
users:
type: rest
resource: Foo\ApiBundle\Controller\UsersController
products:
type: rest
resource: Foo\ApiBundle\Controller\ProductsController
By the error message it seems that the problem is related to Doctrine, but since I'm not using it, I don't know exactly how there can be a conflict or how to fix it. Any suggestions?
There are a few DocBlock typos of #returns in the Parse\ParseFile class that is causing Doctrine's Annotations class to attempt to identify them as a class. This is not your fault but a bug in the Parse PHP SDK library.
I've made a fix in this commit and submitted a pull request back to the original devs, so it should be a simple matter of eventually running composer update to bring your Parse library to the latest correct version.
You can read more about DocBlock and the part specifically on Annotations here
Here is a copy/paste of the resulting diff for src/Parse/ParseFile.php:
## -31,7 +31,7 ## class ParseFile implements \Parse\Internal\Encodable
/**
* Return the data for the file, downloading it if not already present.
*
- * #returns mixed
+ * #return mixed
*
* #throws ParseException
*/
## -50,7 +50,7 ## public function getData()
/**
* Return the URL for the file, if saved.
*
- * #returns string|null
+ * #return string|null
*/
public function getURL()
{
## -112,7 +112,7 ## public function getMimeType()
* #param string $name The file name on Parse, can be used to detect mimeType
* #param string $mimeType Optional, The mime-type to use when saving the file
*
- * #returns ParseFile
+ * #return ParseFile
*/
public static function createFromData($contents, $name, $mimeType = null)
{
## -132,7 +132,7 ## public static function createFromData($contents, $name, $mimeType = null)
* #param string $name Filename to use on Parse, can be used to detect mimeType
* #param string $mimeType Optional, The mime-type to use when saving the file
*
- * #returns ParseFile
+ * #return ParseFile
*/
public static function createFromFile($path, $name, $mimeType = null)
{
The correct way to initialize Parse using Symfony, is on the setContainer method of your controller:
class BaseController extends Controller
{
....
public function setContainer(ContainerInterface $container = null)
{
parent::setContainer( $container );
ParseClient::initialize( $app_id, $rest_key, $master_key );
}
}
Depending of your needs, you can create a BaseController and extend it in your rest of controllers.
class UsersController extends Controller
In addition, you could add your keys in the parameters.yml file.
parameters:
#your parameters...
ParseAppId: your_id
ParseRestKey: your_rest_key
ParseMasterKey: your_master_key
TIP: Note you can add have different Parse projects (dev and release
version). Add your parameters in your different parameters
configuration provides an easy way to handle this issue.
class BaseController extends Controller
{
....
public function setContainer(ContainerInterface $container = null)
{
parent::setContainer( $container );
$app_id = $container->getParameter('ParseAppId');
$rest_key = $container->getParameter('ParseRestKey');
$master_key = $container->getParameter('ParseMasterKey');
ParseClient::initialize( $app_id, $rest_key, $master_key );
}
}
I've got some issues with Symfony's form validation handling. I'd like to validate a form bound to an entity based on its data. There are quite a bunch of information how to dynamically modify the form fields using FormEvents. What I'm missing on this topic is how to control/modify the validation.
My simplified use case is:
A user can add an event to a calendar.
The validation checks if there's already an event.
If there's a collision, the validation will throw an error.
The user should now be able to ignore this error/warning.
The validation is implemented as a Validator with Constraint::CLASS_CONSTRAINT as the target (as it's taking some more stuff into account).
I tried to:
Hack around the validation groups, but couldn't find access to the entity wide validators.
Hack around the FormEvents and add an extra field like "Ignore date warning".
Hack around the submit button to change it to something like "Force submit".
... but never found a working solution. Even simpler hacks with a single property based validator didn't work out. :(
Is there a Symfony way to dynamically control the validation?
Edit: My code looks like this:
use Doctrine\ORM\Mapping as ORM;
use Acme\Bundle\Validator\Constraints as AcmeAssert;
/**
* Appointment
*
* #ORM\Entity
* #AcmeAssert\DateIsValid
*/
class Appointment
{
/**
* #ORM\Column(name="title", type="string", length=255)
*
* #var string
*/
protected $title;
/**
* #ORM\Column(name="date", type="date")
*
* #var \DateTime
*/
protected $date;
}
The validator used as a service:
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
/**
* Validates the date of an appointment.
*/
class DateIsValidValidator extends ConstraintValidator
{
/**
* {#inheritdoc}
*/
public function validate($appointment, Constraint $constraint)
{
if (null === $date = $appointment->getDate()) {
return;
}
/* Do some magic to validate date */
if (!$valid) {
$this->context->addViolationAt('date', $constraint->message);
}
}
}
The corresponding Constraint class is set to target the entity class.
use Symfony\Component\Validator\Constraint;
/**
* #Annotation
*/
class DateIsValid extends Constraint
{
public $message = 'The date is not valid!';
/**
* {#inheritdoc}
*/
public function getTargets()
{
return self::CLASS_CONSTRAINT;
}
/**
* {#inheritdoc}
*/
public function validatedBy()
{
return 'acme.validator.appointment.date';
}
}
Edit 2: Try with FormEvents... I also tried all the different events.
$form = $formFactory->createBuilder()
->add('title', 'text')
->add('date', 'date')
->addEventListener(FormEvents::WHICHONE?, function(FormEvent $event) {
$form = $event->getForm();
// WHAT TO DO HERE?
$form->getErrors(); // Is always empty as all events run before validation?
// I need something like
if (!$dateIsValid) {
$form->setValidationGroup('ignoreWarning');
}
});
Edit 3: Constraint are correctly declared. That's not the issue:
services:
validator.acme.date:
class: AcmeBundle\Validator\Constraints\DateValidator
arguments: ["#acme.other_service"]
tags:
- { name: validator.constraint_validator, alias: acme.validator.appointment.date }
Validation is done on the entity, all Forms does is execute the Object's validations.
You can choose groups based on submitted data
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'validation_groups' => function(FormInterface $form) {
$data = $form->getData();
if (Entity\Client::TYPE_PERSON == $data->getType()) {
return array('person');
} else {
return array('company');
}
},
));
}
I have had issues when using this approach on embedded forms && cascade-validation
Edit: using flash to determine if validation must take place.
// service definition
<service id="app.form.type.callendar" class="%app.form.type.callendar.class%">
<argument type="service" id="session" />
<tag name="form.type" alias="my_callendar" />
</service>
// some controller
public function somAvtion()
{
$form = $this->get('app.form.type.callendar');
...
}
// In the form
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'validation_groups' => function(FormInterface $form) {
$session = $form->getSession();
if ($session->getFlashBag()->get('callendar_warning', false)) {
return array(false);
} else {
return array('Validate_callendar');
}
},
));
}
How does your user interact with the application to tell it to ignore the warning? Is there some kind of additional button?
In that case you could simply check the button used for submitting the form or add some kind of hidden field (ignore_validation) etc.
Wherever you end up getting that user input from (flash and dependency injection, based on submitted data etc.), I would then use validation groups and a closure to determine what to validate (just like juanmf explained in his answer).
RE your second approach (Form Events), you can add a priority to event listeners: As you can see in Symfony's Form Validation Event Listener, they use FormEvents::POST_SUBMIT for starting the validation process. So if you just add an event listener, it gets called before the validation listener and so no validation has happened yet.
If you add a negative priority to your listener, you should be able to also access the form validation errors:
$builder->addEventListener(FormEvents::POST_SUBMIT, function(){...}, -900);
Old question but...
I would first add a field (acceptCollision) in the form as suggested by you and other answers above.
Then you validator can do something like:
public function validate($appointment, Constraint $constraint)
{
if (null === $date = $appointment->getDate()) {
return;
}
if ($appointment->getAcceptCollision()) {
$valid = true;
} elseif (
// Check Unicity of the date (no collision)
) {
$valid = true;
} else {
$valid = false;
}
if (!$valid) {
$this->context->addViolationAt('date', $constraint->message);
}
}
I think you run into a problem because you are using the wrong concept. The decision which validation should be running belongs to the controller, not the validator.
So I would simply check in the controller which submit button is pressed (or weither there is a checkbox checked) and switch validation groups. However the form should be visually different, so I would probably create 2 forms for both states (both extend a base one or one form type that use options).
I am prototyping a REST API in Symfony2 with FOSRestBundle using JMSSerializerBundle for entity serialization. With GET request I can use the ParamConverter functionality of SensioFrameworkExtraBundle to get an instance of an entity based on the id request parameter and when creating a new entity with POST request I can use the FOSRestBundle body converter to create a new instance of the entity based on the request data. But when I want to update an existing entity, using the FOSRestBundle converter gives an entity without id (even when the id is sent with the request data) so if I persist it, it will create a new entity. And using SensioFrameworkExtraBundle converter gives me the original entity without the new data so I would have to manually get the data from the request and call all the setter methods to update the entity data.
So my question is, what is the preferred way to handle this situation? Feels like there should be some way to handle this using the (de)serialization of the request data. Am I missing something related to the ParamConverter or JMS serializer that would handle this situation? I do realize that there are many ways to do this kind of things and none of them are right for every use case, just looking for something that fits this kind of rapid prototyping you can do by using the ParamConverter and minimal code required to be written in the controllers/services.
Here is an example of a controller with the GET and POST actions as described above:
namespace My\ExampleBundle\Controller;
use My\ExampleBundle\Entity\Entity;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Validator\ConstraintViolationListInterface;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use FOS\RestBundle\Controller\Annotations as Rest;
use FOS\RestBundle\View\View;
class EntityController extends Controller
{
/**
* #Route("/{id}", requirements={"id" = "\d+"})
* #ParamConverter("entity", class="MyExampleBundle:Entity")
* #Method("GET")
* #Rest\View()
*/
public function getAction(Entity $entity)
{
return $entity;
}
/**
* #Route("/")
* #ParamConverter("entity", converter="fos_rest.request_body")
* #Method("POST")
* #Rest\View(statusCode=201)
*/
public function createAction(Entity $entity, ConstraintViolationListInterface $validationErrors)
{
// Handle validation errors
if (count($validationErrors) > 0) {
return View::create(
['errors' => $validationErrors],
Response::HTTP_BAD_REQUEST
);
}
return $this->get('my.entity.repository')->save($entity);
}
}
And in config.yml I have the following configuration for FOSRestBundle:
fos_rest:
param_fetcher_listener: true
body_converter:
enabled: true
validate: true
body_listener:
decoders:
json: fos_rest.decoder.jsontoform
format_listener:
rules:
- { path: ^/api/, priorities: ['json'], prefer_extension: false }
- { path: ^/, priorities: ['html'], prefer_extension: false }
view:
view_response_listener: force
If you are using PUT, according to REST, you should use a route for the update with the id of the entity in question in the route itself like /entity/{entity}. FOSRestBundle does it that way too.
In your case this should be something like:
/**
* #Route("/{entityId}", requirements={"entityId" = "\d+"})
* #ParamConverter("entity", converter="fos_rest.request_body")
* #Method("PUT")
* #Rest\View(statusCode=201)
*/
public function putAction($entityId, Entity $entity, ConstraintViolationListInterface $validationErrors)
EDIT: It would actually be even better to have two entities injected. One being the current database state and one being the sent data from the client. You can achieve this with two ParamConverter-annotations:
/**
* #Route("/{id}", requirements={"id" = "\d+"})
* #ParamConverter("entity")
* #ParamConverter("entityNew", converter="fos_rest.request_body")
* #Method("PUT")
* #Rest\View(statusCode=201)
*/
public function putAction(Entity $entity, Entity $entityNew, ConstraintViolationListInterface $validationErrors)
This will load the current db state into $entity and the uploaded data into $entityNew. Now you can merge the data as you see fit.
If it's fine for you to just overwrite the data without merging/checking, then use the first option. But keep in mind that this would allow creating a new entity if the client sends a not yet used id if you do not prevent that.
Seems one way would be to use Symfony Form component (with SimpleThingsFormSerializerBundle) as described in http://williamdurand.fr/2012/08/02/rest-apis-with-symfony2-the-right-way/#post-it
Quote from SimpleThingsFormSerializerBundle README:
Additionally all the current serializer components share a common flaw: They cannot deserialize (update) into existing object graphs. Updating object graphs is a problem the Form component already solves (perfectly!).
I also had a problem with the processing of PUT requests using JMS serializer. First of all I would like to automate the processing of queries using the serializer. The put request may not contain the complete data. Part of the data must be map on entity. You can use my simple solution:
/**
* #Route(path="/edit",name="your_route_name", methods={"PUT"})
*
* This parameter is using for creating a current fields of request
* #RequestParam(
* name="id",
* requirements="\d+",
* nullable=false,
* allowBlank=true,
* strict=true,
* )
* #RequestParam(
* name="some_field",
* requirements="\d{13}",
* nullable=true,
* allowBlank=true,
* strict=true,
* )
* #RequestParam(
* name="some_another_field",
* requirements="\d{13}",
* nullable=true,
* allowBlank=true,
* strict=true,
* )
* #param Request $request
* #param ParamFetcher $paramFetcher
* #return Response
*/
public function editAction(Request $request, ParamFetcher $paramFetcher)
{
//validate parameters
$paramFetcher->all();
/** #var EntityManager $em */
$em = $this->getDoctrine()->getManager();
$yourEntity = $em->getRepository('YourBundle:SomeEntity')->find($paramFetcher->get('id'));
//get request params (param fetcher has all params, but we need only params from request)
$data = $request->request->all();
$this->mapDataOnEntity($data, $yourEntity, ['some_serialized_group','another_group']);
$em->flush();
return new JsonResponse();
}
Method mapDataOnEntity you can locate in some trait or in you intermediate controller class. Here is his implementation of this method:
/**
* #param array $data
* #param object $targetEntity
* #param array $serializationGroups
*/
public function mapDataOnEntity($data, $targetEntity, $serializationGroups = [])
{
/** #var object $source */
$sourceEntity = $this->get('jms_serializer')
->deserialize(
json_encode($data),
get_class($targetEntity),
'json',
DeserializationContext::create()->setGroups($serializationGroups)
);
$this->fillProperties($data, $targetEntity, $sourceEntity);
}
/**
* #param array $params
* #param object $targetEntity
* #param object $sourceEntity
*/
protected function fillProperties($params, $targetEntity, $sourceEntity)
{
$propertyAccessor = new PropertyAccessor();
/** #var PropertyMetadata[] $propertyMetadata */
$propertyMetadata = $this->get('jms_serializer.metadata_factory')
->getMetadataForClass(get_class($sourceEntity))
->propertyMetadata;
foreach ($propertyMetadata as $realPropertyName => $data) {
$serializedPropertyName = $data->serializedName ?: $this->fromCamelCase($realPropertyName);
if (array_key_exists($serializedPropertyName, $params)) {
$newValue = $propertyAccessor->getValue($sourceEntity, $realPropertyName);
$propertyAccessor->setValue($targetEntity, $realPropertyName, $newValue);
}
}
}
/**
* #param string $input
* #return string
*/
protected function fromCamelCase($input)
{
preg_match_all('!([A-Z][A-Z0-9]*(?=$|[A-Z][a-z0-9])|[A-Za-z][a-z0-9]+)!', $input, $matches);
$ret = $matches[0];
foreach ($ret as &$match) {
$match = $match == strtoupper($match) ? strtolower($match) : lcfirst($match);
}
return implode('_', $ret);
}
The best way is using JMSSerializerBundle
The problem is JMSSerializer initializes with the default ObjectConstructor for deserialization (setting the fields that are not in the request as null, and making that merge method will also persist null properties to database). So you need to switch this one with the DoctrineObjectConstructor.
services:
jms_serializer.object_constructor:
alias: jms_serializer.doctrine_object_constructor
public: false
Then just deserialize and persist the entity, and it will be filled with the missing fields. When you save to database only the attributes that have changed will be updated on the database:
$foo = $this->get('jms_serializer')->deserialize(
$request->getContent(),
'AppBundle\Entity\Foo',
'json');
$em = $this->getDoctrine()->getManager();
$em->persist($foo);
$em->flush();
Credits to: Symfony2 Doctrine2 De-Serialize and Merge Entity issue
I'm having the same issue as you described, I just do the entity merging manually:
public function patchMembersAction($memberId, Member $memberPatch)
{
return $this->members->updateMember($memberId, $memberPatch);
}
This calls method that does the validation, and then manually calls all the required setter methods. Anyway, I'm wondering about writing my own param converter for such cases.
Another resource which helped me a lot is http://welcometothebundle.com/symfony2-rest-api-the-best-2013-way/. A step by step tutorial which filled in the blanks I had after the resource in the previous comment. Good luck!
I am trying to create something with extbase, but the error-message I get is not very helpful. I took the blog_example extension as a guide. A (maybe) important difference is: I don't have a database table because I want to write a custom domain repository that connects to an external servive through REST.
The actual error message (displayed above the plugin, not as an exception message):
An error occurred while trying to call Tx_MyExt_Controller_SubscriptionController->createAction()
Classes/Controller/SubscriptionController:
Stripped down to the important parts.
class Tx_MyExt_Controller_SubscriptionController extends Tx_Extbase_MVC_Controller_ActionController
{
/**
* #var Tx_MyExt_Domain_Repository_SubscriberRepository
*/
protected $subscriberRepository;
/**
* #return void
*/
public function initializeAction()
{
$this->subscriberRepository = t3lib_div::makeInstance('Tx_MyExt_Domain_Repository_SubscriberRepository');
}
/**
* #param Tx_MyExt_Domain_Model_Subscriber $subscriber
* #dontvalidate $subscriber
* #return string The rendered view
*/
public function newAction(Tx_MyExt_Domain_Model_Subscriber $subscriber = null)
{
$this->view->assign('subscriber', $subscriber);
}
/**
* #param Tx_MyExt_Domain_Model_Subscriber $subscriber
* #return string The rendered view
*/
public function createAction(Tx_MyExt_Domain_Model_Subscriber $subscriber)
{ }
}
Classes/Domain/Model/Subscriber
class Tx_MyExt_Domain_Model_Subscriber extends Tx_Extbase_DomainObject_AbstractEntity
{
/**
* #var string
* #dontvalidate
*/
protected $email = '';
/**
* #param string $email
* #return void
*/
public function setEmail($email)
{
$this->email = $email;
}
/**
* #return string
*/
public function getEmail()
{
return $this->email;
}
}
Resources/Private/Templates/Subscription/new
<f:form action="create" controller="Subscription" objectName="Subscriber" object="{subscriber}" method="post">
<f:form.textfield property="email"></f:form.textfield>
<f:form.submit value="submit"></f:form.submit>
</f:form>
Facts
Adding $subscriber = null removes the message. But $subscriber is null then
A var_dump($this->request->getArguments()); displays the form's fields
There is an index action, and it is also the first action defined in ext_localconf.php
The hints and solutions I found aren't working for me, so I hope someone can guide me into the right direction.
I've got the same bug.
If you pass an Model as argument to an method, it will also validate the model fields.
I've had this annotation on my model property:
/**
*
* #var \string
* #validate NotEmpty
*/
It validates the "#validate" annotation.
The field in the database was empty so i got the error message
An error occurred while trying to call ...
It would be good if there was a better error message.
You need to customize the validation annotation or verify that the property is not empty in the database
Hope it helps somebody
In addtion: check any Validations in your Model and your TCA. If a field is marked as #validate NotEmpty in your Model and is not marked appropriately in the TCA, a record can be saved ignoring the #validate settings in the Model. This can happen if you change the Model and/or TCA after creating records.
An example:
Field 'textfield' is set to not validate, both in the TCA and the Model. You create a new record and save it without filling in the field 'textfield' (you can, it is not set to validate). You then change the Model setting 'textfield' to #validate NotEmpty and then try to show the record on the FE, you will get the error.
The solution for that example:
Simply remove the validation in your Model OR check validations in the TCA and Model so that they work together.
--
A German blog post covers this solution: http://www.constantinmedia.com/2014/04/typo3-extbase-an-error-occurred-while-trying-to-call-anyaction/
just override the template method getErrorFlashMessage in yout controller to provide a custom error message...
/**
* A template method for displaying custom error flash messages, or to
* display no flash message at all on errors. Override this to customize
* the flash message in your action controller.
*
* #return string|boolean The flash message or FALSE if no flash message should be set
* #api
*/
protected function getErrorFlashMessage() {
return 'An error occurred while trying to call ' . get_class($this) . '->' . $this->actionMethodName . '()';
}
classic case of "start over from scratch and it works, and if you compare it you have the same code, though".
I updated the code in the question, maybe it helps someone.