Cakephp validation from both the model and the controller

I gave validation to the model. In addition, I gave some validation in the controller to check the captcha code. After that, the application does not display model validation errors. If I comment on the controller code, the model check works fine and displays errors. Both do not work.

Model Code (sample)

class User extends AppModel {

var $name = 'User';

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

}

Controller code

if (!empty($this->data)) {

$this->User->set($this->data);
$this->User->create();
$captcha = $this->Session->read('CAPTCHA_CODE');

if (strtolower($captcha) != strtolower($this->data['User']['captcha']))
{
    $this->User->invalidate('captcha', __('Sorry, your entry did not match', true));
}

if ($this->User->save($this->data, array('validate' => 'only')))
{
    if ($this->User->save($this->data, array('validate' => 'false'))) {
        $this->Session->setFlash(__('Registered successfully', true));
        $this->redirect('success');
    }
}else {
    $this->Session->setFlash(__('Please, try again.', true));
}

}

I replaced the string if ($this->User->save($this->data, array('validate' => 'only')))with if ($this->User->validates()), and then it doesn't work.

please, help

+5
source share
1 answer

if ( $this->User->validates() ). , , , Model::save() Model::validationErrors, . Model::validates() , .

, , CakePHP 1.2.

:

class User extends AppModel
{
    var $validate = array(
        'my_other_field' => array(
            'rule' => 'notEmpty',
            'message' => 'This field should not be empty.'
        )
    );
}

:

class UsersController extends AppModel
{
    function add()
    {
        if (! empty($this->data)) {
            $this->User->set( $this->data );

            if ( 'foo' != $this->data['User']['my_field'] ) {
                $this->User->invalidate( 'my_field', 'Should be "foo".' );
            }

            if ( $this->User->validates() ) {
                $this->flash('Form validated correctly.'); exit;
            }
        }
    }
}

:

<?php echo $form->create('User', array('action'=>'add')); ?> 
<?php echo $form->input('User.my_field', array('value'=>'bar')); ?> 
<?php echo $form->input('User.my_other_field', array('value'=>'')); ?> 
<?php echo $form->end('Submit'); ?> 

- , , , .

, , MVC, . , - :

:

class UsersController extends AppController
{
    function add()
    {
        if (! empty($this->data)) {

            $captcha = $this->Session->read('CAPTCHA_CODE');
            $this->User->setCaptchaCheck( $captcha );

            if ( $this->User->save( $this->data, array('validate'=>true))) {
                $this->Session->setFlash('Success');
                $this->redirect('success',303,true);
            }
        }
    }
}

:

class User extends AppModel
{
    var $captchaCheck = '';

    var $validates = array(
        'captcha' => array(
            'matchesCheck' => array(
                'rule' => array( 'matchesCaptchaCheck', 'captchaCheck' ), // second value of array should match class member-name above
                'message' => "CAPTCHAs don't match."
            )
        )
    );

    function matchesCaptchaCheck( $data, $checkVar )
    {
        $data = reset(array_values($data)); // I don't need to know the field name now.

        return low($data) == low($this->{$checkVar});
    }

    function setCaptchaCheck( $captcha )
    {
        $this->captchaCheck = $captcha;
    }
}

, , , ; .

, .

+11

All Articles