-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
[Form] Explain how to trigger errors from DataMapper #13513
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Symfony allows the user to send a form error from the DataMapper in case the data does not fit the object. This powerful feature was not documented.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool, didn't know this feature!
Can you maybe add the foreach/if statements you introduced to the original code example (around line 130)? Then we can add a comment like // TransformationFailedException will be transformed into a FormError
. That would keep this short and simple, while not loosing important information.
ab0dd30
to
c4c5280
Compare
@wouterj this is updated according to your comment. |
c4c5280
to
250f237
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great, thanks a lot for the quick response!
Hello @Nek-, thank you for proposing this change. I'm really sorry, I comment after you put some work into it, and @wouterj approved, but I'm 👎 for now on merging this PR. I know we can do this, but this is hack and it's against separation of responsibilities. Also, the So I'm not in favor of documenting what is IMHO an invalid usage of the API, which would not be supported by our BC policy. Gentle ping @xabbuh, WDTY about this? |
I think I agree with Jules here. |
I'm closing here then, sorry again, thank you for understanding. |
It has been announced as new feature here: https://symfony.com/blog/new-in-symfony-4-3-more-form-improvements#custom-errors-in-data-mappers Please tell me what is the alternative to map an object without throwing a 500 error on wrong input data, I'll be more than happy to write doc about it. |
The feature is legit about customizing the message without using Thank you for reporting that there has been a mistake in the news... that should show an usage of the exception in a transformer instead of a mapper. Ping @javiereguiluz any chance we can correct it 🙏?
@Nek- thank you for proposing but it seems that the feature has been properly document in #11756 already. If you really need to check the validate state from a mapper (e.g you need to catch an invalid argument exception when calling
|
@HeahDude I suggest adding my example but with the usage of the |
This is a lesser evil for the docs I think. What I suggest is adding a note or a caution at the end about validation, saying that a mapper must deal with an invalid state when submitted, either using |
How can validation work if there is no hydrated data? 🤔 (I understand this flow is better, but I do not understand how it could work, except for |
With the |
@HeahDude yes but what if your class does not allow an invalid state? |
I know it's hard to get the big picture, trust me ;). I will show you an example. Consider a model class: // ...
class Apply
{
/** @Assert\NotNull */
public ?string firstName;
// ... more fields
} Then the following form: // ...
class ApplyFormType extends AbstractType
{
public function configureOptions(OptionsResolver $resolver)
{
// This class is used as underlying data
$resolver->setDefault('data_class', Apply::class);
}
public function buildForm(FormBuilderInterface $builder, array $options = [])
{
$builder
->add('firstName', TextType::class)
// .. add every field related to the object graph
// they will be validated by cascade when validating once $rootForm->getData()
// reading annotated constraints
// add a required field for this form to be valid but no data needed in model
->add('condition', CheckboxType::class, [
'label' => 'Accept something',
'required' => false,
// we don't want to read and write in apply underlying object
// this field can still used its own transformers and listeners that may invalid it
'mapped' => false,
// add a specific constraint in the form graph that will be validated in a second time
// on $rootForm->get('condition')->getData()
'constraints' => new Assert\IsTrue(['message' => 'Condition should be checked']),
])
;
}
} I hope it helps you understanding. The |
As I said before:
and
|
@HeahDude I tried to do what you said. [edit] I made a little error by not catching all error, this is my bad we discovered it while discussing. The following example works and return proper errors: |
Symfony allows the user to send a form error from the DataMapper in case the data does not fit the object. This powerful feature was not documented.