I have now got the system going through the process however for some reason, the code for actually updating the password in the database isn't updating anything? After insert new password and re-password, system immediately redirect to the login page, Even though no errors are shown? Any thoughts? thanks.
This is my Reset Function
function reset($token=null){
$this->User->recursive=-1;
if(!empty($token)){
$u=$this->User->findBytokenhash($token);
if($u){
$this->User->id=$u['User']['id'];
if($this->request->is('post')){
$this->User->data=$this->request->data;
$this->User->data['User']['username']=$u['User']['username'];
$new_hash=sha1($u['User']['username'].rand(0,100));//created token
$this->User->data['User']['tokenhash']=$new_hash;
// print_r($this->request->data);
// exit;
if($this->User->validates(array('fieldList'=>array('password','password_confirm')))){
if($this->User->save($this->User->data)){
$this->Session->setFlash('Password Has been Updated');
$this->redirect(array('controller'=>'users','action'=>'login'));
}
} else{
$this->set('errors',$this->User->invalidFields());
}
}
} else {
$this->Session->setFlash('Token Corrupted,,Please Retry.the reset link work only for once.');
}
} else{
$this->redirect('/');
}
}
And this is my reset.ctp
<?php echo $this->Form->create('User', array('action' => 'reset')); ?>
<?php
if(isset($errors)){
echo '<div class="error">';
echo "<ul>";
foreach($errors as $error){
echo "<li><div class='error-message'>$error</div></li>";
}
echo "</ul>";
echo '</div>';
}
?>
<form method="post">
<?php
echo $this->Form->input('User.password', array('type' => 'password',
'name' => '$data[User][password]',
'class' => 'form-control'));
echo $this->Form->input('User.re_password', array('type' => 'password',
'name' => 'data[User][re_password]',
'class' => 'form-control'));
?>
<input type="submit" class="button" style="float:left;margin-left:3px;" value="Save" />
<?php echo $this->Form->end();?>
</form>
Please, help me , thanks
Replace your ctp code by this
<?php echo $this->Form->create('User'); ?>
<?php
if(isset($errors)){
echo '<div class="error">';
echo "<ul>";
foreach($errors as $error){
echo "<li><div class='error-message'>$error</div></li>";
}
echo "</ul>";
echo '</div>';
}
?>
<?php
echo $this->Form->input('User.password', array('type' => 'password',
'name' => '$data[User][password]',
'class' => 'form-control'));
echo $this->Form->input('User.re_password', array('type' => 'password',
'name' => 'data[User][re_password]',
'class' => 'form-control'));
?>
<input type="submit" class="button" style="float:left;margin-left:3px;" value="Save" />
<?php echo $this->Form->end();?>
Hope this will work.
I just noticed ,
You shuld use $this->User->save($this->request->data)
Check out using this
echo $this->Form->input('User.password', array('type' => 'password',
'class' => 'form-control'));
echo $this->Form->input('User.re_password', array('type' => 'password',
'class' => 'form-control'));
Let name attribute built by cakephp to match with database table,fields. I think you have only one or 2 ctp etc, you are just building your project from start , correct?
try this :
public function beforeSave() {
if (isset($this->data['User']['password'])) {
$this->data['User']['password'] = AuthComponent::password($this->data['User']['password']);
}
return true;
}
and in function reset
if($this->User->save($this->User->data,false))
it worked for me. Hope it works for you!!
Related
I want call the javascript function into callback function of codeigniter controller.
Controller: Callback function
public function email_exist($id) {
$this->db->where('email', $id);
$query = $this->db->get('login');
if (!$query->num_rows() > 0) {
$this->form_validation->set_message(__FUNCTION__, $this->email_validate());
return FALSE;
} else {
return TRUE;
}
Javascript: Alert function
function email_validate(){
alert('Invalid Email Id');
}
An uncaught Exception was encountered
Type: Error
Message: Call to undefined method Login::email_validate()
Filename: C:\wamp64\www\CodeIgniterProject1\application\controllers\login.php
Line Number: 46
login_form.php
<?php echo validation_errors(); ?>
<?php echo form_open_multipart(); ?>
<form name="form">
<div class="loginform">
<?php echo form_label('LOGIN', 'login'); ?>
</div>
<div class="email">
<?php echo form_input('email', 'E-MAIL', set_value('email')); ?>
</div>
<div class="password">
<?php echo form_password('password', 'PASSWORD', set_value('password')); ?>
</div>
<div class='checkbox'>
<?php
echo form_checkbox(array(
'name' => 'remember',
'id' => 'remember',
'value' => 'REMEMBER ME',
'checked' => FALSE,
'style' => 'margin:1px'
)) . form_label('REMEMBER ME', 'remember');
?>
</div>
<div class='btnlogin'>
<?php echo form_submit('login', 'LOGIN'); ?>
</div>
</form>
you cant do that in Codeigniter, use Codeigniter default form validation library
View here
if you want to add css for validation errors then try set_error_delimeters()
if you have bootstrap then use
alert alert-danger
class, else create one class
$this->form_validation->set_error_delimiters('<div class="alert alert-danger">', '</div>');
see
[other suggestions]
instead of putting all error in the top, you can also try
<?php
echo form_error('email');
form_input('email', 'E-MAIL', set_value('email'));
?>
other options is jquery validation or js validation, write js in view or external .js file
<?php
data = array(
'name' => 'username',
'id' => 'username', //use the id for jquery validation $('#username').on('blur', function() { });
'value' => 'johndoe',
'maxlength' => '100',
'size' => '50',
'style' => 'width:50%'
'onBlur' => 'email_validate();' //use this for javascript validation
);
echo form_input($data);
?>
im unable to comment, thats why posting as answer
I am trying to add a child in a parent view in CakePHP.
Meaning, I have a Lesson I want to add to a Course in the Course view.
I tried doing it via a simple form helper - adding a $course['Lesson']:
<div class="lessons form">
<?php echo $this->Form->create($course['Lesson']); ?>
<fieldset>
<legend><?php echo __('Add Lesson'); ?></legend>
<?php
echo $this->Form->input('name');
echo $this->Form->input('description');
echo $this->Form->input('course_id', array(
'value'=>$course['Course']['id']
));
?>
</fieldset>
<?php echo $this->Form->end(__('Submit')); ?>
</div>
But that doesn't seem to do it.
Am I missing something in the Controller/Model?
let's think course controller
//course controller
public function add_lesson() {
if ($this->request->is('post')) {
$this->Lesson->create();
if ($this->Lesson->save($this->request->data)) {
$this->Session->setFlash(__('The Lesson has been saved.'));
return $this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The Lesson could not be saved. Please, try again.'));
}
}
$this->set('courses', $this->Course->find('list'));
}
//add_Lesson View
<?php echo $this->Form->create('Lesson', array('url' => array('controller'=>'courses', 'action'=>'add_lesson')) ); ?>
<fieldset>
<legend><?php echo __('Add Lesson'); ?></legend>
<?php
echo $this->Form->input('name');
echo $this->Form->input('description');
echo $this->Form->input('course_id');
?>
</fieldset>
<?php echo $this->Form->end(__('Submit')); ?>
If you want to use Course controller you can save your data like this:
$this->Course->Lesson->save($this->request->data);
in this case you will need to make sure after submitting your form your data is structure as below:
array(
'Course' => array(
'id' => 1
),
'Lesson' => array(
'name' => 'name'
'description' => 'balbalbala'
'course_id' => 1
),
);
I have used codeigniter in the past but I am building a social network and I am working on the registration. I have autoloaded the form and form validation helpers. I have a form going to my user controller/register function and I have validation rules setup but its ignoring them and proceeding to the next page. I cant for the life of me figure out why. Any help is appreciated..the code and rules are not complete (obviously) but the validation should work at this point
VIEW
<?php echo validation_errors('<p class="error">'); ?>
<?php echo form_open('user/register/1'); ?>
<?php echo form_hidden('register_id', $randomcode); ?>
<?php echo form_label('First Name', 'first_name'); ?>
<?php echo form_input('first_name',set_value('first_name')); ?>
<br />
<?php echo form_label('Last Name', 'last_name'); ?>
<?php echo form_input('last_name',set_value('last_name')); ?>
<br />
<?php echo form_label('Gender', 'gender:'); ?>
<?php echo form_radio('gender', 'male'); ?> Male
<?php echo form_radio('gender', 'female'); ?> Female
<br />
<?php echo form_label('Date of Birth:', 'birthdate'); ?>
<?php echo form_date('birthdate'); ?>
<br />
<?php echo form_label('Zipcode:', 'zipcode'); ?>
<?php echo form_input('zipcode',set_value('zipcode')); ?>
<br />
<?php echo form_label('Email:', 'email'); ?>
<?php echo form_email('email',set_value('email')); ?>
<br />
<?php echo form_label('Password', 'password'); ?>
<?php echo form_password('password'); ?>
<br />
<?php echo form_label('Confirm', 'password2'); ?>
<?php echo form_password('password2'); ?>
<br />
<?php
$data = array(
'name' => 'submit',
'class' => 'big-blue-btn',
'value' => 'Sign Up'
);
?>
<?php echo form_submit($data); ?>
<?php echo form_close(); ?>
CONTROLLER
public function register($step){
if($step == 1){
$this->form_validation->set_rules('first_name', 'first_name', 'required');
$this->form_validation->set_rules('last_name', 'last_name', 'required');
if($this->form_validation->run() == FALSE){
//Views
$data['main_content'] = 'public/user/register1';
$this->load->view('public/template', $data);
} else {
//Post values to array
$data = array(
'register_id' => $this->input->post('register_id'),
'first_name' => $this->input->post('first_name'),
'last_name' => $this->input->post('last_name'),
'gender' => $this->input->post('gender'),
'birthdate' => $this->input->post('birthdate'),
'zipcode' => $this->input->post('zipcode'),
'email' => $this->input->post('email'),
'password' => $this->input->post('password'),
'password2' => $this->input->post('password2')
);
//Send input to model
$this->User_model->register1($data);
$data['show_sidebar'] = FALSE;
}
} elseif($step == 2){
//Send input to model
$this->User_model->register2($data);
} elseif($step == 3){
//Send input to model
$this->User_model->register3($data);
}
}
Review your code, you're missing a closing bracket on your first if.
I have a CActiveForm which when submitted should make a second CActiveForm become visible. I know how to change the htmlOptions of the form when its created but not how to access it via the controller.
My View with two forms. The second form has visibility:hidden
<div class="form">
<?php
$numberForm = $this->beginWidget('CActiveForm', array(
'id' => 'addnumber-form',
'enableAjaxValidation' => true,
'clientOptions' => array(
'validateOnSubmit' => true,
),
));
?>
<p class="note"><?php echo UserModule::t('Fields with <span class="required">*</span> are required.'); ?></p>
<?php echo $numberForm->errorSummary($numberModel); ?>
<div class="row">
<?php echo $numberForm->labelEx($numberModel, 'number'); ?>
<?php echo $numberForm->textField($numberModel, 'number'); ?>
<?php echo $numberForm->error($numberModel, 'number'); ?>
<?php
echo CHtml::submitButton(UserModule::t("Verify"), array(
"class" => "btn btn-success"
));
?>
</div>
<?php $this->endWidget(); ?>
<?php
$verifyForm = $this->beginWidget('CActiveForm', array(
'id' => 'verify-form',
'enableAjaxValidation' => true,
'clientOptions' => array(
'validateOnSubmit' => true,
),
'htmlOptions' => array("style"=>"visibility: hidden"),
));
?>
<?php echo $verifyForm->errorSummary($verifyModel); ?>
<p>A authorisation code has been sent to your phone. Please enter it below. If you don't receive a text message make sure you entered your number correctly and try again</p>
<div class="row">
<?php echo $verifyForm->labelEx($verifyModel, 'authcodeUser'); ?>
<?php echo $verifyForm->textField($verifyModel, 'authcodeUser'); ?>
<?php echo $verifyForm->error($verifyModel, 'authcodeUser'); ?>
<?php
echo CHtml::submitButton(UserModule::t("Confirm"), array(
"class" => "btn btn-success"
));
?>
<?php
foreach(Yii::app()->user->getFlashes() as $key => $message) {
echo '<div class="flash-' . $key . '">' . $message . "</div>\n";
}
?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
My controller for these forms
public function actionAddnumber(){
$numberModel = new UserAddNumber;
$verifyModel = new UserVerifyNumber;
if (Yii::app()->user->id) {
// ajax validator
if(isset($_POST['ajax']) && $_POST['ajax']==='addnumber-form')
{
echo UActiveForm::validate($numberModel);
Yii::app()->end();
}
if(isset($_POST['UserAddNumber'])) {
$numberModel->attributes=$_POST['UserAddNumber'];
if($numberModel->validate()) {
$profile = Profile::model()->findByAttributes(array('user_id'=>Yii::app()->user->id));
$profile->mobileNo = $numberModel->number;
$profile->save();
//MAKE $verifyForm visibility to visible uring htmlOptions
Yii::app()->session['authcode'] = '4444';
}
}
if(isset($_POST['UserVerifyNumber'])) {
$verifyModel->attributes=$_POST['UserVerifyNumber'];
if($verifyModel->validate()) {
$profile = Profile::model()->findByAttributes(array('user_id'=>Yii::app()->user->id));
$profile->mobileNoVerified = True;
$profile->save();
Yii::app()->user->setFlash('profileMessage',UserModule::t("Your mobile number has been verified"));
$this->redirect(array("profile"));
}
}
}
$this->render('addnumber', array('numberModel'=>$numberModel, 'verifyModel' => $verifyModel));
}
It looks like you could just create a new variable for whether or not to show the second form and then pass it to the view. Here is your controller:
public function actionAddnumber(){
$numberModel = new UserAddNumber;
$verifyModel = new UserVerifyNumber;
$formVisibility = "hidden";
if (Yii::app()->user->id) {
// ajax validator
if(isset($_POST['ajax']) && $_POST['ajax']==='addnumber-form')
{
echo UActiveForm::validate($numberModel);
Yii::app()->end();
}
if(isset($_POST['UserAddNumber'])) {
$numberModel->attributes=$_POST['UserAddNumber'];
if($numberModel->validate()) {
$profile = Profile::model()->findByAttributes(array('user_id'=>Yii::app()->user->id));
$profile->mobileNo = $numberModel->number;
$profile->save();
//MAKE $verifyForm visibility to visible uring htmlOptions
$formVisibility = "visible";
Yii::app()->session['authcode'] = '4444';
}
}
if(isset($_POST['UserVerifyNumber'])) {
$verifyModel->attributes=$_POST['UserVerifyNumber'];
if($verifyModel->validate()) {
$profile = Profile::model()->findByAttributes(array('user_id'=>Yii::app()->user->id));
$profile->mobileNoVerified = True;
$profile->save();
Yii::app()->user->setFlash('profileMessage',UserModule::t("Your mobile number has been verified"));
$this->redirect(array("profile"));
}
}
}
$this->render('addnumber', array('numberModel'=>$numberModel, 'verifyModel' => $verifyModel, 'formVisibility' => $formVisibility));
}
And here is the first part of your second form:
<?php
$verifyForm = $this->beginWidget('CActiveForm', array(
'id' => 'verify-form',
'enableAjaxValidation' => true,
'clientOptions' => array(
'validateOnSubmit' => true,
),
'htmlOptions' => array("style"=>"visibility: ".$formVisibility),
));
?>
[edit] To make sure I am answering your question, I should add that I've never seen any way of changing the htmlOptions from the controller directly. That's why I proposed this solution instead.
I'm trying to add a layout for a cakephp application but now my validation message is no longer being displayed. When validating a comment on a blog entry, the validation message thats suppose to be at the top is not displayed.
If you changing the layout means that you missed to add
<?php
if ($this->Session->check('Message.flash')){
echo $this->Session->flash();
}
?>
before the
Other possible place is in the current controller.
Search if you have code like:
$this->Session->setFlash('...');
The first code is responsible for displaying the message, while the second one is responsible for setting the message.
But the code definitely will help more :)
Here is my add function in comments_controller.php
function add(){
debug($this->data);
//if the user submitted a comment post
if (!empty($this->data)){
//display the 'add view'
$this->Comment->create();
if ($this->MathCaptcha->validates($this->data['Comment']['security_code'])) {
if ($this->Comment->save($this->data)){
$this->Session->setFlash(__('The Comment has been added.', true));
$this->redirect('/posts/index/'.$this->data['Comment']['post_id']);
}
//failed validation
else{
debug($this->data);
$this->Session->setFlash(__('Comment could not be saved. Please try again.', true));
}
}
else {
$this->Session->setFlash(__('Please enter the correct answer to the math question.', true));
$this->redirect('/posts/index/'.$this->data['Comment']['post_id']);
}
Here is my entry.ctp where my posts and comments reside:
<div id="article">
<h2><?php echo $entry[0]['Post']['title']; ?></h2>
<p class="date"><em>Modified:</em> <?php $date = new DateTime($entry[0]['Post']['modified']);
echo $date->format('Y-m-d');?></p>
<p class="date"><em>Author:</em> <?php echo $entry[0]['User']['username']; ?></p>
<p class="intro"><?php echo $entry[0]['Post']['content']; ?></p>
<h2>Comments:</h2>
<div id="comments_success"></div>
<!-- show the comment -->
<?php
echo $form->create('Comment', array('action' => 'add'));
echo $form->input('name', array('class' => 'validate[required] text-input'));
echo $form->input('email', array('class' => 'validate[required,custom[email]] text-input'));
echo $form->input('text', array('id' => 'commenttext', 'type' => 'textarea', 'label' => 'Comment:', 'rows' => '10', 'class' => 'validate[required] text-input'));
//captcha
echo $form->input('security_code', array('label' => 'Please Enter the Sum of ' . $mathCaptcha));
echo $form->input( 'Comment.post_id', array( 'value' => $entry[0]['Post']['id'] , 'type' => 'hidden') );
echo $form->end('Submit');
?>
<!-- comments -->
<ol>
<?php
foreach ($entry[0]['Comment'] as $comment) :
?>
<li>
<h3><?php echo $comment['name']; ?></h3>
<p class="date"><em>Date:</em> <?php echo $comment['created']; ?></p>
<p class="text"> <?php echo $comment['text']; ?></p>
</li>
<?php
endforeach;
?>
</ol>
</div>
This is the index function in my posts_controller
function index($entry_id = null) {
if (isset($entry_id)){
$entry = $this->Post->findAllById($entry_id);
$comments = $this->Post->Comment->getCommentsFromPostID($entry_id);
$this->set('entry' , $entry);
$this->set('mathCaptcha', $this->MathCaptcha->generateEquation());
$this->render('entry');
}
else{
$posts = $this->Post->find('all');
$this->set(compact('posts'));
}
}
I know this is old, but ust make sure you have the following code somewhere visible in your app/views/layouts/default.ctp (or whatever is your layout for this application)
<?php echo $this->Session->flash(); ?>
It will echo nothing if there is no message to be displayed, but if there is a message, then it will be output accordingly.