Forums › Forums › OroPlatform › OroPlatform – How do I? Questions › Is it possible to inherit frontend validation?
This topic contains 8 replies, has 3 voices, and was last updated by Stepan Yudin 8 years, 5 months ago.
Starting from March 1, 2020 the forum has been switched to the read-only mode. Please head to StackOverflow for support.
- CreatorTopic
- October 8, 2015 at 11:07 pm #36093
For example i have a 3 entities:
– AbstractCar
– Car
– BrandCar extends AbstractCar, AbstractCar has validation.yml like this:
CarsBundle\Entity\AbstractCar:
properties:
name:
- NotBlank: ~Brand entity has field ‘cars’ that contains OneToMany relations with Car entities.
Brand form type contains ‘cars’ field rendered as ‘oro_collection’ with ‘type’ => New AbstractCarType().
I want the AbstractCar frontend constraints be applied to AbstractCarType form types that are rendered in BrandType form. Is it possible?Is it possible?
Docker environment for Symfony2/OroCRM/OroBAP development on Windows platform
https://github.com/stepansib/sf-oro-docker - CreatorTopic
- AuthorReplies
- October 15, 2015 at 9:54 am #36094
Hi, @stepan-sib.
Does the validation work on the backend side in the described case?October 20, 2015 at 3:04 am #36095Hi, Yurii
If i am not wrong – the validator checks for constraint both front-end and back-end validators.
In my case there is a simple NotBlank constraint, that of course fires at the front-end by JS.In fact i need any solution to validate every embedded form in collection with constraints defined in AbstractCar’s validation.yml
Docker environment for Symfony2/OroCRM/OroBAP development on Windows platform
https://github.com/stepansib/sf-oro-dockerOctober 29, 2015 at 6:18 am #36096Hi, @stepan-sib.
Please look how it was done for Address collection.
We have AddressCollectionType that extends oro_collection https://github.com/laboro/platform/blob/master/src/Oro/Bundle/AddressBundle/Form/Type/EmailCollectionType.php.
This collection form type have own renderer https://github.com/laboro/platform/blob/master/src/Oro/Bundle/AddressBundle/Resources/views/Include/fields.html.twig#L138.Address entity extends from the AbstractAddress (just like in your case).
And it have validator for AbstractAddress https://github.com/laboro/platform/blob/master/src/Oro/Bundle/AddressBundle/Resources/config/validation.yml#L20This address collection form type we use at create contact page. Here, if you will add sone ibfo to the first address will turn on frontend validators for this entity. You can check this case here http://demo.orocrm.com/desktop/contact/create.
October 30, 2015 at 12:20 am #36097Yes, you’re totally correct!
I tried to do like your say about couple of week ago and got strange cache issue. Now everything works like a charm. Thank you!And one more question: can i disable client side validation (via jQuery) for specified field/constraint?
I tried this:
form_widget(form.countryPrefixCode,{'attr':{'data-validation':''}})
but in fact it disabled both client side and backend validation. But i need only disable frontend validationThe full widget code:
123456789101112131415161718192021{% block oq_phone_widget %}<div>{{ form_widget(form.type,{'attr':{'style':'width: 100px;'}}) }}{{ form_widget(form.countryPrefixCode,{'attr':{'data-validation':'','placeholder':form.countryPrefixCode.vars.label|trans,'style':'width: 30px; margin-left: 6px;'}}) }}{{ form_widget(form.prefixCode,{'attr':{'placeholder':form.prefixCode.vars.label|trans,'style':'width: 50px; margin-left: 6px;'}}) }}{{ form_widget(form.phone,{'attr':{'placeholder':form.phone.vars.label|trans,'style':'width: 90px; margin-left: 6px;'}}) }}{{ form_widget(form.internalCode,{'attr':{'placeholder':form.internalCode.vars.label|trans,'style':'width: 40px; margin-left: 6px;'}}) }}</div><div class="clearfix"></div><div style="border: 1px solid red;">{{ form_errors(form.type) }}{{ form_errors(form.countryPrefixCode) }}{{ form_errors(form.prefixCode) }}{{ form_errors(form.phone) }}{{ form_errors(form.internalCode) }}{# but errors are rendered right after corresponding field instead of inside this block #}</div>{% endblock %}Docker environment for Symfony2/OroCRM/OroBAP development on Windows platform
https://github.com/stepansib/sf-oro-dockerOctober 30, 2015 at 12:24 am #36098I need it in cause of jQuery validator renders error right after the field it casted.
But i want to render error in specified block in the field widget templateDocker environment for Symfony2/OroCRM/OroBAP development on Windows platform
https://github.com/stepansib/sf-oro-dockerOctober 30, 2015 at 5:10 am #36099And i found that ClientValidationExtension is looking for option
'client_validation' => false,
but it doesnt helped me, client validation fires anywayDocker environment for Symfony2/OroCRM/OroBAP development on Windows platform
https://github.com/stepansib/sf-oro-dockerOctober 30, 2015 at 6:52 am #36100Hi, @stepan-sib.
I tried this:
form_widget(form.countryPrefixCode,{'attr':{'data-validation':''}})
but in fact it disabled both client side and backend validation. But i need only disable frontend validationIt looks like your backend validation does not work for this field. Because backend validation can’t be turned off by removing HTML attributes. This attribute is utilized only by frontend validation.
So, if you want to remove all validation rule for the field with no condition — you can use this approach. But there’s better approach. Same as data-validation-optional-group attribute, there’s other useful attribute — data-validation-ignore. If any field/group of fields is wrapped in element with data-validation-ignore attribute, frontend validator ignores validation rules for this field/group of fields. You can add/remove this attribute in runtime to change validation behavior.
November 1, 2015 at 6:54 am #36101Hryhorii, such a great tip, you should add it to bundle docs!
I tried and this works perfect (same as my approach, but my one looks like a hack), thank you a lot!And i should agree with you – my backend validation didn’t worked correctly because of i forgot to add Valid constraint to an oro_collection field type. I always forget about something important, my fault)
Docker environment for Symfony2/OroCRM/OroBAP development on Windows platform
https://github.com/stepansib/sf-oro-docker - AuthorReplies
The forum ‘OroPlatform – How do I? Questions’ is closed to new topics and replies.