1

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?

7
  • 2
    Good idea to include the form code itself, since that's where you're having issue(s). Commented Apr 26, 2013 at 13:38
  • Can you verify that the validation error isn't being added and just hidden with CSS or something? Commented Apr 26, 2013 at 13:58
  • Yes tried to search it in the source code but it is not present Commented Apr 26, 2013 at 17:08
  • Just to verify; the code you provided for the form contains errors; you've got a closing ?> after $this->Form->create(); but not re-opening it to output the 'input'-tag? Could you please include the actual code? Commented Apr 26, 2013 at 20:15
  • 2
    also from this answer (and from my own experience) if you use read() after attempting to save() then the validation error messages will be unset. Commented Aug 1, 2013 at 18:50

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.