Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to Show error message in cakephp validation #2

Closed
muhammadcahya opened this issue Jun 21, 2013 · 9 comments
Closed

Unable to Show error message in cakephp validation #2

muhammadcahya opened this issue Jun 21, 2013 · 9 comments

Comments

@muhammadcahya
Copy link

I try to implement field in bootsrap like Html->input helper in original cakephp.
In original field, if the value empty or value is invalid from model, the field in view is automatically show error message.

I try in example in BoostCakePassword field, but if my field is empty or value is invalid it's not showing the error message :(

I missing something, or the cakephp validation is not connected with this plugin ?

@slywalker
Copy link
Owner

Please show me view and Model::$validate code.

@Kareylo
Copy link

Kareylo commented Jun 24, 2013

There's the same problem with auth's flashes.

My Validate code :

<?php
class User extends AppModel{

    public $validate = array(
        'username' => array(
            'alpha' => array(
                'rule' => '/^[a-z0-9-A-Z]*$/',
                'message' => 'Username is not valid'
            ),
            'uniq' => array(
                'rule' => 'isUnique',
                'message' => "The Username is already used"
            )
        ),
        'mail' => array(
            'mail' => array(
                'rule' => 'email',
                'message' => 'Mail is not valid'
            ),
            'uniq' => array(
                'rule' => 'isUnique',
                'message' => 'This Mail Address is already used'
            )
        ),
        'password' => array(
            'rule' => 'notEmpty'
        ),
        'password2' => array(
            'rule' => 'identicalFields'
        )
    );

    public function identicalFields($check, $limit){
        return $check['password2'] ==  $this->data['User']['password'];
    }

}

@slywalker
Copy link
Owner

It is displayed as this in my environment

Model/User.php

<?php
class User extends AppModel {

    public $validate = array(
        'username' => array(
            'alpha' => array(
                'rule' => '/^[a-z0-9-A-Z]*$/',
                'message' => 'Username is not valid'
            ),
            'uniq' => array(
                'rule' => 'isUnique',
                'message' => "The Username is already used"
                )
            ),
        'mail' => array(
            'mail' => array(
                'rule' => 'email',
                'message' => 'Mail is not valid'
            ),
            'uniq' => array(
                'rule' => 'isUnique',
                'message' => 'This Mail Address is already used'
            )
        ),
        'password' => array(
            'rule' => 'notEmpty'
        ),
        'password2' => array(
            'rule' => 'identicalFields',
            'message' => 'Password is not same'
        )
    );

    public function identicalFields($check, $limit) {
        return $check['password2'] === $this->data['User']['password'];
    }

}

View/Users/add.ctp

<?php echo $this->Form->create('User', array(
    'inputDefaults' => array(
        'div' => 'control-group',
        'label' => array(
            'class' => 'control-label'
        ),
        'wrapInput' => 'controls'
    ),
    'class' => 'form-horizontal'
)); ?>
    <?php echo $this->Form->input('username'); ?>
    <?php echo $this->Form->input('mail'); ?>
    <?php echo $this->Form->input('password'); ?>
    <?php echo $this->Form->input('password2', array('type' => 'password')); ?>
    <?php echo $this->Form->submit('Submit', array(
        'div' => 'form-actions',
        'class' => 'btn'
    )); ?>
<?php echo $this->Form->end(); ?>

It mean message of password2?

@slywalker
Copy link
Owner

@Kareylo
Oh, you mean Auth flash!

Auth flash setting

class AppController extends Controller {

    public $components = array(
        'Auth' => array(
            'flash' => array(
                'element' => 'alert',
                'key' => 'auth',
                'params' => array('plugin' => 'BoostCake', 'class' => 'alert-error')
            )
        )
    );
}

@Kareylo
Copy link

Kareylo commented Jun 25, 2013

For Auth Flash, it's ok, now, Thanks a lot ! (Don't forget to add that to the doc !)

But, for my validation form looks like:
Validation Form

