电子邮件验证在Cakephp 3.0中不起作用

时间:2016-11-13 04:39:21

标签: php validation cakephp cakephp-3.0

电子邮件验证不起作用,如果我将该字段的名称仅作为电子邮件,那么它需要的是电子邮件,但也不是完美的验证,例如@ a'如果我将其作为电子邮件使用,否则只有非空的工作。

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

    }

2 个答案:

答案 0 :(得分:2)

patchEntity函数返回修补实体。因此,当在布尔上下文中使用时,此行的结果将始终计算为true

$temp = $this->Recommend->patchEntity($temp, $this->request->data);

因此,要检查是否检测到任何错误,您的if语句不能仅仅将返回值与true进行比较,而是执行以下操作:

if (!$temp->errors()) {

答案 1 :(得分:-2)

在defult验证库中提供Cakephp,请在(http://book.cakephp.org/2.0/en/models/data-validation.html)中找到此验证。请尝试使用此代码。

public $validate = array('email' => array('rule' => 'email'));

public $validate = array(
    'email' => array(
        'rule' => array('email', true),
        'message' => 'Please supply a valid email address.'
    )
);