0

My User model:

class User extends AppModel {
    public $validate = array(
        'username' => 'alphaNumeric',
        'password' => 'notempty',
        'confirmpassword' => 'notempty',
        'group_id' => 'notempty',
        'email' => array( 
              'rule' => 'email', 
              'message' => 'Please provide a valid email address.' 
         ),
    );
}

My User Registration view:

<div class="users form">
        <?php echo $this->Form->create('User'); ?>
    <fieldset>
        <legend><?php echo __('Register User'); ?></legend>
                <?php
                        echo $this->Form->input('User.username');
                        echo $this->Form->input('User.password');
                        echo $this->Form->input('User.confirmpassword', array('type'=>'password', 'label'=>'Confirm password'));
                        echo $this->Form->input('User.group_id');
                        echo $this->Form->input('User.email');
                ?>
        </fieldset>
        <?php echo $this->Form->end(__('Submit')); ?>
</div>

My User Registration action in the Controller:

public function register() {
            if ($this->request->is('post')) {
                    ...
                    //create mail for verification
                    $email = new CakeEmail();
                    ...
                    $email->to($this->data['User']['email']);
                    $email->send($ms);
            }
}

Accessing /users/register, When I enter something like 'asdfgh' in the e-mail field, I get the default CakePHP error message:

Invalid email: asdfgh
Error: An Internal Error Has Occurred.

Instead of an error message above the e-mail field, telling me:

Please provide a valid email address.

My question: Should it really call the action in the controller when the user's input data is invalid? Is there a way to call the action in the controller only if the user's input data is valid?

cawecoy
  • 2,359
  • 4
  • 27
  • 36
  • I think there is something wrong in your controller. You can find more information about the error in /tmp/logs/error.log – noslone Nov 28 '12 at 10:07
  • You are right, the problem is an action in the controller, which try to send an e-mail, but it fails when the e-mail is invalid. How to validate the user's e-mail before call the action in the controller? – cawecoy Nov 29 '12 at 15:13
  • 1
    Try `if (!Validation::email($email)) { //Invalid email!; }` – noslone Nov 29 '12 at 15:31

1 Answers1

0

I found my answer! ^^

Actually, the validation is done only before the "save()" method, and not before the action call in the controller. But I can validate the user input in the controller, using:

if ( $this->User->validates() )

Reformulating my User Resgistration action in the controller:

public function register() {
            if ($this->request->is('post')) {
                    $this->User->set( $this->data );

                    if ( $this->User->validates() ) {
                            ...
                            //create mail for verification
                            $email = new CakeEmail();
                            ...
                            $email->to($this->data['User']['email']);
                            $email->send($ms);
                    }
            }
}
cawecoy
  • 2,359
  • 4
  • 27
  • 36