OroPlatform Forums

Covering OroPlatform topics, including community updates and company announcements.

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, 4 months ago.

Starting from March 1, 2020 the forum has been switched to the read-only mode. Please head to StackOverflow for support.

  • Creator
    Topic
  • #36093

    Stepan Yudin
    Participant

    For example i have a 3 entities:
    – AbstractCar
    – Car
    – Brand

    Car 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

Viewing 8 replies - 1 through 8 (of 8 total)
  • Author
    Replies
  • #36094

    Yurii Muratov
    Participant

    Hi, @stepan-sib.
    Does the validation work on the backend side in the described case?

    #36095

    Stepan Yudin
    Participant

    Hi, 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-docker

    #36096

    Yurii Muratov
    Participant

    Hi, @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#L20

    This 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.

    #36097

    Stepan Yudin
    Participant

    Yes, 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 validation

    The full widget code:


    Docker environment for Symfony2/OroCRM/OroBAP development on Windows platform
    https://github.com/stepansib/sf-oro-docker

    #36098

    Stepan Yudin
    Participant

    I 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 template


    Docker environment for Symfony2/OroCRM/OroBAP development on Windows platform
    https://github.com/stepansib/sf-oro-docker

    #36099

    Stepan Yudin
    Participant

    And i found that ClientValidationExtension is looking for option
    'client_validation' => false,
    but it doesnt helped me, client validation fires anyway


    Docker environment for Symfony2/OroCRM/OroBAP development on Windows platform
    https://github.com/stepansib/sf-oro-docker

    #36100

    Hryhorii Hrebiniuk
    Participant

    Hi, @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 validation

    It 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.

    #36101

    Stepan Yudin
    Participant

    Hryhorii, 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

Viewing 8 replies - 1 through 8 (of 8 total)

The forum ‘OroPlatform – How do I? Questions’ is closed to new topics and replies.

Back to top