cakephp Model validation error message not displaying in hasOne association

后端 未结 2 571
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-21 20:57

i want to do model validation with association in a single form. i have two tables users(parent table) and user_details(child

2条回答
  •  鱼传尺愫
    2021-01-21 21:28

    You're trying to patch your details entity with the wrong data ($this->request->getData()). But really, there is no need to create, patch and save a separate entity in your add function, because the first level of associations will be handled transparently for you. This should work just fine:

    public function add() {
        $user = $this->Users->newEntity();
        if ($this->request->is('post')) {
            $user = $this->Users->patchEntity($user, $this->request->getData());
            if ($this->Users->save($user)) {
                $this->Flash->success(__('The user has been saved.'));
                return $this->redirect($this->referer());
            } else {
                $this->Flash->error(__('The user could not be saved. Please, try again.'));
            }
        }
        $this->set(compact('user'));
    }
    

    Or, at least, it should once you correct the field names from the details entity to include "user_detail.", as @ndm said; your HTML above is still showing $this->Form->control('address',...) instead of $this->Form->control('user_detail.address',...), though it also includes fields for city and state, which aren't in your screen shot, so it's unclear what your current version might look like.

提交回复
热议问题