简体   繁体   中英

Email Validation are not Working in Cakephp 3.0

Email validation is not working and if i take name of the field as email only then it take is email but also not perfect validation like 'a@a' is working if i take it as email otherwise only non-empty is working only.

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>

Recommend Controller

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

    }

The patchEntity function returns a patched entity. So, the result of this line will always evaluate to true when used in a boolean context.

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

So, to check whether any errors were detected, your if statement cannot just compare the returned value to true , but instead do something like this:

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

Cakephp is provide in defult validation library, please find this validation in (ie http://book.cakephp.org/2.0/en/models/data-validation.html ). Please try to use this code.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM