Question

I am upgrading CakePhp 1.1 to 1.2 (and beyond) ... finally.

I'm having problems with form validation. I understood from the documentation that I found that $html->tagErrorMsg is deprecated, and needs to be changed to $form->error .

I did this in all locations, but the errors do not display. They work fine in my 1.1 version.

Here's the code from the .ctp

.ctp USED TO BE:

<div class="column span-5">
<?php echo $html->input('Account/firstname', array('size' => 20, 'class'=>'span-4 first last txt')); ?>
</div>
<div class="column span-3 last"><span class="my_error"><?php echo $html->tagErrorMsg('Account/firstname', 'Please enter a first name.');?></span></div>
</div>

.ctp IS NOW:

<div class="column span-5">
<?php echo $form->input('Account/firstname', array('size' => 20, 'class'=>'span-4 first last txt')); ?>
</div>
<div class="column span-3 last"><span class="my_error"><?php echo $form->error('Account/firstname', 'Please enter a first name.');?></span></div>
</div>

IN THE MODEL (account.php): I changed it FROM:

 var $validate = array(
          'firstname' => VALID_NOT_EMPTY,
    );

TO:

   var $validate = array(
      'firstname' => 'notEmpty',
);

What am I doing wrong? Would you please include an example of correct form validation in 1.2 and beyond?

Was it helpful?

Solution

I figured it out. There are a few more convention changes between 1.1 and 1.2

In the controller, I had to add:

                $this->Account->set($this->data);
            if ($this->Account->validates()) {
                // validated logic
            } else {
                // didn't validate logic
                $errors = $this->Account->validationErrors;
            }

However, that didn't quite do it either. I also had to remove the "Account/" reference in the ctp file.

Here is the correct ctp:

    <div class="column span-5">
<?php echo $form->input('firstname', array('size' => 20, 'class'=>'span-4 first last txt')); ?>
</div>
<div class="column span-3 last"><span class="my_error"><?php echo $form->error('firstname', 'Please enter a first name.');?></span></div>
</div>

It turns out that my controllers were also not named using the plural convention. So I had to add that also in order to get my $form->create() to work correctly.

 <?php echo $form->create('Account', array('action' => 'register')); ?>

It goes at the beginning of the form in the ctp instead of

<form action="<?php echo $html->url('/account/register/'); ?>" method="post">
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top