我正在尝试在cakephp中添加图像服务器端验证但是它们无法正常工作任何人都可以帮助我...我是cakephp的新手...这里是我的视图和模型文件代码: - 我正在使用自定义插件名称"帐户" Account.php
class Account extends AccountAppModel {
public $useTable = 'users';
public $name = "Account";
public $validate = array(
'name' => array(
'rule1' => array(
'rule' => 'notEmpty',
'message' => 'Please enter your name!'
),
'alphaNumeric' => array(
'rule' => 'alphaNumeric',
'message' => 'Name must be alphabetic or number!'
),
'between' => array(
'rule' => array('between', 3, 20),
'on' => 'create',
'message' => 'Name must be between 5to20 character!'
),
'isUnique' => array(
'rule' => 'isUnique',
'on' => 'create',
'message' => 'Name already taken! please choose another-one!'
)
),
'email'=>array(
'notEmpty'=>array(
'rule'=>'notEmpty',
'message'=>'Please enter Mail ID!'
),
'email'=>array(
'rule'=>'email',
'message'=>'Please enter a valid mail id!'
)
),
'region'=>array(
'rule'=>'notEmpty',
'message'=>'Please enter Region!'
),
'city'=>array(
'rule'=>'notEmpty',
'message'=>'Please enter City!'
),
'password'=>array(
'notEmpty'=>array(
'rule'=>'notEmpty',
'message'=>'please enter password'
),
'between'=>array(
'rule'=>array('between', 5,20),
'message'=>'password must be between 5 to 20 character'
)
),
'cpassword'=>array(
'compare' => array(
'rule'=>array('confirmPassword'),
'message' => 'Password do not match please try again!'
)
),
'photo'=>array(
'mimeType'=>array(
'rule'=>array('mimeType',"photo","photo"),
'message'=>'Please enter a valid image!'
),
'fileSize'=>array(
'rule' => array('fileSize', '<=', '1MB'),
'message'=>'Image is too big please try again!'
),
'uploadError'=>array(
'rule'=>'uploadError',
'message'=>'File upload field!'
)
),
'photo_id'=>array(
'mimeType'=>array(
'rule'=>array('mimeType','photo_id','photo_id'),
'message'=>'Please enter a valid image!'
),
'fileSize'=>array(
'rule'=>array('fileSize','<=','1MB'),
'message'=>'Image is too big please try again!'
),
'uploadError'=>array(
'rule'=>'uploadError',
'message'=>'File upload field!'
)
)
);
function confirmPassword()
{
return $this->data['Account']['password']==$this->data['Account']['cpassword'];
}
public function mimeType($field = null,$cfield = null){
//pr($cfield) or die;
if(isset($field[$cfield]["tmp_name"]) && !empty($field[$cfield]["tmp_name"]))
{
$size = @getimagesize($field[$cfield]["tmp_name"]);
//pr($size) or die;
$imgType = array('image/jpeg','image/jpg');
if(in_array($size['mime'],$imgType)){
$return = true;
}
else{
$return = false;
}
return $return;
}
}
}
这里是add.ctp文件代码: -
<?php echo $this->Form->create('Account',array('type'=>"file")); ?>
<table width='800px' border='0' align='center'>
<tr>
<td>Name :</td>
<td>
<?php
echo $this->Form->input('name',array('label'=>false,'data-rule-required'=>true,'lettersonly'=>true,"required"=>false));
?>
</td>
<td>E-Mail :</td>
<td>
<?php
echo $this->Form->input('email',
array('label'=>false,'required'=>false,'type'=>'text'));
?>
</td>
</tr>
<tr>
<td>Region :</td>
<td>
<?php
echo $this->Form->input('region',
array('type'=>'select',
'empty'=>'Select Region',
'options'=>$strlt,
'label'=>false,
'id'=>'name',
'data-rule-required'=>true,'required'=>false));
?>
</td>
<td>City :</td>
<td>
<?php
echo $this->Form->input('city',
array('type'=>'select',
'empty'=>'Select City',
'options'=>'',
'label'=>false,
'id'=>'slct',
'data-rule-required'=>true,'required'=>false
)
);
?>
</td>
</tr>
<tr>
<td>Password :</td>
<td>
<?php
echo $this->Form->input('password',
array('type'=>'password',
'label'=>false,
'data-rule-required'=>true,
'id'=>'pass_id',
'required'=>false));
?>
</td>
<td>Confirm Password :</td>
<td>
<?php
echo $this->Form->input('cpassword',
array('type'=>'password',
'label'=>false,
'data-rule-required'=>true,
'equalTo'=>'pass_id','required'=>false));
?>
</td>
</tr>
<tr>
<td>Address</td>
<td>
<?php
echo $this->Form->input('address',
array('label'=>false,
'data-rule-required'=>true,'required'=>false));
?>
</td>
<td>Zip-Code :</td>
<td>
<?php
echo $this->Form->input('zip',
array('label'=>false,
'data-rule-required'=>true,'required'=>false));
?>
</td>
</tr>
<tr>
<td>About Me :</td>
<td>
<?php
echo $this->Form->textarea('about_me',
array('label'=>false,
'data-rule-required'=>true,'required'=>false));
?>
</td>
<td>Contact No :</td>
<td>
<?php
echo $this->Form->input('contact_no',
array('label'=>false,
'data-rule-required'=>true,'required'=>false));
?>
</td>
</tr>
<tr>
<td>Upload Photo :</td>
<td>
<?php
echo $this->Form->input('photo',
array('type'=>'file',
'label'=>false,
'data-rule-required'=>true,'required'=>false));
?>
</td>
<td>Photo ID :</td>
<td>
<?php
echo $this->Form->input('photo_id',
array('type'=>'file',
'label'=>false,
'data-rule-required'=>true,'required'=>false));
?>
</td>
</tr>
<tr>
<td>Show Info :</td>
<td>
<?php
echo $this->Form->input('show_info',
array('type'=>'checkbox',
'label'=>false));
?>
</td>
<td>Premium Member :</td>
<td>
<?php
echo $this->Form->input('membership',
array('type'=>'checkbox',
'label'=>false,'required'=>false));
?>
</td>
</tr>
<tr>
<td>Unit / Company :</td>
<td>
<?php
echo $this->Form->input('user_type', array(
'type' => 'select',
'options' => array('unit' => 'Unit', 'company' => 'Company'),
'selected' => 'Unit',
'label'=>false,
'data-rule-required'=>true,'required'=>false
));
?>
</td>
<td>Status :</td>
<td>
<?php
echo $this->Form->input('status',
array('type'=>'select',
'options'=>array('1'=>'Active',
'0'=>'Inactive'),
'label'=>false,
'data-rule-required'=>true,'required'=>false));
?>
</td>
</tr>
<tr><td colspan=2>
<?php echo $this->Form->input('submit',
array('type'=>'submit',
'label'=>false,'class'=>'btn btn-default','id'=>'sb'));
?>
<?php
echo $this->Html->link('Cancel',
array('plugin'=>'account',
'controller'=>'accounts',
'action'=>'index'),
array('class'=>'btn btn-primary',
'div'=>false,
'escape'=>false,
'id'=>'sb'
)
);
?>
<td><tr>
</table>
<?php echo $this->Form->end();?>
答案 0 :(得分:1)
使用规则,(用此替换图像规则)
'photo_id'=>array(
'mimeType'=>array(
'rule' => array('mimeType', array('image/gif', 'image/png', 'image/jpg', 'image/jpeg')),
'message'=>'Please enter a valid image!'
),