Validate form from another model
I have a project model which is related to a question model and a answer model. In project/view/ I added a form to insert a new question, and it works fine. But if I send the form with an error, it validates inside /question/add. I want those validation errors to show at the project/view/ page. How can I do that?
Thanks!
CODE:
function add() {
if (!empty($this->data)) {
$this->Question->create();
if ($this->Question->save($this->data)) {
$this->Session->setFlash(__('The question has been saved', true));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The question could not be saved. Please, try again.', true));
}
}
}
开发者_运维技巧
THE FORM:
<?php echo $this->Form->create('Question', array('action' => 'add'));?>
<fieldset>
<legend><?php __('Add Question'); ?></legend>
<?php
echo $this->Form->input('text');
echo $this->Form->hidden('Project', array('value' => $project['Project']['id']));
?>
</fieldset>
<?php echo $this->Form->end(__('Submit', true));?>
Both of them are similar, since they were baked by Cake.
As far as i understood you want to show validation errors on view page instead of add view.
if ($this->Question->save($this->data)) {
$this->Session->setFlash(__('The question has been saved', true));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The question could not be saved. Please, try again.', true));
$this->Session->write('errors', $this->Question->validationErrors); //or you could use $this->Question->invalidFields()
$this->redirect(array('controller' => 'projects', 'action' => 'view', $this->data['Project']['id']));
}
Now in view echo($this->Session->read('errors'));
Edit: ok, to ensure proper model separation: because you want to show the validation errors on projects/view page, you still need to post the data to projects/view/$id (otherwise you'll have to deal with redirecting to referrer). You can write a method addQuestionForProject($data) in Question model, move the saving code there, and call that method in projects/view controller code.
<?php echo $this->Form->create('Question', array('controller'=>'projects','action' => 'view',{your project id here}));?>
projects controller
function view($id=null) { if(!$id)$this->redirect(array('action' => 'index')); if (!empty($this->data)) { if ($this->Project->Question->addQuestionForProject($this->data)) { $this->Session->setFlash(__('The question has been saved', true)); } else { $this->Session->setFlash(__('The question could not be saved. Please, try again.', true)); } } // read your project record here }
I'm not sure if cake can detect the validation errors automatically in this case (it probably can); but if not, you can pass the errors back from addQuestionForProject and display it yourself.
Another way is using ajax call, so you can send the request directly to questions/add and return the errors array (in xml, json, or just plain html), but you'll have to display the errors yourself.
I just did what Ehtesham suggested and it worked!
In questions_controllers.php:
function add() {
if (!empty($this->data)) {
$this->Question->create();
if ($this->Question->save($this->data)) {
$this->Session->setFlash(__('The question has been saved', true));
$this->redirect($this->referer());
} else {
$this->Session->setFlash(__('The question could not be saved. Please, try again.', true));
$this->Session->write('question_error', $this->Question->invalidFields());
$this->redirect($this->referer());
}
}
}
projects_controller.php:
function beforeFilter () {
if ($this->Session->check('question_error')) {
$this->Project->Question->validationErrors = $this->Session->read('question_error');
$this->Session->delete('question_error');
}
}
精彩评论