Thursday, April 28, 2011

Re: Form Validation

Before accusing the manual of being crap, perhaps you should take some
time to actually read it?

How to implement validation in the model: http://book.cakephp.org/view/1143/Data-Validation
How to do validation from your controller:
http://book.cakephp.org/view/1182/Validating-Data-from-the-Controller
How to show validation message: On http://book.cakephp.org/view/1143/Data-Validation
it says "For more information about how to handle the displaying of
validation errors, check out the section covering FormHelper." in the
second paragraph. Reading through the section on the FormHelper,
you'll quickly discover how it works.

All this stuff in one place: http://book.cakephp.org/view/1538/Data-Validation

As for your immediate issue, if you had actually read the section in
the book about validation data from the controller, you'd know exactly
what to do. http://book.cakephp.org/view/1182/Validating-Data-from-the-Controller

I quote:
First, set the data to the model:
$this->ModelName->set( $this->data );
Then, to check if the data validates, use the validates method of the
model, which will return true if it validates and false if it doesn't:
if ($this->ModelName->validates()) { // it validated logic} else
{ // didn't validate logic}

On Apr 29, 4:31 am, Sanfly <san...@gmail.com> wrote:
> Hi
>
> My cake version is 1.3.7
>
> I'm just learning form validation and am having a few issues.
>
> Firstly, the manual is crap in that it doesn't tell you how to
> implement the validation in your model, and it doesn't tell you how to
> show the validation error messages in your view.  Took a lot of
> digging to find all that!
>
> Anyway, on the the problem at hand.  I thought I had this working on
> one field, went to implement for the rest of my fields and now it is
> always validating the form even when I have deliberately used inputs
> that should fail validation.  I have stripped it back down to one
> field to try and get it working again
>
> In my model: user.php
>
> // VALIDATION
>         var $validate = array(
>                         'first_name' => 'notEmpty'
>
>                 );
>
> In my controller:
>
> function add(){
>
>                 if($this->data){
>                         if ($this->User->validates($this->data)) {
>                                 echo "does validate";
>
>                         }
>                         else { //data doesn't validate
>                                 echo "doesnt validate<br>";
>                                 $this->Session->setFlash('Please correct errors below.<br><br>');
>                         }
>
>                 }
>         }
>
> Can anyone see an obvious error that I have overlooked?
>
> BTW, the 'does validate' and 'doesn't validate' are just in there for
> me to see easily where my problems are occuring.  Obviously wouldnt be
> there for production

--
Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions.


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

No comments: