[TYPO3-mvc] different validations for same model used twice in a single form
typo3 at litedesigns.nl
typo3 at litedesigns.nl
Sat Mar 31 10:05:15 CEST 2012
Hi List,
I've taken a closer look at the custom validators, but i have found
something odd for which i need to provide a little more info.
I have a 'new' and 'create' action in my controller to create my new
Company as described in my previous mail. If i set each Model separately
in my annotations and params of my 'new' and 'create' function, extbase
will validate all these models. If i use 'dotnotation', the 'subObjects'
are not validated.
Some code to explain what i mean:
/**
* action newAccount
*
* @param $newCompany - The Company to create
* @param $newAddress - The Company->Address to create
* @param $newBillingAddress - The Company->BillingAdrress to create
* @param $newBillingAddressAddress - The Company->BillingAddress->Address
to create. Explicitly named here!
* @dontvalidate $newCompany
* @dontvalidate $newAddress
* @dontvalidate $newBillingAddress
* @dontvalidate $newBillingAddressAddress
* @return void
*/
public function newAccountAction(Tx_MyExt_Domain_Model_Company $newCompany
= NULL,
Tx_MyExt_Domain_Model_Address $newAddress = NULL,
Tx_MyExt_Domain_Model_BillingAddress $newBillingAddress = NULL,
Tx_MyExt_Domain_Model_Address $newBillingAddressAddress = NULL)
{
$this->view->assign('newCompany', $newCompany);
$this->view->assign('newAddress', $newAddress);
$this->view->assign('newBillingAddress', $newBillingAddress);
$this->view->assign('newBillingAddressAddress', $newBillingAddressAddress);
}
/**
* action createAccount
*
* @param $newCompany
* @param $newAddress
* @param $newBillingAddress
* @param $newBillingAddressAddress
* @return void
*/
public function createAccountAction(Tx_MyExt_Domain_Model_Company
$newCompany,
Tx_MyExt_Domain_Model_Address $newAddress,
Tx_MyExt_Domain_Model_BillingAddress $newBillingAddress,
Tx_MyExt_Domain_Model_Address $newBillingAddressAddress)
{
$newBillingAddress->setAddress($newBillingAddressAddress);
$newCompany->setAddress($newAddress);
$newCompany->setBillingAddress($newBillingAddress);
... (repository and redirect stuff here)
}
Pay special attention to the $newBillingAddressAddress param. In my form
view i access the fields like (by the two excplicitly named models):
{newBillingAddress.fieldC}
{newBillingAddress.fieldD}
{newBillingAddressAddress.street}
{newBillingAddressAddress.housenumber}
etc
This way Extbase expects the Tx_MyExt_Domain_Model_Address
$newBillingAddressAddress fields all to be notEmpty because of the
annotations in the Address model.
But if i remove the $newBillingAddressAddress param from the 'new' and
'create' action and access the fields in my form view like (dotnotation,
only BillingAddress is named explicitly):
{newBillingAddress.fieldC}
{newBillingAddress.fieldD}
{newBillingAddress.address.street} <= the address is not named in the
controller actions, but accessed with dotnotation
{newBillingAddress.address.housenumber}
etc
Extbase does not seem to validate the BillingAddress->Address fields. Even
though they are left empty in the form and the Address model annotations
indicate the fields are required, Extbase creates the
BillingAddress->Address in my address table and sets the BillingAddress
reference to this newly created address. But i thought is should give me
an error that the BillingAddress->Address fields are empty (like in the
first situation).
I hope my explanation made some sense, as it is quite a complex form...
Anyway, is this behavior intended by Extbase? I.e., if i explicitly name
the Tx_MyExt_Domain_Model_Address $newBillingAddressAddress param in my
controller functions it will be automatically validated. If i do not
explicitly name the Tx_MyExt_Domain_Model_Address
$newBillingAddressAddress param but use dotnotation, it is not validated
automatically.
For my situation this behavior is what i need. By not explicitly naming
the BillingAddress->Address in my controller, the fields become optional
instead of required (notEmpty). But can i rely on this behavior, or is it
a bug which will be fixed someday, breaking my forms logic when i upgrade?
Kind regards and have a great weekend!
Tim
> Hi Jochen,
>
> Thanks for the suggestion, i will take a look at custom validators.
>
> To be more clear about my model:
>
> Company
> |- FieldA
> |- FieldB
> |- Address (Model)
> |- BillingAddress
> |- FieldC
> |- FieldD
> |- Address (Model)
>
> And address:
> Address
> |- Street
> |- Housenumber
> |- City
> |- Etc
>
> Since both Company and BillingAddres have an Address, i want to validate
> both Address instances in my single form differently.
>
> The Company->Address fields need to be required, the
> BillingAddress->Address fields must be optional.
>
> Another way to look at it would be like with an eCommerce website when a
> customer registers or buys something:
>
> Customer
> |- Address
> |- ShippingAddress
>
> Where the Customer->Address is where the customer lives and the
> ShippingAddress is where the products will be delivered.
>
> I will report back my findings with the custom validators, although i'm
> still not entirely sure how Company->Address fields can be set required
> and the BillingAddress->Address fields optional. But as said, i will first
> look into the custom validators :)
>
> Kind regards,
> Tim
>
>> Hi Tim,
>>
>>> How can i make the Company->Address fields required and the
>>> BillingAddress->Address fields optional? Both Company and
>>> BillingAddress
>>> obviously use the same Address Model (to avoid duplicate code) and i
>>> can
>>> only set one annotation per property.
>>
>> So, you have two distinct properties in your Company class: one
>> referencing the address and one referencing the billingAddress? A custom
>> BillingAddressValidator attached to the billingAddress might help.
>>
>> -Jochen
>> _______________________________________________
>> TYPO3-project-typo3v4mvc mailing list
>> TYPO3-project-typo3v4mvc at lists.typo3.org
>> http://lists.typo3.org/cgi-bin/mailman/listinfo/typo3-project-typo3v4mvc
>>
>
> _______________________________________________
> TYPO3-project-typo3v4mvc mailing list
> TYPO3-project-typo3v4mvc at lists.typo3.org
> http://lists.typo3.org/cgi-bin/mailman/listinfo/typo3-project-typo3v4mvc
>
More information about the TYPO3-project-typo3v4mvc
mailing list