0

Ok, so I have validation somewhat working. It doesn't validate when it SHOULD, which seems to be the opposite of every problem I can find on google. I've tried copying the exact code from the CakePHP docs, but it doesn't seem to work. Maybe someone here can figure it out.

Model:

<?php

    App::uses('AppModel', 'Model');

    class User extends AppModel {

        public $validate = array(
            'email' => array(
                'rule' => 'email',
                'required' => true,
                'allowEmpty' => false
            ),
            'full_name' => array(
                'rule'     => 'alphaNumeric',
                'required' => true,
                'allowEmpty' => false
            ),
            'password' => array(
                'rule' => array('minLength', 8),
                'required' => true,
                'allowEmpty' => false
            )
        );

    }

?>

Controller:

<?php

    App::uses('AppController', 'Controller');


    class UsersController extends AppController {

        function login() {
            $this->layout = 'signin';
        }

        function signup() {
            $this->layout = 'signin';

            if($this->request->is('post')) {

                $this->User->set($this->request->data);

                if($this->User->validates())
                    $this->Session->setFlash('Validated!');
                else
                    $this->Session->setFlash('Did not validate!' . print_r($this->User->validationErrors, true) . print_r($this->request->data, true));
            }
        }
    }

?>

View:

<div class="placeholder text-center"><i class="fa fa-pencil"></i></div>

<?php echo $this->Session->flash(); ?>
<div class="panel panel-default col-sm-6 col-sm-offset-3">

    <div class="panel-body">
        <?php echo $this->Form->create('User'); ?>

            <div class="form-group">
                <?php echo $this->Form->input('full_name', array('placeholder' => 'Your full name', 'class' => 'form-control')); ?>
            </div>

            <div class="form-group">
                <?php echo $this->Form->input('email', array('placeholder' => 'Enter email', 'class' => 'form-control')); ?>
            </div>

            <div class="form-group">
                <?php echo $this->Form->input('password', array('placeholder' => 'Password', 'class' => 'form-control')); ?>
            </div>

            <div class="form-group">
                <?php echo $this->Form->input('confirm_password', array('placeholder' => 'Retype Password', 'class' => 'form-control')); ?>
            </div>

            <button type="submit" class="btn btn-primary btn-block">Create Account</button>

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

Any help in the right direction is appreciated. I've always had issues with validation with CakePHP so I never used it before. Now it's required so I have no choice but to drudge through this until I get it working.

Oh, I should note that the data does go through. Here's the result of the print_r function:

Did not validate!Array ( [full_name] => Array ( [0] => This field cannot be left blank ) [password] => Array ( [0] => This field cannot be left blank ) ) Array ( [User] => Array ( [full_name] => Sean Templeton [email] => sean@********.com [password] => ******** [confirm_password] => ******** ) )

2 Answers 2

1

Please go through this link. It explains how cakephp validations work.

http://book.cakephp.org/2.0/en/models/data-validation/validating-data-from-the-controller.html

Updated:

Your fullname validation has 'rule'=> 'alphaNumeric' which does not include spaces. but if you check your data [full_name] => Sean Templeton which has a space in it.

You can set your own messages in the model. I don't think I need to say that.

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

7 Comments

I did...didn't you read my post? Not only did I read it, I copied the code from it. I just can't get it to work.
Check the action. I don't see any action set in the form. How does you code call the signup actions?
CakePHP automatically creates the form with the correct action. To be specific it outputs this: <form action="/users/signup" id="UserSignupForm" method="post" accept-charset="utf-8"> I don't want to sound rude, but do you even know much about CakePHP?
Yah, you are rude but no problem. I have started working on Cake very recently. So don't know much. I accept that. But, I am trying to help you out.
Your fullname validation has 'rule'=> 'alphaNumeric' which does not include spaces. but if you check your data [full_name] => Sean Templeton which has a space in it. I hope this time I am not wrong.
|
0

Try this in your controller

function signup() {
     $this->layout = 'signin';
     if ($this->request->is('post')) {
         $this->User->create($this->request->data); //"create" instead of "set"
         if ($this->User->validates())
             $this->Session->setFlash('Validated!');
         else
             $this->Session->setFlash('Did not validate!' . print_r($this->User->validationErrors, true) . print_r($this->request->data, true));
        }
    }
}

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.