duanan5940 2016-04-23 10:29
浏览 21

cakephp在验证错误后保留数据

user data has been lost if there is a validation error and error validation messages are also not showing on front end.

Here is my controller

    public function add() {
        $this->theme = 'Front';
        if (!empty($this->logged_in)) {
            return $this->redirect(Router::url('/', true) . $this->logged_in['Role']['alias']);
        }
        $this->set('title_for_layout', __('Create An Account'));

// hanlde post data
        if ($this->request->is("post")) {
            if (!empty($this->request->data)) {
                $this->User->set($this->request->data);
                if (!$this->User->validates()) {
                    $this->User->validationErrors;
                    $this->Session->setFlash(__('The Information could not be saved. Please, try again.'), 'message', array('class' => 'danger'));
                    $this->redirect(Router::url('/', true) . 'sign-up/');
                }
            }

//                   die('sdsddsaf');
            // if (!empty($this->request->data['User']['role_id']) && $this->request->data['User']['role_id'] == 2) {
            unset($this->request->data['User']['specialization_id']);
            $randomSt = $this->Custom->randomString(28);
            $this->request->data['User']['activation_key'] = $randomSt;
            // }


            if ($this->User->save($this->request->data)) {
                $user = $this->request->data;
                if (!empty($this->request->data['User']['role_id']) && $this->request->data['User']['role_id'] == 3) {
                    $this->Session->setFlash(__('Thank you for registering with us. Please check your email inbox/junk.'), 'message', array('class' => 'successs'));
// set mail variable
                    $to = $this->request->data['User']['email'];
                    $fname = $this->request->data['User']['first_name'];
                    $lname = $this->request->data['User']['last_name'];
//$activelink = SITEURL . 'activate?code=' . $randomSt;

                    $siteurl = Configure::read('Site.title');

                    $replace = array($fname, $lname, $siteurl);
// Send mail on Registration
                    $this->sendMail($to, 'RegisterDoctor', $replace);
                } else {
                    $this->Session->setFlash(__('Thank you for registering with us. Please check your e-mail inbox/junk as your e-mail confirmation has just been sent.'), 'message', array('class' => 'successs'));
                    $activelink = Router::url('/', true) . 'activate/' . $this->User->id . '/' . $randomSt;
// set mail variable
                    $to = $this->request->data['User']['email'];
                    $fname = $this->request->data['User']['first_name'];
                    $lname = $this->request->data['User']['last_name'];
//$activelink = SITEURL . 'activate?code=' . $randomSt;

                    $siteurl = Configure::read('Site.title');

                    $replace = array($fname, $lname, $activelink, $siteurl);
// Send mail on Registration
                    $this->sendMail($to, 'Register', $replace);
                }


                $this->redirect(array('controller' => 'users', 'action' => 'login'));
            }
        }

        $reponse = array();

        $roles = $this->User->rolesSignup();
        $genders = $this->User->genders();
        $specializations = $this->User->specializations();
        $this->set(compact('roles', 'genders', 'specializations'));
    }

And below is my view

 <?php
                echo $this->Form->create('User', array(
                    'inputDefaults' => array('div' => false, 'label' => false, 'fieldset' => false),
                    'class' => 'ac p10',
                    // 'data-togglesd' => "validator",
                    'role' => 'form',
                    'type' => 'file',
                    'onSubmit' => 'return checksubmitPass()'
                        )
                );
                ?>
                <div class="ph10">
                    <div class="btn-group radio_group w100" data-toggle="buttons">
                        <label class="btn btn-primary col-md-6 <?php echo (empty($this->request->data['User']['role_id']) || @$this->request->data['User']['role_id'] == 2 ) ? 'active' : ""; ?> btn_roles">
                            <input type="radio" name="data[User][role_id]"  value="2" class="role_id" <?php echo (empty($this->request->data['User']['role_id']) || @$this->request->data['User']['role_id'] == 2 ) ? 'checked="checked"' : ""; ?>> User
                        </label>
                        <label class="btn btn-primary col-md-6 <?php echo (!empty($this->request->data['User']['role_id']) && $this->request->data['User']['role_id'] == 3 ) ? 'active' : ""; ?> btn_roles">
                            <input type="radio" name="data[User][role_id]"  value="3" class="role_id" <?php echo (!empty($this->request->data['User']['role_id']) && $this->request->data['User']['role_id'] == 3 ) ? 'checked="checked"' : ""; ?>> Doctor
                        </label>

                    </div>
                </div>
                <div class="form-group p10 cr">
                    <div class="cm upload_img round"><img src="<?php echo $this->webroot; ?>img/user_avtar(upload).png" class="user_pic_upload">
                        <?php echo $this->Form->file('image', array('class' => 'user_img', 'data-fv-file' => 'true', 'data-fv-notempty-message' => __('notEmpty'), 'data-fv-field' => 'data[User][image]')); ?>

                    </div>
                    <small style="display: none;" class="help-block" data-fv-validator="notEmpty" data-fv-for="data[User][image]" data-fv-result="VALID">This field cannot be left blank.</small>
                </div>
                <div class="form-group mt10 cr">
                    <div class="clearfix">
                        <div class="col-lg-6 first_name"><?php echo $this->Form->input('first_name', array('class' => 'form-control', 'placeholder' => 'First Name', 'data-stripe' => "alphabet", 'data-fv-notempty-message' => __('notEmpty'), 'data-fv-regexp' => 'true', 'data-fv-regexp-regexp' => "^[a-zA-Z\s]+$", 'data-fv-regexp-message' => "The first name can consist of alphabetical characters and spaces only")); ?></div>
                        <div class="col-lg-6 last_name"><?php echo $this->Form->input('last_name', array('class' => 'form-control', 'placeholder' => 'Last Name', 'data-fv-notempty-message' => __('notEmpty'), 'data-fv-regexp' => 'true', 'data-fv-regexp-regexp' => "^[a-zA-Z\s]+$", 'data-fv-regexp-message' => "The last name can consist of alphabetical characters and spaces only", 'required')); ?></div>
                    </div>
                </div>
                <div class="form-group ph10 mt10">
                    <?php echo $this->Form->input('email', array('class' => "form-control", 'placeholder' => 'Email Address', 'data-fv-notempty-message' => __('notEmpty'), 'data-fv-emailaddress-message' => __('validemail'))); ?>
                </div>
                <div class="form-group mt10 ph10 row_org">
                    <div id="dropdown-menu">
                        <?php echo $this->Form->input('specialization_id', array('empty' => 'Select Speciallization', 'class' => 'selectpicker form-control', 'data-fv-notempty-message' => __('notEmpty'), 'required')); ?>                                   
                    </div>
                </div>
                <div class="form-group ph10 mt10">
                    <?php echo $this->Form->input('password', array('class' => "form-control", 'placeholder' => 'Password', 'type' => 'password', 'data-fv-notempty-message' => __('notEmpty'))); ?>
                </div>
                <span id="result"></span>
                <div class="form-group ph10 mt10">
                    <?php
                    echo $this->Form->input('verify_password', array(
                        'class' => "form-control",
                        'type' => 'password',
                        'placeholder' => 'Confirm Password',
                        'data-fv-notempty-message' => __('notEmpty'),
                        'data-fv-identical' => "true",
                        'data-fv-identical-field' => "data[User][password]",
                        'data-fv-identical-message' => __('passwordNotMatch')
                            )
                    );
                    ?>
                </div>
                <div class="form-group mt10">
                    <div class="clearfix">
                        <div class="col-lg-6 mobile-custom">
                            <?php echo $this->Form->input('mobile', array('class' => 'form-control phonenumber', 'placeholder' => 'Mobile number', 'maxlength' => '14', 'minlength' => '10', 'data-fv-stringlength-message' => 'The mobile number must be 10 to 14 characters long', 'data-fv-numeric-message' => __('Please enter valid mobile numbers'), 'data-fv-numeric' => "true")); ?>

                        </div>
                        <div class="col-lg-6" id="dropdown-menu">
                            <?php //echo $this->Form->input('gender', array('class' => 'form-control selectpicker', 'data-fv-notempty-message' => __('notEmpty'),'empty' => 'I Am','error' => false, 'required')); ?>
                            <select id="UserGender" title="I Am" required="required" data-fv-notempty-message="This field cannot be left blank." class="form-control" name="data[User][gender]" style="display: none;" data-fv-field="data[User][gender]">

                                <option value="Male">Male</option>
                                <option value="Female">Female</option>
                                <option value="Other">Other</option>
                            </select>
                        </div>
                    </div>
                </div>
                <div class="form-group ph10 mt10"><button type="submit" class="btn btn-info w100">Sign Up</button></div>
                <p class="mt10"><small>By Signing up you are agree with <br><a href="/terms">Terms &amp; Conditions</a> and <a href="/privacy-policy">Privacy Policy</a></small></p>
                <p class="mt10 signup-text"><strong>Already on HealthDrop?</strong></p>
                <div class="ph10">
                    <?php echo $this->Html->link('Sign In', Router::url('/', true) . 'login', array('class' => 'btn deep btn-primary w100')); ?>
                </div>
                <?php echo $this->Form->end(); ?>

it was working before bu suddenly stop to showing error and start loosing data. I have check with this question but it is pointing out at field names in input and I have correct ones. Any help appreciate.

  • 写回答

2条回答 默认 最新

  • doxzrb8721 2016-04-23 10:49
    关注

    You lost your data because there is redirect to sign-up url.

    $this->redirect(Router::url('/', true) . 'sign-up/');
    

    And after redirection

    $this->request->data
    

    is empty because you perform new request, the same with errors

    评论

报告相同问题?

悬赏问题

  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错