Quantcast

Form: hasMany fields table validation not working

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Form: hasMany fields table validation not working

Paulo Terra
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:

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'
        ]);




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 [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Form: hasMany fields table validation not working

Dakota
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Form: hasMany fields table validation not working

Paulo Terra
Great! It Works! Thank you Dakota!

2016-02-29 11:15 GMT-03:00 Dakota <[hidden email]>:
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 a topic in the Google Groups "CakePHP" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/cake-php/M8MwE8p8SZc/unsubscribe.
To unsubscribe from this group and all its topics, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.

--
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Form: hasMany fields table validation not working

Dakota
Not a problem, glad that worked for you.

For future reference, you can usually get nearly instant help on the IRC channel (http://webchat.freenode.net/?channels=cakephp&uio=MT1mYWxzZSY5PXRydWUmMTE9MjQ2b8), depending on who is online of course. Stackoverflow is also usually more active than the group.

On Monday, 29 February 2016 16:31:11 UTC+2, Paulo Terra wrote:
Great! It Works! Thank you Dakota!

2016-02-29 11:15 GMT-03:00 Dakota <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="2WSTCi8CDQAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">walth...@...>:
Hi Paulo,

Your form field for address zipcode field is in the wrong format. <a href="http://book.cakephp.org/3.0/en/views/helpers/form.html#creating-inputs-for-associated-data" target="_blank" rel="nofollow" onmousedown="this.href=&#39;http://www.google.com/url?q\75http%3A%2F%2Fbook.cakephp.org%2F3.0%2Fen%2Fviews%2Fhelpers%2Fform.html%23creating-inputs-for-associated-data\46sa\75D\46sntz\0751\46usg\75AFQjCNGraozaIBwQAZZ70TyvWXzgvIgXkQ&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\75http%3A%2F%2Fbook.cakephp.org%2F3.0%2Fen%2Fviews%2Fhelpers%2Fform.html%23creating-inputs-for-associated-data\46sa\75D\46sntz\0751\46usg\75AFQjCNGraozaIBwQAZZ70TyvWXzgvIgXkQ&#39;;return true;">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.
<a href="http://cakephp.org/newsletter/signup" target="_blank" rel="nofollow" onmousedown="this.href=&#39;http://www.google.com/url?q\75http%3A%2F%2Fcakephp.org%2Fnewsletter%2Fsignup\46sa\75D\46sntz\0751\46usg\75AFQjCNGErMluKHvMycgXNStE2DC-BG17zg&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\75http%3A%2F%2Fcakephp.org%2Fnewsletter%2Fsignup\46sa\75D\46sntz\0751\46usg\75AFQjCNGErMluKHvMycgXNStE2DC-BG17zg&#39;;return true;">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 <a href="https://www.facebook.com/CakePHP" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://www.google.com/url?q\75https%3A%2F%2Fwww.facebook.com%2FCakePHP\46sa\75D\46sntz\0751\46usg\75AFQjCNG_OVGPrsW2S4xCvT0yb2leRWBsQg&#39;;return true;" onclick="this.href=&#39;https://www.google.com/url?q\75https%3A%2F%2Fwww.facebook.com%2FCakePHP\46sa\75D\46sntz\0751\46usg\75AFQjCNG_OVGPrsW2S4xCvT0yb2leRWBsQg&#39;;return true;">https://www.facebook.com/CakePHP
Follow us on Twitter <a href="http://twitter.com/CakePHP" target="_blank" rel="nofollow" onmousedown="this.href=&#39;http://www.google.com/url?q\75http%3A%2F%2Ftwitter.com%2FCakePHP\46sa\75D\46sntz\0751\46usg\75AFQjCNGm99t3RztzLJAbpv22cUAOb0aFHw&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\75http%3A%2F%2Ftwitter.com%2FCakePHP\46sa\75D\46sntz\0751\46usg\75AFQjCNGm99t3RztzLJAbpv22cUAOb0aFHw&#39;;return true;">http://twitter.com/CakePHP
---
You received this message because you are subscribed to a topic in the Google Groups "CakePHP" group.
To unsubscribe from this topic, visit <a href="https://groups.google.com/d/topic/cake-php/M8MwE8p8SZc/unsubscribe" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/topic/cake-php/M8MwE8p8SZc/unsubscribe&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/topic/cake-php/M8MwE8p8SZc/unsubscribe&#39;;return true;">https://groups.google.com/d/topic/cake-php/M8MwE8p8SZc/unsubscribe.
To unsubscribe from this group and all its topics, send an email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="2WSTCi8CDQAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">cake-php+u...@googlegroups.com.
For more options, visit <a href="https://groups.google.com/d/optout" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;">https://groups.google.com/d/optout.

--
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Form: hasMany fields table validation not working

Paulo Terra
Ok, thank you very much! I am sorry that I didn't realize this singular and plural detail.

Thank you very much!

Paulo

2016-02-29 11:36 GMT-03:00 Dakota <[hidden email]>:
Not a problem, glad that worked for you.

For future reference, you can usually get nearly instant help on the IRC channel (http://webchat.freenode.net/?channels=cakephp&uio=MT1mYWxzZSY5PXRydWUmMTE9MjQ2b8), depending on who is online of course. Stackoverflow is also usually more active than the group.

On Monday, 29 February 2016 16:31:11 UTC+2, Paulo Terra wrote:
Great! It Works! Thank you Dakota!

2016-02-29 11:15 GMT-03:00 Dakota <[hidden email]>:
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 a topic in the Google Groups "CakePHP" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/cake-php/M8MwE8p8SZc/unsubscribe.
To unsubscribe from this group and all its topics, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.

--
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 a topic in the Google Groups "CakePHP" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/cake-php/M8MwE8p8SZc/unsubscribe.
To unsubscribe from this group and all its topics, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.

--
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.
Loading...