cakephp模型验证错误消息未显示在hasOne关联中

时间:2018-07-01 02:45:06

标签: php model cakephp-3.0

我想用一种形式的model进行association验证。我有两个表users(父表)和user_details(子表)。现在模型验证仅适用于users表。我也希望它适用于userDetails表。它们之间的关系是hasOne

验证仅适用于用户表,因为我仅为用户表创建了newEntity。

usersController.php(控制器代码)

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

UsersTable.php(父模型)

 public function validationDefault(Validator $validator) {
        $validator
                ->integer('id')
                ->allowEmpty('id', 'create');

        $validator
                ->email('email')
                ->requirePresence('email', 'create')
                ->notEmpty('email');


        $validator
                ->scalar('mobile')
                ->maxLength('mobile', 10,'Maximum length should be 10 digits')
                ->minLength('mobile', 10,'Minimum length should be 10 digits')
                ->requirePresence('mobile', 'create')
                ->notEmpty('mobile');

        $validator
                ->scalar('password')
                ->maxLength('password', 20,'Minimum length should be 20 digits')
                ->minLength('password', 4,'Minimum length should be 4 digits')
                ->requirePresence('password', 'create')
                ->notEmpty('password');


        return $validator;
    }

UserDetailsTable.php(子模型)

 public function validationDefault(Validator $validator)
    {
        $validator
            ->integer('id')
            ->allowEmpty('id', 'create');

        $validator
            ->scalar('name')
            ->maxLength('name', 50)
            ->requirePresence('name', 'create')
            ->notEmpty('name');

        $validator
            ->scalar('pin')
            ->maxLength('pin', 6)
            ->requirePresence('pin', 'create')
            ->notEmpty('pin');

        $validator
            ->scalar('address')
            ->maxLength('address', 4294967295)
            ->requirePresence('address', 'create')
            ->notEmpty('address');

        return $validator;
    }

add.ctp

<?= $this->Form->create($user, ['url' => ['controller' => 'Users', 'action' => 'add'], 'class' => 'form-horizontal', 'id' => 'add-user']); ?>

        <div class="box-body">
            <div class="form-group">
                <label for="name" class="col-sm-2 control-label">Name <label class="text-danger">*</label></label>
                <div class="col-sm-3">
                    <?= $this->Form->control('user_detail.name', ['class' => 'form-control', 'id' => 'name', 'placeholder' => 'Name', 'type' => 'text', 'label' => false]); ?>
                </div>
                <label for="email" class="col-sm-2 control-label">Email <label class="text-danger">*</label></label>
                <div class="col-sm-3">
                    <?= $this->Form->control('email', ['class' => 'form-control', 'id' => 'email', 'placeholder' => 'Email', 'type' => 'email', 'label' => false]); ?>
                </div>
            </div>
            <div class="form-group">
                <label for="mobile" class="col-sm-2 control-label">Mobile <label class="text-danger">*</label></label>
                <div class="col-sm-3">
                    <?= $this->Form->control('mobile', ['class' => 'form-control', 'id' => 'mobile', 'placeholder' => 'Mobile', 'type' => 'text', 'label' => false]); ?>
                </div>
                <label for="password" class="col-sm-2 control-label ">Password <label class="text-danger">*</label></label>
                <div class="col-sm-3">
                    <?= $this->Form->control('password', ['class' => 'form-control', 'id' => 'password', 'placeholder' => 'Password', 'type' => 'text', 'label' => false]); ?>
                </div>
                <div class="col-sm-2">
                    <button type="button" class="btn btn-default"><i class="fa fa-refresh"></i> Generate</button>
                </div>
            </div>
            <div class="form-group">
                <label for="state" class="col-sm-2 control-label">State</label>
                <div class="col-sm-3">
                    <?= $this->Form->select('user_detail.state', ['Odisha', 'Hyderbad'], ['class' => 'form-control', 'id' => 'state', 'label' => false]); ?>

                </div>
                <label for="city" class="col-sm-2 control-label">City</label>
                <div class="col-sm-3">
                    <?= $this->Form->select('user_detail.city', ['Bhubaneswar', 'Cuttack'], ['class' => 'form-control', 'id' => 'city', 'label' => false]); ?>
                </div>
            </div>
            <div class="form-group">
                <label for="inputEmail3" class="col-sm-2 control-label">Address</label>
                <div class="col-sm-3">
                    <?= $this->Form->control('user_detail.address', ['class' => 'form-control', 'id' => 'address', 'type' => 'textarea', 'rows' => 2, 'label' => false]); ?>
                </div>
                <label for="pin" class="col-sm-2 control-label">Pin</label>
                <div class="col-sm-3">
                    <?= $this->Form->control('user_detail.pin', ['class' => 'form-control', 'id' => 'pin', 'placeholder' => 'Pin', 'type' => 'text', 'label' => false]); ?>
                </div>
            </div>
            <div class="form-group">
                <div class="col-sm-offset-2 col-sm-10">
                    <button type="button" class="btn btn-default">Cancel</button>
                    <?= $this->Form->control('Save', ['class' => 'btn btn-primary', 'id' => 'submit', 'type' => 'submit', 'label' => false, 'div' => false, 'templates' => ['submitContainer' => '{{content}}']]); ?>

                </div>
            </div>
        </div>

        <?= $this->Form->end(); ?>

这是参考图片

enter image description here

此图中的所有字段均为必填项 所有字段均来自用户表...仅地址字段来自user_details表。现在您可以看到错误消息显示姓氏字段,但错误消息不显示地址字段。验证适用于地址字段,但不知道为什么出现错误消息没有显示。

用户表中的名字和user_details表中的地址

预先感谢

2 个答案:

答案 0 :(得分:2)

  

验证仅适用于用户表,因为我仅为用户表创建了newEntity。

这与它无关。问题是您没有正确遵循命名约定。

文件名必须与类名匹配,因此它们是UserDetailsTable.php,而不是UserDetails.phpUsersTable.php,而不是usersTable.php,等等。

并且hasOne关联的正确属性名称是关联名称的单数,强调的变体,因此对于UserDetails将是user_detail,因此关联名称是表单控件应为user_detail.name

另请参见

答案 1 :(得分:0)

您正在尝试使用错误的数据($this->request->getData())修补详细信息实体。但是,实际上,无需在add函数中创建,修补和保存单独的实体,因为第一级关联将为您透明地处理。这应该可以正常工作:

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

或者至少,一旦您更正了来自详细信息实体的字段名称,使其包括“ user_detail。”,就如@ndm所说;您的上方HTML仍显示$this->Form->control('address',...)而不是$this->Form->control('user_detail.address',...),尽管它还包括城市和州的字段,这些字段不在您的屏幕快照中,因此尚不清楚您的当前版本是什么样。< / p>