Here's what I finally did, and it is working now:
function search() {
if (empty($this->data)) {
// just render the view
} else {
$this->Book->set($this->data); // sends $data to the model for
validation purposes.
if ($this->Book->validates()) {
$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']
)
)
)
);
} else {
$this->Session->setFlash('Your search contains wrong data,
please check');
$this->redirect(array('action' => 'search'));
}
Now, how would it be possible for me to provide the user with the
validation feedback, such as showing in the view the messages upon
validation failure?
On Feb 5, 3:05 pm, "[email protected]" <[email protected]> wrote:
> Could you please provide a little more detail on how to do this?
>
> Specifically, what do you mean by "just pass $this->data to be
> checked"
>
> Sorry for the newbie question.
>
> On Jan 25, 1:28 pm, brian <[email protected]> wrote:
>
> > 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, [email protected]
>
> > <[email protected]> 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 [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---