Rename the group_id
form field to the relation name in either $has_one
(so probably group
) or in the $has_many
(probably groups
) and add that name to the $fields
array when calling the from_array()
. So something like this:
// $_POST = array('group' => 3); // let's say this is what's coming in
$user->from_array($_POST, array('username', 'email', 'status', 'group', TRUE));
Notice the symmetry:
- the relation called group
- the input parameter called group
- the field called group
This is what's happening:
- You have a
required
on yourUser
model'sgroup
field, which is the relation's name. - You try to update and save a
User
instance and with an input field namedgroup_id
, however the relation is named group. - Inside the array extension's
from_array()
method the code checks every key is its ahas_one
or ahas_many
relation to gather the related objects for the final save. Since your data havegroup_id
but your relation is namedgroup
they don't match, the related group instance is not fetched. - At the end of the
from_array()
save is called with the related objects gathered while looping trough the fields, in your case with no objects. - The validation kicks in, and tries to
_count_related()
for your user's groups but it won't find the group and the "and take these with you too" second array is empty (this is where the objects gathered in step 3. would appear) - The
related_required
validation fails and stops the save process.