A mensagem de erro de validação do modelo cakephp não é exibida na associação hasOne
eu quero fazermodel
validação comassociation
em uma única forma. Eu tenho duas mesasusers
(tabela pai) euser_details
(validação do modelo filho.) agora está funcionando somente para a tabela de usuários. Quero-o também para a tabela userDetails. relação entre eles éhasOne
.
validação trabalhando apenas para a tabela de usuários, porque eu criei a tabela newEntity somente para usuários.
usersController.php (código do controlador)
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 (modelo pai)
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 (modelo filho)
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(); ?>
aqui está a imagem para referência
Nesta imagem, todos os campos são obrigatórios, todos os campos são da tabela de usuários ... apenas o campo de endereço é da tabela user_details.now você pode ver a mensagem de erro sendo exibida no campo de nome, mas a mensagem de erro não sendo exibida no campo de endereço. não sei por que a mensagem de erro não está sendo exibida.
primeiro nome da tabela de usuários e endereço da tabela user_details
desde já, obrigado