In my Patients 'view' page I replaced a div with 'doctors/add' using ajax. And now I can add a doctor in Patients view page. But if any validation error occured it goes directly from my Patients view page. to 'doctors/add' page showing validation messages.
I Want it stay on the current page showing validation messages appropriately on input fields of 'add'.
function add() {
if (!empty($this->data)) {
$this->Doctors->create();
if ($this->Doctors->save($this->data)) {
$this->Session->setFlash(__('The Doctor has been saved', true));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The Doctor could not be saved. Please, try again.', true));
}
}
}
Is this due to $this->Session->setFlash in add() ?
thanks...!
try this
$this->redirect($this->here);
Can you pls try this?
function add() {
if (!empty($this->data)) {
$this->Doctors->create();
if ($this->Doctors->save($this->data)) {
$this->Session->setFlash(__('The Doctor has been saved', true));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The Doctor could not be saved. Please, try again.', true));
$this->redirect(array('controller'=>'patient_controller_name', 'action' => 'view'));
}
}
}
Related
I am currently trying to update my user before sending an email which will have a confirm link with a resetkey for the user to reset the password.
The problem is that the user is not updating whilst the key is being generated.
the code can be seen hereunder
public function forgotpassword() {
if ($this->request->is('post')) {
$mail = $this->request->data['ContactDetail']['email'];
$data = $this->User->find('first', array('conditions' => array('ContactDetail.email' => $mail)));
if (!$data) {
$message = __('No Such E-mail address registerd with us ');
$this->Session->setFlash($message);
} else {
$data['User']['resetkey'] = Security::hash(mt_rand(),'md5',true);
debug($data);
if ($this->User->saveAssociated($data)) {
$this->Session->setFlash(__('The user has been saved.'));
}
else {
$this->Session->setFlash(__('The user could not be updated. Please, try again.'));
}
$key = $data['User']['resetkey'];
$id = $data['User']['id'];
$mail = $data['ContactDetail']['email'];
$email = new CakeEmail('default');
$email->to($mail);
$email->from("xxxx#yahoo.com");
$email->emailFormat('html');
$email->subject('Password reset instructions from');
$email->viewVars(array('key' => $key, 'id' => $id, 'rand' => mt_rand()));
$email->template('reset');
if ($email->send('reset')) {
$message = __('Please check your email for reset instructions.');
$this->Session->setFlash($message);
} else {
$message = __('Something went wrong with activation mail. Please try later.');
$this->Session->setFlash($message);
}
}
$this->redirect('/');
}
}
I checked the execution log of MySQL but all I am seeing are selects no updates or even inserts.
This is probably something very dumb but I can't understand why it doesn't say anything.
Why do you use saveAssociated method for update single field?
Something like this would be easier:
$this->User->updateAll(
array( 'User.resetkey' => $hash ),
array( 'User.id' => $userId )
);
try using
$this->User->saveMany($data, array('deep' => true));
When saving. Also
if (empty($data)) {...
Might be a safer way to go
This will work for you-
$this->User->id = $data['User']['id'];
if ($this->User->save($data)) {
$this->Session->setFlash(__('The user has been saved.'));
}else {
$this->Session->setFlash(__('The user could not be updated. Please, try again.'));
}
Try to debug like:
if ($this->User->saveAssociated($data)) {
$this->Session->setFlash(__('The user has been saved.'));
}
else {
$this->Session->setFlash(__('The user could not be updated. Please, try again.'));
}
$log=$this->User->getDataSource()->getLog(false, false);
echo "<pre>";print_r($log);exit;
Validations are not working with saveMany for all values.its only working for first value. I am using translate behavior.
My code:
public function admin_add() {
if ($this->request->is('post')) {
$this->FaqCategory->create();
$this->request->data['FaqCategory']['name'] = Inflector::slug($this->request->data['FaqCategory']['name'], $replacement = '-');
if ($this->FaqCategory->saveMany($this->request->data)) {
$this->Session->setFlash('The faq category has been saved', 'default', array('class' => 'success'));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The faq category could not be saved. Please, try again.'));
}
}
$languages = $this->Language->getlangs();
$this->set('langs', $languages);
}
Try this: $this->FaqCategory->saveMany($this->request->data, array('validate' => true));
I have a Horse object that has a completed variable (text). I want to be able to click set complete in the View and then have that horse's completed variable set to "yes." After this, we would redirect the user to tasks/index. My thinking is to create setcomplete($id) function in Controller, so I could pass /setcomplete/4 and that horse's flag would change.
I have created a basic view for it, but the idea is that I would not need a view, but I do not know how to get around that....I was thinking of using $this->render() or something of this nature.
This is the basic code...pass in the id and then change the variable, then redirect to tasks/index.
I am not getting an error....its just not working, that's all.
public function setcomplete($id = null) {
if (!$this->Task->exists($id)) {
throw new NotFoundException(__('Invalid task'));
}
if ($this->request->is(array('post', 'put'))) {
$this->set('completed', 'yes');
if ($this->Task->save($this->request->data)) {
$this->Session->setFlash(__('The task has been updated.'));
return $this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The task could not be saved. Please, try again.'));
}
} else {
}
}
According to my understanding of your problem. you want to set the completed to yes for Task model.
i think you've done in a wrong way here.
what about the $this->request->data here?
Does it contain the data you want to save for the task model like the structure as below
Array(
'Task' => Array(
'id' => 4, // for eg..
'completed' => 'yes'
)
)
if not
you can solve the problem by doing like this. [Assuming you don't have $this->request->data]
public function setcomplete($id = null) {
if (!$this->Task->exists($id)) {
throw new NotFoundException(__('Invalid task'));
}
$this->Task->id = $id;
$this->Task->set('completed', 'yes')
if ($this->Task->save()) {
$this->Session->setFlash(__('The task has been updated.'));
return $this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The task could not be saved. Please, try again.'));
}
}
I'm kind new to the cakephp and I want to know how to static the id where the id is on drop down or list. I have do hidden but its not enter the database. This is my coding:
This is in the controller
function add() {
if (!empty($this->data)) {
$this->Post->create();
if ($this->Post->save($this->data)) {
$this->Session->setFlash(__('The post has been saved', true));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The post could not be saved. Please, try again.', true));
}
}
$users = $this->Post->User->find('list');
$this->set(compact('users'));
$this->set('userid',$this->Auth->user('id'));
}
this coding is in the ctp
<?php
echo $this->Form->input('user_id');
?>
The best way is to not send the user id to the browser and back at all, since that opens the possibility of form tinkering and security breaches/invalid results. Just inject the user id into the data before saving:
function add() {
if (!empty($this->data)) {
$this->Post->create();
// setting user id
$this->data['Post']['user_id'] = $this->Auth->user('id');
if ($this->Post->save($this->data)) {
$this->Session->setFlash(__('The post has been saved', true));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The post could not be saved. Please, try again.', true));
}
}
$users = $this->Post->User->find('list');
$this->set(compact('users'));
}
I have two models
Rest (id, name .....)
Operating_hours(id, open, close, rest_id)
When I try to save a record from Restaurant add form. It saves only the open and the close time but not the reference id at rest_id.
$this->Restaurant->create();
if($this->Restaurant->saveAll($this->data, array('validate' => 'first'))) {
$this->Session->setFlash(__('The restaurant has been saved', true));
//$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The restaurant could not be saved. Please, try again.', true));
}
If you are doing an insert (since it is an "add"), it is unlikely that you can perform everything in one step, as MySQL would not know the id of your restaurant to save the opening hours. I'd suggest doing the following:
$this->Restaurant->create();
if($this->Restaurant->save($this->data, array('validate' => 'first'))) {
$this->data['rest_id'] = $this->Restaurant->getLastInsertId();
if($this->Restaurant->OperatingHours->save($this->data, array('validate' => 'first'))) {
$this->Session->setFlash(__('The restaurant has been saved', true));
//$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The restaurant opening hours could not be saved. Please, try again.', true));
}
} else {
$this->Session->setFlash(__('The restaurant could not be saved. Please, try again.', true));
}