0

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.

2
  • which controller is handling the add question action? the project controller or question controller? In your view (form) make sure the input names are prefixed with Model name 1.e. ModelName.field_name. Commented Jul 26, 2011 at 19:08
  • The Questions controller is handling the input. Commented Jul 26, 2011 at 19:19

3 Answers 3

1

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'));

Sign up to request clarification or add additional context in comments.

4 Comments

That's almost what I wanted, but the view is only showing the flash error. I wanted to show as it would in the questions/add view.
In your view $errors = $this->Session->read('errors'); now $errors contain all the validation errors try var_dump($errors); and see if you could find errors. once you have errors you can use $errors array to display all errors.
That helped. But then how can I show the errors below the input form? I was thinking, what if I try to insert into the array validationErrors the data? I would do that in the beforeFilter function.
I couldn't set the validationErros. :(
1

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.

2 Comments

The best way then is to create an add question function into my Projects Controller? It looks like I'm cheating, haha. I mean it seems as if I'm breaking the models encapsulation. I don't know. :(
yep, you are breaking the convention, because you are adding new question while in projects/view. No, they way I post above is to add new question in the view() in Projects Controller. So well, it's not the best way, but it's quick and should work fine. It can be done the "correct" way, but it would be more work. If you want, I can add it.
0

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');
    }
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.