電子郵件驗證不起作用,如果我將該字段的名稱僅作爲電子郵件發送,那麼它將採用電子郵件,但也不完美驗證,如'a @ a'正在工作,如果我將其作爲電子郵件,否則只有非空僅工作。電子郵件Validaion不在CakePHP 3.0中工作
RecommendTable.php
<?php
namespace App\Model\Table;
use App\Model\Entity\recommend;
use Cake\ORM\Query;
use Cake\ORM\RulesChecker;
use Cake\Validation\Validator;
class RecommendTable extends Table
{
public function initialize(array $config)
{
parent::initialize($config);
$this->table('recommend');
$this->displayField('id');
$this->primaryKey('id');
$this->addBehavior('Timestamp');
}
public function validationDefault(Validator $validator)
{
$validator = new Validator();
$validator
->requirePresence('name')
->notEmpty('name', 'Please fill this field')
->add('name', [
'length' => [
'rule' => ['minLength', 10],
'message' => 'Titles need to be at least 10 characters long',
]
]);
$validator->add("emai", "validFormat", [
"rule" => ["email"],
"message" => "Email must be valid."
]);
$validator
->requirePresence('yemail')
->notEmpty('yemail', 'Please fill this field..')
->add('yemail', ['length' => ['rule' => ['minLength', 10],'message' => 'Titles need to be at least 10 characters long',]]);
return $validator;
}
public function buildRules(RulesChecker $rules)
{
$rules->add($rules->isUnique(['email']));
return $rules;
}
}
Recommend.php
<?php
namespace App\Model\Entity;
use Cake\Auth\DefaultPasswordHasher;
use Cake\ORM\Entity;
class Recommend extends Entity
{
protected $_accessible = [
'*' => true,
'id' => false,
];
protected function _setPassword($value)
{
$hasher = new DefaultPasswordHasher();
return $hasher->hash($value);
}
}
Index.ctp
<?= $this->Form->create($temp) ?>
<div class="row">
<div class="container">
<div class="comment-form">
<div class="row">
<div>
<h2>
<center>
Recommend us to your Friends/Library
</center>
</h2>
</div><fieldset>
<div class="col-md-12 col-sm-12">
<div class="input-container">
<?php
echo $this->Form->input('emai',array('id'=>'emai','label'=>'To (Receiver’s mail-ID)',"placeholder"=>"Send E-mail to multiple (seperated by commas).")) ?>
</div>
</div>
<div class="col-md-6 col-sm-3">
<div class="input-container">
<?php
echo $this->Form->input('name',array('id'=>'name','label'=>'Name',"placeholder"=>"Name")); ?>
</div>
</div>
<div class="col-md-6 col-sm-3">
<div class="input-container">
<?php
echo $this->Form->input('yemail',array('id'=>'yemail','label'=>'From',"placeholder"=>"From")); ?>
</div>
</div>
<div class="col-md-12 col-sm-12">
<div class="input-container">
<label>Message</label>
<textarea name="msg" id="msg" style="resize: none;text-align:justify; " disabled placeholder="Hello"></textarea>
</div>
</div>
</fieldset>
<div class="col-md-12 col-sm-12">
<div class="input-container">
<?= $this->Form->button(__('Submit')) ?>
<button type="Reset">Reset</button>
<?= $this->Form->end() ?></div>
推介控制器
<?php
namespace App\Controller;
use Cake\ORM\TableRegistry;
use App\Controller\AppController;
use Cake\Mailer\Email;
use Cake\ORM\Table;
use App\Model\Tabel\RecommendTabel;
use Cake\Event\Event;
class RecommendController extends AppController
{
public function index()
{
$temp = $this->Recommend->newEntity();
if ($this->request->is('post')) {
$temp = $this->Recommend->patchEntity($temp, $this->request->data);
if($temp) {
$name=$this->request->data('name');
$receiver_email=$this->request->data('emai');
$Subject_Title='Temp';
$Sender_email=$this->request->data('yemail');
$email = new Email();
$email->template('invite', 'default')
->viewVars(['value' => $name])
->emailFormat('html')
->from($Sender_email)
->to($receiver_email)
->subject($Subject_Title)
->send();
$this->Flash->success(__('The user has been saved.'));
return $this->redirect(['action' => 'add']);
} else {
$this->Flash->error(__('The user could not be saved. Please, try again.'));
}
}
$this->set(compact('temp'));
$this->set('_serialize', ['temp']);
}