1

我是蛋糕的新手,完成了博客教程,现在我正在尝试一些简单的开始。但是我有一些我无法理解的东西。在模型 PersonalInformation 上,我得到了表字段的数据验证:

这是我的带有 $validate 数组的模型:

class PersonalInformation extends AppModel {

/**
 * Validation rules
 * @var $validate
 */
public $validate = array(
    "first_name" => array(
        "name"      => array(
            "rule"      => "validName",
            "message"   => 'Only alpha characters and "." (dots)',
        ),
    ),
    "last_name"  => array(
        "name"      => array(
            "rule"      => "validName",
            "message"   => 'Only alpha characters and "." (dots)',
        ),
    ),
    "birth_date" => array(
        "date"      => array(
            "rule"      => "date",
            "message"   => "Enter a valid date",
        ),
    ),
    "nickname"  => array(
        "alphanumeric"  => array(
            "rule"      => "alphaNumeric",
            "message"   => "Only alpha-numeric characters",
        ),
    ),
    "gender"     => array(
        "notEmpty"  => array(
            "rule"          => "notEmpty",
            "message"       => "Can't be empty",
        ),
    ),
);

} // end class

这是我的桌子:

CREATE TABLE `personal_informations`
(
`id` INT NOT NULL UNIQUE AUTO_INCREMENT,
`first_name` VARCHAR(45),
`last_name` VARCHAR(45),
`birth_date` DATE,
`nickname` VARCHAR(45),
`is_company` TINYINT DEFAULT 0 NOT NULL,
`picture_url` VARCHAR(255),
`gender` ENUM('m','f') NOT NULL,
`notes` TEXT,
`created` DATETIME,
`modified` DATETIME,
PRIMARY KEY (`id`)
) ENGINE=InnoDB CHARACTER SET=utf8;

CREATE INDEX `id_idx` ON personal_informations(id);

这是视图:

<!-- /PersonalInformations/add -->

<div class="">
<h2>Create a personal information</h2>
    <?php
    echo $this->Form->create();

    echo $this->Form->input( "PersonalInformation.first_name" );
    echo $this->Form->input( "PersonalInformation.last_name" );
    echo $this->Form->input( 
        "PersonalInformation.birth_date", 
        array( 
            "separator" => " - ", 
            "minYear" => date('Y') - 90,
            "maxYear" => date('Y')
    )
    );
    echo $this->Form->input( "PersonalInformation.nickname" );
    echo $this->Form->input( "PersonalInformation.is_company", array( "type" =>     "checkbox" ));
    echo $this->Form->input( "PersonalInformation.piture_url", array( "type" => "file"     ));
    echo $this->Form->radio( "PersonalInformation.gender", array( "m" => __("Male"), "f"     => __("Female")), array( "hiddenField" => false ));
    echo $this->Form->textarea( "PersonalInformation.notes" );

    echo $this->Form->end( "Done" );
    ?>
</div>

所以问题是,由于某种原因,“昵称”是必需的,我的意思是,当我点击“昵称”为空的提交按钮时,它显示“只有字母数字字符”。对于“性别”,当没有选择任何内容时,它不会显示任何内容。

如果还有什么我需要发布的,请告诉我。

4

3 回答 3

1

为了你的昵称

'nickname' => array(
            'required' => array(
                'rule' => array('notEmpty'),
                'message' => 'Nick name is required.'
            )
        )

对于性别:

在视图中

echo $this->Form->input('PersonalInformation.gender',array(
          'type'=>'select','empty' => '---- Select----',
          'class'=>'select',
          'options'=>$option_array
          ))

在模型验证数组中:

'gender' => array(
            'required' => array(
                'rule' => array('notEmpty'),
                'message' => 'Can\'t be empty.'
            )
        )
于 2013-02-05T11:53:32.623 回答
1

为昵称添加'allowEmpty' => true规则。

 "nickname"  => array(
        "alphanumeric"  => array(
            "allowEmpty"=> true,
            "rule"      => "alphaNumeric",
            "message"   => "Only alpha-numeric characters",
        ),
    ),
于 2013-02-05T14:53:30.437 回答
0

对于昵称,你试过用required => false吗?您可以在文档中阅读有关它的更多信息。

"nickname"  => array(
    "alphanumeric"  => array(
        "rule"      => "alphaNumeric",
        "required" => false,
        "message"   => "Only alpha-numeric characters",
    ),
),
于 2013-02-05T11:46:30.833 回答