问题很简单,但为什么它对我来说很复杂。我也写过数据验证。但是对于空字段,它也接受输入。请检查我的action.php中是否有错误。
型号/ action.php的
func prepareChangePasswordField() {
enterPasswordText.placeholder = "Password"
enterPasswordText.detailColor = Color.red
enterPasswordText.delegate = self
enterPasswordText.placeholderNormalColor = UIColor.lightGray
enterPasswordText.placeholderActiveColor = Color.blue.base
enterPasswordText.dividerNormalColor = Color.grey.base
enterPasswordText.dividerActiveColor = Color.blue.base
enterPasswordText.isVisibilityIconButtonEnabled = true
enterPasswordText.visibilityIconButton?.tintColor = Color.grey.base.withAlphaComponent(passwordText.isSecureTextEntry ? 0.2 : 1.0)
}
func prepareConfirmPasswordField() {
confirmPasswordText.placeholder = "Confirm password"
confirmPasswordText.detailColor = Color.red
confirmPasswordText.delegate = self
confirmPasswordText.placeholderNormalColor = UIColor.lightGray
confirmPasswordText.placeholderActiveColor = Color.blue.base
confirmPasswordText.dividerNormalColor = Color.grey.base
confirmPasswordText.dividerActiveColor = Color.blue.base
confirmPasswordText.isVisibilityIconButtonEnabled = true
confirmPasswordText.visibilityIconButton?.tintColor = Color.grey.base.withAlphaComponent(passwordText.isSecureTextEntry ? 0.2 : 1.0)
//self.view.addSubview(userNameText)
//view.layout(emailTextField).center(offsetY: -passwordField.height - 60).left(20).right(20)
}
查看/操作/ index.ctp
<?php
App::uses('AppModel', 'Model');
class Actions extends AppModel {
public $validate = array(
'value_to_plot' => array(
'required'=>true,
'message' => 'atleast select one measure'
),
'column_name' => array(
'required'=>true,
'rule'=>array('notBlank'),
'message' => 'atleast select one table'
)
);
}
?>
控制器/ ActionsController.php
<div align="center">
<fieldset>
<?php echo $this->Form->create('valueToSend',array('type' => 'get'));?>
<?php if(isset($_GET['table_name'])){ ?>
<table class="table table-bordered table-hover table-striped">
<?php echo $this->Form->hidden('table_name', array('hiddenField' => true, 'value'=> $_GET['table_name'],'id'=>'table_name'));
echo $this->Form->hidden('chart', array('hiddenField' => true, 'value'=> "column3d",'id'=>"chart"));
?>
<tr>
<th>MEASURES</th>
<td>
<?php echo $this->Form->select('value_to_plot',$measures1,array('class'=>'form-control','id'=>'measures','required'=>true),['empty' => 'choose one']);?>
</td>
</tr>
<tr>
<th>DIMENSIONS</th>
<td>
<?php echo $this->Form->select('column_name[]',$measures2,array('multiple'=>'true','class'=>'form-control','id'=>'dimensions','required'=>true),['empty' =>'choose one']);?>
</td>
</tr>
</table>
<div style="text-align:center">
<?php echo $this->Form->end('submit'); ?>
</div>
<?php } ?>
</fieldset>
</div>
答案 0 :(得分:1)
这将验证并保存您的数据
if ($this->request->is('post')) {
$this->Actions->set($this->request->data);
if ($this->Actions->save($this->request->data)) {
// Set a session flash message and redirect.
}
}
答案 1 :(得分:0)
我认为你混淆了''required'=&gt; true“和''allowEmpty'=&gt; true”。前者意味着无法保存记录而不将该字段包含在更新的字段列表中,无论它是否包含实际数据。后者,我认为是你真正的意思,允许一个字段为空,虽然它不会强制声明。