We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

getMessages doesn't work properly ?

Hello,

I have a problem when I send a form post, I have something like this on my controller action:

$post->post_title = $this->request->getPost("title");

I receive the value but when I do this:

if ($post->create() == false) { foreach ($post->getMessages() as $message) { $a_msg[] = $message; } }

it says that post_title is required.

How can I solve this?

Thank you!



5.1k

Hello

Can you post the code between


$post->post_title = $this->request->getPost("title");

and


f ($post->create() == false) 

or do a var_dump of variable $post juste before the create

I suppose that this variable is reset between statements

Sounds like you're having some debugging difficulty. Do your best to isolate the problem so you can identify what works and what causes it to stop working. I assume you're initializing $post to a model like $post = new Post(); or whatever you called your model. Go into your model and double check that it has a post_title property defined on the class.

Also, have you tried doing something more in this direction:

$post->post_title = $this->request->getPost('title', null, 'untitled');

Then you'd do:

if (empty($post->post_title)){ $post->post_title = 'title not set'; }
$postDump = print_r(json_decode(json_encode($post)),true);
if ($post->create() == false)
{
    echo "create failed<br><pre>$postDump</pre><br>";
    foreach ($post->getMessages() as $message) { $a_msg[] = $message; }
} else {
    echo 'create successful';
}

Otherwise if you're still getting the message, can you show us where you're calling $post->create()? Are you doing this in the controller action as well in the same context? Can you hard-code a post_title rather than grab it from $_POST? Try these suggestions and get back to us with more information on what's going on.



1.1k

This is the code:

public function addnewAction() { $a_msg = array();

    if ($this->request->isPost())
    {
        $post = new Posts();

        //sa fac sa nu primeasca orice traba sa fac validation!
        $post->id_user                  = $this->session->get('auth-identity')['id'];
        $post->post_title              = $this->request->getPost("title"); 
        $post->post_description = $this->request->getPost('short_description');
        $post->img                        = $this->request->getPost('image_url');
        $post->post_text              = $this->request->getPost('long_description');
        $post->category_id          = $this->request->getPost('category');
        $post->date_time             = date("Y-m-d H:i:s");
        $post->cpc                        = 0;
        $post->traffic                   = 0;

        if ($post->create() == false)
        {
            foreach ($post->getMessages() as $message)
            {
                $a_msg[] = $message;
            }
        }
    }
    $success = 1;

    if( count( $a_msg ) )
    {        
        echo json_encode( array('status'=>'error', 'message' => implode("<br />", $a_msg)));
    }
    else
    {
        if( isset($success) && $success )
        {
            echo json_encode(array('status' => 'success', 'message' => "Good job accepted!"));
        }
    }
    $this->view->disable();
    return;
}


1.1k

It works like this:

$_POST['formData']['title'];

I send data to action from angularjs like this:

submitData: function(model, $scope){

        var config = {  headers : { 'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8;' } }
        var data = $.param({
            formData: model
        });
        var url = '../users/dashboard/addnew';

        return $http.post(url, data, config);
    }


1.1k

I think this is not working: $this->request->getPost();



1.1k
edited Jun '17

With var_dump: ' ["id"]=> NULL ["id_user"]=> string(1) "6" ["post_title"]=> NULL ["post_description"]=> NULL ["img"]=> NULL ["post_text"]=> NULL ["category_id"]=> int(0) ["date_time"]=> string(19) "2017-06-30 09:32:09" ["cpc"]=> int(0) ["traffic"]=> int(0) '



5.1k
edited Jun '17

you call


$post->post_title  and in your var_dump you have ["posttitle"]

try : $post->posttitle = $this->request->getPost("title"); or $post->postTitle = $this->request->getPost("title");

Models use class magc function to et Attribute They don't like underscores and tranform them in camelCase



1.1k
edited Jun '17

No, I don't know why this editor it is soo ugly. On var_dump I have post underline title this editor elimitates the underline.

you call


$post->post_title  and in your var_dump you have ["posttitle"]

try : $post->posttitle = $this->request->getPost("title"); or $post->postTitle = $this->request->getPost("title");



5.1k
edited Jun '17

Yes y have the same problem too and i have edited my post

Before and after your code

use ``` with a blank line before and a blank line after



5.1k
edited Jun '17

But you can try this ? without underscore and the T of tilte in uppecase

Models use class magc function to set Attribute

They don't like underscores and tranform them in camelCase

Edit


$post->postTitle = $this->request->getPost("title");
edited Jun '17

Regarding the editor, backslash any underlines for literal underlines, it's markdown, otherwise it has meaning in markdown.
Double check the "Preview" before submitting to make sure it looks right. You can also put code between ticks, this character: `

Since you're having issues with getPost, you might try get instead which works on the entire $_REQUEST:

$post->post_title = $this->request->get('title', null, 'untitled');

Might also try print_r($_POST) to make sure you're getting what you think you're getting from angular.
This section of the documentation should be helpful: https://olddocs.phalcon.io/en/3.0.3/api/Phalcon_Http_Request.html



1.1k
Accepted
answer
edited Jun '17

Ok I solved like this: I send the post like formData { array [ title, description etc.]} WIth $request = $this->request->getPost('formData'); I have now the access to the array so after that for every $post->title = $request['title'];

Regarding the editor, backslash any underlines for literal underlines, it's markdown, otherwise it has meaning in markdown.
Double check the "Preview" before submitting to make sure it looks right. You can also put code between ticks, this character: `

Since you're having issues with getPost, you might try get instead which works on the entire $_REQUEST:

$post->post_title = $this->request->get('title', null, 'untitled');

Might also try print_r($_POST) to make sure you're getting what you think you're getting from angular.
This section of the documentation should be helpful: https://olddocs.phalcon.io/en/3.0.3/api/Phalcon_Http_Request.html



3.4k

Hello,

if you've found your solution, can you pass your post to solved ?

thanks ;o))