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' ),
'message' => "CAPTCHAs don't match."
)
)
);
function matchesCaptchaCheck( $data, $checkVar )
{
$data = reset(array_values($data));
return low($data) == low($this->{$checkVar});
}
function setCaptchaCheck( $captcha )
{
$this->captchaCheck = $captcha;
}
}
, , , ; .
, .