Sunday, January 25, 2009

Re: How to call validation in a form that doesn't use save()?

You'll need to validate the user's input before you call find(). If
you want, you can create methods in the model for that and just pass
$this->data to be checked. Have the method(s) check the values and
return true/false back to the controller.

On Sun, Jan 25, 2009 at 7:31 AM, libardoab@gmail.com
<libardoab@gmail.com> wrote:
>
> I am learning Cake with a test project.
>
> As far as I know, validation automatically occurs when the Model's save
> () method is called from within a controller. So, if I have defined
> validation rules in my model, those rules will be scrutinized before
> actually either saving or editing data.
>
> Now, what if the purpose of the form is not to save data?
>
> Example. What if I create a form whose function is to perform a
> search?
>
> In this case, I am only using the $this->data information in
> conjunction with the find() method, but I am not calling the save()
> function.
>
> So... my question, how could I link this search form with the
> validation rules initially created for the add() and edit() actions
> (which evidently make use of the save() function)?
>
> Here is my sample code:
>
> In my controller:
> function search() {
> if (empty($this->data)) {
> // just render the view
> } else {
> $results = $this->Book->find('all',
> array(
> 'conditions' => array(
> 'or' => array(
> 'Book.isbn' => $this->data['Book']['isbn'],
> 'Book.title' => $this->data['Book']['title'],
> 'Book.description' => $this->data['Book']
> ['description'],
> 'Book.author_name' => $this->data['Book']
> ['author_name'],
> 'Book.starred' => $this->data['Book']['starred']
> )
> )
> )
> );
> $this->set('data', $this->data);
> $this->set('results', $results);
> }
> }
>
>
> In my view (ctp file):
>
> <?php echo $form->create('Book', array('action'=>'search')); ?>
> <fieldset>
> <legend> Search Books </legend>
> <?php
> echo $form->input('isbn');
> echo $form->input('title');
> echo $form->input('description');
> echo $form->input('author_name');
> echo $form->input('starred');
> ?>
> <?php echo $form->end('Search'); ?>
> </fieldset>
>
> Thank you.
>
> >
>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to cake-php+unsubscribe@googlegroups.com
For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

No comments: