validation

sample

code


target file(s):


controllers/validation_controller.php

1 <?php
2 class ValidationController extends AppController {
3
4 var $name = 'Validation';
5 var $uses = array('User');
6
7 function index() {
8 if (!empty($this->data)) {
9 $this->User->create();
10 if ($this->User->saveDummy($this->data)) {
11 $this->Session->setFlash(__('The user has been saved', true));
12 } else {
13 $this->Session->setFlash(__('The user could not be saved. Please, try again.', true));
14 }
15 }
16 if (empty($this->data)) {
17 $this->data = $this->User->readDummy();
18 }
19 }
20 }
21 ?>

views/validation/index.ctp

1 <?php
2 $params = array(
3 'sourceKey' => 'validation',
4 );
5 echo $this->element('pageLink', $params);
6 ?>
7
8 <hr>
9 <div class="tests form">
10 <?php echo $this->Form->create('User', array('url' => '/validation/'));?>
11 <fieldset>
12 <legend><?php __('User'); ?></legend>
13 <?php
14 echo $this->Form->input('name');
15 echo $this->Form->input('birthday');
16 echo $this->Form->input('is_invalid');
17 ?>
18 </fieldset>
19 <?php echo $this->Form->end(__('Submit', true));?>
20 </div>

models/user.php

1 <?php
2 class User extends AppModel {
3 var $name = 'User';
4 var $validate = array(
5 'name' => array(
6 'alphanumeric' => array(
7 'rule' => array('alphanumeric'),
8 'message' => 'アルファベットか数字で入力してください。',
9 ),
10 'maxlength' => array(
11 'rule' => array('maxlength', 10),
12 'message' => '10文字以内で入力してください',
13 ),
14 ),
15 'birthday' => array(
16 'rule' => 'date',
17 'message' => '正しい日付で入力してください。',
18 'allowEmpty' => true,
19 ),
20 'is_invalid' => array(
21 'rule' => array('boolean'),
22 'message' => 'is_invalidの値が無効です。',
23 ),
24 );
25 var $dummyId = 1;
26
27 function getDummyId() {
28 return $this->dummyId;
29 }
30
31 function readDummy() {
32 return $this->read(null, $this->dummyId);
33 }
34
35 function saveDummy($data) {
36 $data['User']['id'] = $this->dummyId;
37 return $this->save($data);
38 }
39 }
40 ?>