【问题标题】:cakephp Model validation error message not displaying in hasOne associationcakephp 模型验证错误消息未显示在 hasOne 关联中
【发布时间】:2018-12-09 17:28:35
【问题描述】:

我想用association 以单一形式进行model 验证。我有两个表users(父表)和user_details(子表)。现在模型验证仅适用于用户表。我也希望它用于 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;
    }

添加.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(); ?>

这是图片供参考

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

users 表中的名字和 user_details 表中的地址

提前致谢

【问题讨论】:

  • 不清楚用户和user_details之间有什么样的关系。我猜有一个。所以根据manual(ndm 在他的回答中提供的相同链接)输入名称应该是user_detail.address

标签: php model cakephp-3.0


【解决方案1】:

您正在尝试使用错误数据 ($this-&gt;request-&gt;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-&gt;Form-&gt;control('address',...) 而不是 $this-&gt;Form-&gt;control('user_detail.address',...),但它还包括城市和州的字段,这些字段不在您的屏幕截图中,因此不清楚您当前的版本可能是什么样子。

【讨论】:

  • 我已尝试使用 user_detail.address 和 user_detail.0.address,但插入的用户表记录中没有任何工作,但 user_details 表仍为空白
  • 如果你在patchEntity 调用之后debug($user);,它是什么样子的?
  • 也许还包括来自UsersTable 类的initialize 函数,因此我们可以看到关联。
  • 'email' => 'soubhhbh@yahoo.com', 'mobile' => '5136563514', 'password' => 'vhgnvhgfhfh', '[new]' => true, '[可访问]' => ['email' => true, 'mobile' => true, 'password' => true, 'user_status' => true, 'user_type' => true, 'created' => true, '更新' => true, 'orders' => true, 'user_details' => true ], '[dirty]' => [ 'email' => true, 'mobile' => true, 'password' => true, ' user_detail' => true ], '[original]' => [], '[virtual]' => [], '[errors]' => [], '[invalid]' => [], '[repository ]' => '用户'
  • 公共函数initialize() { parent::initialize(); $this->viewBuilder()->setLayout('admin'); $this->loadModel('Users'); $this->loadModel('UserDetails'); $this->loadComponent('自定义'); $this->loadComponent('Paginator'); }
【解决方案2】:

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

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

文件名必须匹配类名,所以它是UserDetailsTable.php,而不是UserDetails.phpUsersTable.php,而不是usersTable.php等。

hasOne 关联的正确属性名称是关联名称的单数、带下划线的变体,因此对于 UserDetails,这将是 user_detail,因此相关表单控件的名称应该是 @987654332 @。

另见

【讨论】:

  • 我已经按照上述所有链接。抱歉输入错误,但我遵循命名约定。我又更新了。
  • 如果我将其更改为 user_detail.name 则验证正在工作,但错误消息未显示在每个字段下方,但对于用户表,它显示完美。
  • @SoubhagyaKumar 那是您需要进一步调试的东西,我无法重现它,这是与原始问题不同的问题。如果您无法弄清楚,请使用一些可以重现问题的简短示例代码创建一个新问题。
  • 我已经在这个问题中添加了一个截图。我在那里简要解释了这种情况。请看看并告诉我是否有任何解决方案
猜你喜欢
  • 1970-01-01
  • 2021-06-13
  • 2011-04-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-15
  • 1970-01-01
相关资源
最近更新 更多