gpt4 book ai didi

php - Cakephp 数据校验 "notEmpty" "required"

转载 作者:行者123 更新时间:2023-11-29 06:52:07 26 4
gpt4 key购买 nike

我是蛋糕的新手,完成了博客教程,现在我正在尝试一些简单的开始。但是我有一些我无法理解的东西。在模型 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

这是我的 table :

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);

这是 View :

<!-- /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>

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

如果我还需要发布任何其他内容,请告诉我。

最佳答案

为了你的昵称

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

对于性别:

在 View 中

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.'
)
)

关于php - Cakephp 数据校验 "notEmpty" "required",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14706710/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com