October: 'Required' validation doesn't work correctly on relation fields

Created on 24 Jan 2015  路  19Comments  路  Source: octobercms/october

Say _Post_ belongs to many _Categories_. If I put

public $belongsToMany = [
    'categories' => ['My\Plugin\Models\Category', 'table' => 'my_plugin_posts_categories', 'order' => 'name']
];

public $rules = [
    'categories' => 'required'
];

into my Post model and hit save on a record that doesn't already have categories - even if I have a category selected in the form it will error with a message about categories being required.

Some things to note:

  • If the record already has categories attached to it, so long as you leave at least one selected the form will save successfully
  • If you have no categories selected, the form will error as expected
  • If you have categories selected, the form will still error if your record in the DB doesn't already have categories attached to it.

I've whipped up a demo repo complete with instructions to replicate here.

Medium Completed Bug

All 19 comments

Can you retest using the latest build? There has been some work performed to the validation of related fields lately.

This is not fixed. The instructions on the git repo posted in my issue above still replicate the issue.

+1 / the same problem.

:+1: same here

+1

+1, it would be nice to have it fixed

+1, got the same issue.

+1

i've also got the same issue.

after some debugging, i found that in October\Rain\Database\Traits\Validation, line 138 is not getting the related model value, it returning empty array.

...
$data[$attribute] = $this->getRelationValue($attribute);

i don't know if this related to laravel itself or not.

for now i change the above snippet to,

...
$data[$attribute] = $this->getAttribute($attribute)->lists('id');

hope this will help you find out what the bug is.

+1

Any progress on this issue?

+1

+1

+1 this is kinda problematic especially on pivot data for extending a plugin.

I've never seen more ignorant people than here... I abandoned this "CMS". I suggest the same thing to you.

If you lack the ability to read the documentation or understand the concept of open source. You should probably abandon programming too.

For the rest of you, this can be overcome by simply using the beforeValidate method in your model, or the formBeforeSave method in the controller. All issues are important to us, we are working as hard as we can. Thank you for your patience.

Also consider submitting a bounty on issues that require escalation.

[removed nonconstructive comment]

The squeaky wheel gets the grease, I suppose.

This issue is now resolved.

Please don't be rude. We do this for love.

Was this page helpful?
0 / 5 - 0 ratings