0

I'm trying to display validation errors with CakePHP (Newbie) but I'm stuck. I get this error "Delimiter must not be alphanumeric or backslash". Don't know if the logic is respected, I'm starting from scratch.

Nothing is displayed. Here's my code:

User model

class User extends AppModel {
public $validate = array(
    'nom' => array(
            'message' => 'Saisie obligatoire',
            'required' => true
        ),
    'prenom' => array(
            'message' => 'Saisie obligatoire',
            'required' => true
    ),
    'date_naissance' => array(
            'rule' => array('date','dmy'),
            'message' => 'Veuillez respecter le format de la date     (jour/mois/année)',
            'allowEmpty' => true
    ),
    'email' => array(
            'rule' => 'email',
            'message' => 'Veuillez introduire une adresse mail valide',
            'required' => true
        ),
    'password' => array(
            'rule' => 'password',
            'message' => 'Un mot de passe est requis'
        )
);

public function beforeSave($options = array()) {
    if (isset($this->data[$this->alias]['password'])) {
        $this->data[$this->alias]['password'] =     AuthComponent::password($this->data[$this->alias]['password']);
    }
    return true;
}
}

add function into UsersController

public function add() {
    if ($this->request->is('post')) {
        $this->User->set($this->request->data);
            if ($this->User->validates()) {
            $this->User->create();
            if ($this->User->save($this->request->data)) {
                $this->Auth->login($this->User);
                return $this->redirect('/index');
            }
        }
    } else {
        return $this->User->validationErrors;
    }
}

add.ctp

<?= $this->element('navbar');?>

<div class="formcontainer">
<div class="page-header">
    <h1>Rejoignez-nous</h1>
</div>
<form action="/users/add" id="UserAddForm" method="post" accept-charset="utf-8">
    <div style="display:none;">
        <input type="hidden" name="_method" value="POST"/>
    </div>
    <div class="form-group input text">
        <label for="UserNom">Nom:</label>
        <input name="data[User][nom]" maxlength="20" type="text" id="UserNom" class="form-control" placeholder="requis">
    </div>
    <div class="form-group input text">
        <label for="UserPrenom">Prénom:</label>
        <input name="data[User][prenom]" maxlength="20" type="text" id="UserPrenom" class="form-control" placeholder="requis">
    </div>
    <div class="form-group input text">
        <label for="UserDateNaissance">Date de naissance:</label>
        <input name="data[User][date_naissance]" maxlength="20" type="text" id="UserDateNaissance" class="form-control">
    </div>
    <div class="form-group input email">
        <label for="UserEmail">Email:</label>
        <input name="data[User][email]" maxlength="100" type="email" id="UserEmail" class="form-control" placeholder="requis"/>
    </div>

    <div class="form-group input password">
        <label for="UserPassword">Mot de passe:</label>
        <input type="password" name="data[User][password]" class="form-control" id="UserPassword" placeholder="requis">
    </div>
    <button type="submit" class="btn btn-default bSub">M&#039;inscrire</button>
</form>
</div>
1
  • You should use cakephp coding style for your add.ctp or you can achieve this by passing errors messages from your controller. Commented Jun 28, 2015 at 19:34

2 Answers 2

1

Your validation should be like this(You must add rule for field and there is no inbuilt passowrd rule in cakephp).

public $validate = array(
    'nom' => array(
        'rule' => 'notEmpty', //add rule here
        'message' => 'Saisie obligatoire',
        'required' => true
    ),
    'prenom' => array(
        'rule' => 'notEmpty', //add rule here
        'message' => 'Saisie obligatoire',
        'required' => true
    ),
    'date_naissance' => array(
        'rule' => array('date', 'dmy'),
        'message' => 'Veuillez respecter le format de la date     (jour/mois/année)',
        'allowEmpty' => true
    ),
    'email' => array(
        'rule' => 'email',
        'message' => 'Veuillez introduire une adresse mail valide',
        'required' => true
    ),
    'password' => array(
        'rule' => 'notEmpty', //there is no inbuilt validation rule with name *password*
        'message' => 'Un mot de passe est requis'
    )
);

If you want to show error message then you should use Form Helper for create form inputs like this

echo $this->Form->input("User.nom", array("class"=>"form-control", "placeholder"=>"requis", 'label'=>false));

Or you can display message by using isFieldError method of Form helper

if ($this->Form->isFieldError('nom')) {
echo $this->Form->error('nom');

}

Sign up to request clarification or add additional context in comments.

Comments

1

// Model

class User extends AppModel {

    public $validate = array(

        'password' => array(
            'notEmpty' => array(
                'rule' => array('notEmpty'),
                'message' => 'Un mot de passe est requis'
                //'allowEmpty' => false,
                //'required' => false,
                //'last' => false, // Stop validation after this rule
                //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
        ),
        'email' => array(
            'email' => array(
                'rule' => array('email'),
                'message' => 'Veuillez introduire une adresse mail valide',
                //'allowEmpty' => false,
                //'required' => false,
                //'last' => false, // Stop validation after this rule
                //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
        ),
        'date_naissance' => array(
            'date' => array(
                'rule' => array('date'),
                'message' => 'Veuillez respecter le format de la date     (jour/mois/année)',
                //'allowEmpty' => false,
                //'required' => false,
                //'last' => false, // Stop validation after this rule
                //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
        ),
        'prenom' => array(
            'notEmpty' => array(
                'rule' => array('notEmpty'),
                'message' => 'Saisie obligatoire',
                //'allowEmpty' => false,
                //'required' => false,
                //'last' => false, // Stop validation after this rule
                //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
        ),
        'nom' => array(
            'notEmpty' => array(
                'rule' => array('notEmpty'),
                'message' => 'Saisie obligatoire',
                //'allowEmpty' => false,
                //'required' => false,
                //'last' => false, // Stop validation after this rule
                //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
        ),
    );
    public function beforeSave($options = array()) {
        if (isset($this->data[$this->alias]['password'])) {
            $this->data[$this->alias]['password'] =     AuthComponent::password($this->data[$this->alias]['password']);
        }
        return true;
    }
}

// Controller

public function add() {
    if ($this->request->is('post')) {
        $this->User->create();
        if ($this->User->save($this->request->data)) {
            $this->Session->setFlash(__('The user has been saved.'));
            return $this->redirect(array('action' => 'index'));
        } else {
            $this->Session->setFlash(__('The user could not be saved. Please, try again.'));
        }
    }
}

// add.ctp

<div class="users form">
<?php echo $this->Form->create('User'); ?>
    <fieldset>
        <legend><?php echo __('Add User'); ?></legend>
    <?php
        echo $this->Form->input('password');
        echo $this->Form->input('email');
        echo $this->Form->input('date_naissance');
        echo $this->Form->input('prenom');
        echo $this->Form->input('nom');
    ?>
    </fieldset>
<?php echo $this->Form->end(__('Submit')); ?>
</div>
<div class="actions">
    <h3><?php echo __('Actions'); ?></h3>
    <ul>

        <li><?php echo $this->Html->link(__('List Users'), array('action' => 'index')); ?></li>
    </ul>
</div>

enter image description here

Comments

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.