I have a field gentel_id with the following validation rules in my model Contrat:
public $validate = array(
'gentel_id' => array(
'numeric' => array(
'rule' => 'numeric',
'required' => true,
'allowEmpty' => false,
'message' => 'Veuillez entrer un identifiant GENTEL ou mettre la valeur 0000000000',
),
),
);
In my controller:
public function add($id = null) {
if ($this->request->is('post')) {
$this->Contrat->create();
if ($this->Contrat->save($this->request->data)) {
$this->Session->setFlash(__('Le Contrat a été ajouté'));
$this->redirect(array('action' => 'edit', $this->Contrat->getInsertID() ));
} else {
debug($this->Contrat->validationErrors);
$this->Session->setFlash(__('Le Contrat ne peut être ajouté'));
}
$this->set('id',$id);
...
}
In my form:
<?php echo $this->Form->create('Contrat');?>
<?php
if (empty($id)){
echo $this->Form->input('client_id',array('empty' => "Client non défini",'default' => $id, 'onchange'=>"window.location.href= '/contrats/add/'+this.form.ContratClientId.options[this.form.ContratClientId.selectedIndex].value"));
}else{
echo "<span class=\"label\">".__('Client')."</span>".$this->Kaldom->link(h($clients[$id]),array('controller' => 'clients', 'action' => 'view',$id));
echo $this->Form->input('client_id', array('type' => 'hidden','value' => $id));
}
echo $this->Form->input('gentel_id',array('type'=>'text','label'=> 'Gentel ID'));
?>
<?php echo $this->Form->end(__('Créer'));?>
When I try to save my form with "gentel_id" empty, the save is correctly not done and a debug ($this->Contrat->validationErrors) give me this:
Array
(
[gentel_id] => Array
(
[0] => Veuillez entrer un identifiant GENTEL ou mettre la valeur 0000000000
)
)
But the error messsage is not displayed in my form. Note that it is an add function but I have passed a parameter in the url like this: .../contrats/add/3
Can you help me please?
?>after$this->Form->create();but not re-opening it to output the 'input'-tag? Could you please include the actual code?read()after attempting tosave()then the validation error messages will be unset.