Reputation: 8293
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] =>********
) )
Upvotes: 0
Views: 105
Reputation: 28
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.
Upvotes: 1
Reputation: 3889
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));
}
}
}
Upvotes: 0