Project

General

Profile

Actions

Feature #77450

closed

Easier Validation handling inside initilize*Actions

Added by Tobias Braumann over 8 years ago. Updated over 4 years ago.

Status:
Rejected
Priority:
Should have
Assignee:
-
Category:
Extbase
Target version:
-
Start date:
2016-08-09
Due date:
% Done:

0%

Estimated time:
PHP Version:
7.0
Tags:
Complexity:
Sprint Focus:

Description

Half day is gone and i couldn't find any good solution for my problem without writing my own validator or ended up with a second model.

First i want to describe my problem here. This are my models:

My order has to address properties, one for shipping (mandatory) and one for the billing (optional). The address model has some mandatory fields. There is a checkbox to show the billing address. Everthing works out of the box if
the user need both addresses. But in the case the shipping address and the billing address are the same, he only has to fill out the first address fields.

I know i could write my own validator, but than i have to check all my address properties by my self, and i thought about a solution to turn off the validtor for the property billlingAddress when the checkbox is not set. And i think the best
place for the would be the initilize*Actions

There should be an easy way to do that, similar to the propertyMappingConfiguration.

Pseudocode inside an initilize*Action:

if ($this->arguments->hasArgument('order') && $showBillingAddress === TRUE) {
    $orderValidator = $this->arguments->getArgument('order')->getValidator();
    $orderValidator->forProperty('billingAddress')->removeValidator() 
        // or $orderValidator->skipValidationForProperty('billingAddress')
}

By the way, this would give us a more dynamic way for validation for specific actions, without the need to write a own model validator for smaller problems like this. Or did I overlooked anything?


Files

order .png (47.4 KB) order .png Tobias Braumann, 2016-08-09 10:55
Actions #1

Updated by Tobias Braumann over 8 years ago

Tobias Braumann wrote:

Half day is gone and i couldn't find any good solution for my problem without writing my own validator or ended up with a second model.

First i want to describe my problem here. This are my models:

My order has to address properties, one for shipping (mandatory) and one for the billing (optional). The address model has some mandatory fields. There is a checkbox to show the billing address. Everthing works out of the box if
the user need both addresses. But in the case the shipping address and the billing address are the same, he only has to fill out the first address fields.

I know i could write my own validator, but than i have to check all my address properties by my self, and i thought about a solution to turn off the validtor for the property billlingAddress when the checkbox is not set. And i think the best
place for the would be the initilize*Actions

There should be an easy way to do that, similar to the propertyMappingConfiguration.

Pseudocode inside an initilize*Action:

[...]

By the way, this would give us a more dynamic way for validation for specific actions, without the need to write a own model validator for smaller problems like this. Or did I overlooked anything?

Actions #2

Updated by Claus Due about 8 years ago

I think this is good as it is. Your use case clearly calls for two different object-level validators which validate properties differently based on - just as an example - another property in the object (e.g. "is shipping address"). Once you have that designed correctly, you can apply that validator to your aggregate root's property so that your "Order" object declares two different validators for main and shipping address.

If you have an action that receives the address itself and this action is capable of receiving both these addresses then you may have an additional design problem. But, in order to accommodate this case, you can override the initializeStoreAddressAction (or whatever your action is called) and then make a decision for your validator based on a property in $this->request, then using $this->arguments['address']->setValidator(new MyCustomValidator());

Lastly: it is also possible to define every shared validation in the Address model, as annotations therein - and then create a custom validator for NON-shipping addresses that will validate the additional properties. Then, in your initializeAction, use the same method to switch the validator but instead of using "setValidator" do "addValidator", and only do anything if the address is NOT the shipping address.

On a personal note, I don't think there is any easier way to manipulate this validation and make it dynamic, than exactly creating a custom validator and using it in any of the ways described above. If you agree after reading this, please close the issue ;)

Actions #3

Updated by Riccardo De Contardi over 7 years ago

  • Target version changed from 8 LTS to 9.0
Actions #4

Updated by Tobias Braumann about 7 years ago

You didn't get the point. I know i could write my own validator, but I don't want do. I still think this would be a nice thing.

Actions #5

Updated by Susanne Moog almost 7 years ago

  • Target version deleted (9.0)
Actions #6

Updated by Stefan Froemken over 4 years ago

Hello Tobias,

I know that you want a super simple solution, but I don't think that we will implement that to extbase. We have a lot of Validators, you can build your own Validators and you can and should use a DomainValidator. That way you have access to all Properties in one Validator. A good start to check, if an address should be validated or not. No need to do nasty things in initialize*Action. So your controller is clean again.

Stefan

Actions #7

Updated by Alexander Schnitzler over 4 years ago

  • Status changed from New to Rejected
Actions

Also available in: Atom PDF