(The Confirm Password message, I'll change it later, I know) Any idea ?

Here my form :

        <?php echo $this->Form->create('User', array(
            'inputDefaults' => array(
                'div' => false
            ),
            'wrapInput' => 'controls'
        )); ?>
            <?php echo $this->Form->input('username', array(
                'label' => 'Pseudo',
            )); ?>
            <?php echo $this->Form->input('account', array(
                'label' => 'Nom de Compte',
            )); ?>
            <?php echo $this->Form->input('password', array(
                'label' => 'Mot de Passe',
            )); ?>
            <?php echo $this->Form->input('password2', array(
                'label' => 'Confirmer le Mot de Passe',
                'type' => 'password'
            )); ?>
            <?php echo $this->Form->input('mail', array(
                'label' => 'Email',
            )); ?>

            <?php echo $this->Form->submit('S\'inscrire', array(
                'class' => 'btn btn-primary'
            )); ?>

        <?php echo $this->Form->end(); ?>

@slywalker
Copy link
Owner

@Kareylo, If you want error color, it need that array('inputDefaults' => array('div' => 'some class or null'))

Yes, I should add doc soon later :)

@Kareylo
Copy link

Kareylo commented Jun 25, 2013

With array('inputDefaults' => array('div' => 'control-group')) It works like a charm. Thx ! 👍

@muhammadcahya
Copy link
Author

Ok, sorry I have not checked this issue last a few days
And when I read the comments above, my problem is solved. Thanks 👍
Add in documentation and Close :)

@ghost
Copy link

ghost commented Mar 21, 2014

Hi I am having the same trouble:

Database Error
Error: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '' for key 'email'

SQL Query: INSERT INTO `DB`.`abc_users` (`password`) VALUES ('$2a$10$WlBbc5kxyP8vJFdU/9m89eJ5TTCM8q8Q1OGrQn1GX5Z3e260KJ61m')

Notice: If you want to customize this error message, create app/View/Errors/pdo_error.ctp

register.ctp:

<?php
    echo '<div class="row">';
    echo '<div class="col-md-3"></div>';
    echo '<div class="col-md-6">';

    echo $this->Session->flash(); 

    echo $this->Form->create('BoostCake', array(
        'inputDefaults' => array(
            'div' => 'form-group',
            'label' => array(
                'class' => 'col col-md-3 control-label'
            ),
            'wrapInput' => 'col col-md-9 controls',
            'class' => 'form-control'
        ),
        'class' => 'well form-horizontal'
    )); 

    echo '<fieldset>';
    echo '<legend>Register</legend>';

    echo $this->Form->input('firstname', array('label' => 'First name'));
    echo $this->Form->input('lastname', array('label' => 'Last name'));
    echo $this->Form->input('email');
    echo $this->Form->input('password');
    echo $this->Form->input('phone');
    echo $this->Form->input('website');

    echo '<div class="form-group">';
    echo '<div class="col col-md-9 col-md-offset-3">';
    echo $this->Form->submit('Register', array(
            'div' => 'form-actions',
            'class' => 'btn btn-primary'
        ));
    echo '</div>';
    echo '</div>';

    echo $this->Form->end();
    echo '</fieldset>';
    echo '</div>';
    echo '</div>';
?>  

User.php:

<?php
App::uses('Security', 'Utility');
class User extends AppModel{
    public $useTable = 'abc_users';

    public $validate = array(
        'firstname' => array(
            'rule' => 'notEmpty', 
            'message' => 'Please fill in a first name'
        ), 
        'lastname' => array(
            'rule' => 'notEmpty', 
            'message' => 'Please fill in a last name'
        ), 
        'email' => array(
            'rule' => 'notEmpty', 
            'message' => 'Please fill in an email address'
        ), 
        'email' => array(
            'rule' => 'email', 
            'message' => 'Please fill in a valid email address'
        ), 
        'email' => array(
            'rule' => 'isUnique',
            'message' => 'Email is already registered'
        ),
        'password' => array(
            'rule' => array('between', 5, 10),
            'message' => 'Password must be between 5 and 10 characters long'
        )
    );

    public function beforeSave($option = array()){
        $this->data['User']['password'] = Security::hash($this->data['User']['password'], 'blowfish');
        return true;
    }
}

?>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants