Monday, February 29, 2016

Re: Form: hasMany fields table validation not working

Hi Paulo,

Your form field for address zipcode field is in the wrong format. http://book.cakephp.org/3.0/en/views/helpers/form.html#creating-inputs-for-associated-data shows the correct format for each type of relation. Basically, instead of doing 
$this->Form->input('address.zipcode');

You need to do instead do:
$this->Form->input('addresses.0.zipcode');

For hasMany relations, the key is always plural and you need to specify a numeric index.


On Sunday, 28 February 2016 22:07:32 UTC+2, Paulo Terra wrote:
Hi,

I have 3 tables: User, Buyer and Address:

User hasMany Address
User hasOne Buyer
Address belongsTo User
Buyer belongsTo User

In the User form (Users/add.ctp):

echo $this->Form->input('name',['label' => __('Nome')]);
echo $this->Form->input('buyer.cpf',['label' => __('CPF')]);
echo $this->Form->input('address.zipcode');



UsersTable.php:

        $this->hasMany('Addresses', [
            'foreignKey' => 'user_id'
        ]);
       
        $this->hasOne('Buyers', [
            'foreignKey' => 'user_id'
        ]);



BuyersTable.php:

        $this->table('buyers');
        $this->displayField('id');
        $this->primaryKey('id');
        $this->belongsTo('Users', [
            'foreignKey' => 'user_id',
            'joinType' => 'INNER'
        ]);


AddressTable.php:

        $this->table('addresses');
        $this->displayField('id');
        $this->primaryKey('id');
        $this->belongsTo('Users', [
            'foreignKey' => 'user_id',
            'joinType' => 'INNER'
        ]);



The field "cpf" from Buyer is recognized by cake as it´s Model is shown in the include path:

  • Model(array)
    • 0APP/Model/Table/UsersTable.php
    • 1APP/Model/Entity/User.php
    • 2APP/Model/Table/SurveysTable.php
    • 3APP/Model/Table/BuyersTable.php
    • 4APP/Model/Entity/Buyer.php
    • 5APP/Model/Entity/Survey.php
And "cpf" is also a "not null" field, whitch is properly verifyed in cake when I save.

The problem is the "zipcode" field in "Address". It´s is also a required field but it is not validated from cake. And, as can be seen, it is not loaded in Model list.

But when I change the relation Address relation from "hasMany" to "hasOne" it works (it also validade required field when I save).


UsersTable.php:

        $this->hasOne('Addresses', [
            'foreignKey' => 'user_id'
        ]);
       
        $this->hasOne('Buyers', [
            'foreignKey' => 'user_id'
        ]);


  • Model(array)
    • 0APP/Model/Table/UsersTable.php
    • 1APP/Model/Entity/User.php
    • 2APP/Model/Table/SurveysTable.php
    • 3APP/Model/Table/BuyersTable.php
    • 4APP/Model/Entity/Buyer.php
    • 5APP/Model/Table/AddressesTable.php
    • 6APP/Model/Entity/Address.php
    • 7APP/Model/Entity/Survey.php


Does anyone has a clue about what is happening?

--
Sign up for our Newsletter for updates.
http://cakephp.org/newsletter/signup
 
We will soon be closing this Google Group. But don't worry, we have something better coming. Stay tuned for an updated from the CakePHP Team soon.
 
Like Us on FaceBook https://www.facebook.com/CakePHP
Follow us on Twitter http://twitter.com/CakePHP
---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments: