简体   繁体   中英

$this->request->data is empty after submitting a form in CakePHP 2.x

I'm having some problems when trying to request data that has been sent from a form. When I check $this->request->data it says its an empty array, so I thought I'd try debug($this->request) and it shows that data is array(0) . I'm not sure why this is happening. I have provided my model-view-controller files.

Edit

After some further testing, I've figured out that my problem lies with the line

if(!empty($this->request->data))

in UsersController.php . im not sure why but the form data[Reference][] isn't reaching the controller. I have added snippets of the code below.

(Controller) UsersController.php:

public function send_invitation() {
    $user = $this->Auth->user();

    $this->layout = "dashboard";
    // debug($this->request);
    if (!empty($this->request->data)) {

         ...               

    } else { 
        echo 'there is no data';
    }

    $this->set("user", $user);
}

(View) send_invitation.php :

<div class="dashboard-container-right">
    <div class="dashboard-innerright">
        <div class="upper-dashboard"><h1> <?php echo __("Send Invitations"); ?> </h1></div>
        <div class="botm-forcolor">
            <div class="midd-dashord">
                <?php echo __("Your Referral Code is"); ?> <strong><?php echo $user["referral_code"];?> </strong>
                <div class="invite-content">
                    <!--- Instructor Login -->
                    <div class="tab-pane active" id="instructor">
                        <?php echo $this->Form->create('Reference' , array('id'=>'send_invites' , 'url' => array('controller' => 'users', 'action' => 'send_invitation' ) , 'novalidate'=>'novalidate')); ?>
                            <?php  $form=$this->Form;?>
                            <div class="padding_15tabs">
                                <div class="col-md-12  show-grid row">
                                    <?php $name=__("Name"); ?>
                                    <?php echo $form->input('Reference.name',array('class'=>'col-xs-12 col-sm-12 col-lg-12 text-style', 'placeholder'=>$name, 'div' => false,'label'=>false))?>
                                </div>
                                <div class="col-md-12  show-grid row">
                                    <?php $email=__("Email"); ?>
                                    <?php echo $form->input('Reference.email',array('class'=>'col-xs-12 col-sm-12 col-lg-12 text-style', 'placeholder'=>$email, 'div' => false,'label'=>false))?>
                                </div>
                                <div class="col-md-12  show-grid row">
                                    <?php $message=__("Message"); ?>
                                    <?php echo $form->textarea('Reference.message',array('class'=>'col-xs-12 col-sm-12 col-lg-12 text-style', 'placeholder'=>$message, 'div' => false,'label'=>false))?>
                                </div>
                                <input type="hidden" name="data[Reference][user_id]" value="<?php echo $user['id']; ?>">
                                <div class="col-md-12  show-grid row">
                                    <div class="align-center">
                                    <span class="span_float">
                                        <input type="submit" value="<?php echo __('Invite'); ?>" class="create-account btn btn-primary"  /><br/>
                                    </span>
                                    </div>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>

(Model) Reference.php :

class Reference extends AppModel {

    public $useTable = 'references';
    public $name = 'Reference';

    public $belongsTo = array('User' => array(
        'className' => 'User',
        'foreignKey' => 'user_id'
    ));

    var $actsAs = array('Multivalidatable');
    public $validationSets = array(
        'add' => array(
            'email' => array(
                'notEmpty' => array(
                    'rule'     => 'notEmpty',
                    'required' => true,
                    'message'  => 'Please enter email.'
                ),
                'valid' => array(
                    'rule'  => 'email',
                    'required' => true,
                    'message'  => 'Please enter valid email.'
                ),
            ),
            'name' => array(
                'notEmpty' => array(
                    'rule'     => 'notEmpty',
                    'required' => true,
                    'message'  => 'Please enter name.'
                )
            ),
        ),
    );
}

CommonComponent.php File:

<?php 

App::uses('Component', 'Controller');

class CommonComponent extends Component {

    public function sent_mail($params)
    {
        $Email = new CakeEmail();
        //$Email->config('smtp');       
        //$Email->template($params['template']);
        $Email->emailFormat('html');
        //$Email->viewVars($params['vars']);
        $Email->from(array($params['from'] => 'StempClub'));
        $Email->to($params['to']);
        $Email->subject($params['subject']);
        if($Email->send($params['vars']['link']))
        {
            return 1;
        }
        else
        {
            return 0;
        }
    }

    function sendCommonEmailTo($params)
    {
        $Email = new CakeEmail();                                               
        $email_template = $this->get_email_template($params['template']);
        $Email->to($params['to']);          
        $Email->from(array($params['from'] =>'StempClub'));
        $Email->subject($email_template['EmailTemplate']['subject']);           
        $Email->emailFormat('html');

        $email_template_content =  $this->render_email_template($email_template['EmailTemplate']['message'] , $params['vars']);
        $Email->send($email_template_content);              
        return true;
    }       

    function get_email_template($title)
    {
        $EmailTemplate  = ClassRegistry::init('EmailTemplate');
        $email_template = $EmailTemplate->find('first', array('conditions'=>array('EmailTemplate.keyword'=>$title)));
        return $email_template;
    }

    function render_email_template($html_content , $vars)
    {
        foreach($vars as $key => $value)
        {   
            $html_content = str_replace("$".$key,$value,$html_content);         
        } 
        return $html_content;
    }   

    public function resizeImage($image,$width,$height,$scale) 
    {
        list($imagewidth, $imageheight, $imageType) = getimagesize($image);
        $imageType = image_type_to_mime_type($imageType);
        $newImageWidth = ceil($width * $scale);
        $newImageHeight = ceil($height * $scale);
        $newImage = imagecreatetruecolor($newImageWidth,$newImageHeight);
        switch($imageType) {
            case "image/gif":
                $source=imagecreatefromgif($image); 
                break;
            case "image/pjpeg":
            case "image/jpeg":
            case "image/jpg":
                $source=imagecreatefromjpeg($image); 
                break;
            case "image/png":
            case "image/x-png":
                $source=imagecreatefrompng($image); 
                break;
        }
        imagecopyresampled($newImage,$source,0,0,0,0,$newImageWidth,$newImageHeight,$width,$height);

        switch($imageType) {
            case "image/gif":
                imagegif($newImage,$image); 
                break;
            case "image/pjpeg":
            case "image/jpeg":
            case "image/jpg":
                imagejpeg($newImage,$image,90); 
                break;
            case "image/png":
            case "image/x-png":
                imagepng($newImage,$image);  
                break;
        }

        chmod($image, 0777);
        return $image;
    }


    public function resizeThumbnailImage($thumb_image_name, $image, $width, $height, $start_width, $start_height, $scale)
    {
        list($imagewidth, $imageheight, $imageType) = getimagesize($image);
        $imageType = image_type_to_mime_type($imageType);

        $newImageWidth = ceil($width * $scale);
        $newImageHeight = ceil($height * $scale);
        $newImage = imagecreatetruecolor($newImageWidth,$newImageHeight);
        switch($imageType) {
            case "image/gif":
                $source=imagecreatefromgif($image); 
                break;
            case "image/pjpeg":
            case "image/jpeg":
            case "image/jpg":
                $source=imagecreatefromjpeg($image); 
                break;
            case "image/png":
            case "image/x-png":
                $source=imagecreatefrompng($image); 
                break;
        }
        imagecopyresampled($newImage,$source,0,0,$start_width,$start_height,$newImageWidth,$newImageHeight,$width,$height);
        switch($imageType) {
            case "image/gif":
                imagegif($newImage,$thumb_image_name); 
                break;
            case "image/pjpeg":
            case "image/jpeg":
            case "image/jpg":
                imagejpeg($newImage,$thumb_image_name,90); 
                break;
            case "image/png":
            case "image/x-png":
                imagepng($newImage,$thumb_image_name);  
                break;
        }
        chmod($thumb_image_name, 0777);
        return $thumb_image_name;
    }


    public function getHeight($image) 
    {
        $size = getimagesize($image);
        $height = $size[1];
        return $height;
    }


    public function getWidth($image) 
    {
        $size = getimagesize($image);
        $width = $size[0];
        return $width;
    }


    public function unlink_file($image_location)
    {
        @unlink($image_location);   
        return 1;
    }

    public function save_image_from_url($server_image_path,$filename)
    {
        $local_realimage_path = REAL_IMAGE_PATH.$filename;
        file_put_contents($local_realimage_path, file_get_contents($server_image_path));
        return $local_realimage_path;

        /*$ch = curl_init('http://example.com/image.php');
        $fp = fopen('/my/folder/flower.gif', 'wb');
        curl_setopt($ch, CURLOPT_FILE, $fp);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_exec($ch);
        curl_close($ch);
        fclose($fp);*/
    }

    public function copy_file($from_image_location,$to_image_location)
    {
        copy($from_image_location, $to_image_location);
        return $to_image_location;
    }       
}

It looks as if you are using SecurityComponent (maybe loaded in AppController::$components ?).

This component requires that you create your form with FormHelper , plus other restrictions mentioned in the docs for SecurityComponent .

If you wish to disable the security constraints for a particular action, you can add your method to:

$this->Security->unlockedActions()

As described in Disabling CSRF and Post Data Validation For Specific Actions .

This Security Component could be confusing at times and result in such issues. What I'd suggest is write the below mentioned code in your Model/AppModel.php. it works just as fine!

public function beforeSave($options = array()){

    if(isset($this->data)){

        foreach($this->data as $key=>&$result_array){

            foreach($result_array as $field=>&$value){

                if($value)
                    $value = filter_var ($value, FILTER_SANITIZE_STRING, FILTER_FLAG_NO_ENCODE_QUOTES);

            }

        }

    }
    return true;
} 

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