Reputation: 35
I am simply applying a validation rule in cakephp(2.x).When i save the data it saves into the database without validating the form.I am not able to find where i lack. I am beginner in cakephp. please suggest how can i validate my form. My form structure look like this.
<form class="form-horizontal" method="POST" action="saveUser">
<fieldset>
<legend>Register</legend>
<span class = "error">* Required field</span>
<div class="form-group">
<label for="inputName" class="col-lg-2 control-label">Name <span class="error">*</span></label>
<div class="col-lg-10">
<input type="text" class="form-control" id="inputName" name="name" placeholder="Name" required value="">
</div>
</div>
<div class="form-group">
<label for="inputEmail" class="col-lg-2 control-label">Email <span class="error">*</span></label>
<div class="col-lg-10">
<input type="email" class="form-control" id="inputEmail" name="email" placeholder="Email" required value="">
</div>
</div>
<div class="form-group">
<label for="inputPassword" class="col-lg-2 control-label">Password <span class="error">*</span></label>
<div class="col-lg-10">
<input type="password" class="form-control" id="inputPassword" name="password" placeholder="Password" required value="">
</div>
</div>
<div class="form-group">
<div class="col-lg-10 col-lg-offset-2">
<button type="reset" class="btn btn-default">Cancel</button>
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</fieldset>
</form>
And Model validate array like this
public $validate = array(
'name' => array(
'alphaNumeric' => array(
'rule' => 'alphaNumeric',
'required' => true,
'message' => 'Only Allows Letter and Numbers'
),
'between' => array(
'rule' => array('lengthBetween', 5, 15),
'message' => 'length upto 5 to 15 character'
)
),
'password' => array(
'rule' => array('minLength', '8'),
'message' => 'Password Too Short'
),
'email' => 'email',
);
and the controller like
if($this->request->is('post')){
if($this->User->save($this->request->data)){
$this->Session->setFlash(__('Employee Registration Successfully'));
$this->redirect(['action'=>'viewAdminDashboard']);
}
else{
$this->Session->setFlash(__('Cannot Register Employee'));
}
}
Upvotes: 0
Views: 194
Reputation: 146350
CakePHP cannot match input fields with model columns if you assign your own names. For instance:
<input type="text" class="form-control" id="inputName" name="name" placeholder="Name" required value="">
^^^^
... should be
name="data[User][name]"
If you're using CakePHP for model and controller, it'd be easier to just use it for views as well:
<?php
echo $this->Form->create('User');
echo $this->Form->input('name');
I highly recommend the Blog Tutorial.
Upvotes: 0