Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

How to set a getter for form entities if edited?

Hi,

I have a form where a model is edited. I need to manipulte the results that come from the model. Example: From the model I get '2000-1-1' and I need to convert it to '01.01.2000' and insert it to the corresponding input value. How can I do that?

In other words: I get this now from model:

<input type="text" value="2016-07-31">

I need this:

<input type="text" value="07.31.2016">

Thanks for any help!



727
Accepted
answer

If I understand correctly, the problem is you get a wrong format of a string when you fetch from the model. You could implement an afterFetch() method in the model:

public function afterFetch()
    {
        //parse and rearrange $this->value string
    }


3.9k
edited Jul '16

Hey again, @bilicm, that doesn't completely solves my problem. I need the value unformated at some application points.



727
edited Jul '16

Well if you use the resultset object you could add a method in the model called something like getFormatedValue() which you could call only when you need to get the formatted value:

public function getFormatedValue() {
    //parse and rearrange $this->value string and save as $value or something
    return $value;
}

which you can then get when needed:

$result->getFormatedValue();


3.9k

But how can I apply that to my form? I need the formated value when my form is beeing edited.



727

You mean in the view? That depends on how you relay the data to the view. I'm guessing you run some sort of a query in the controller and then pass it the view. What exactly do you send to the view? The resultset? An array? A string?



3.9k
edited Jul '16

I'm just doing this in the view:

<?= $addForm->render('date'); ?>

But the created value is in the sql date format, not the format I need.

In the controller I just create the new form:

$user_run = UsersRuns::findFirstById($id);

$this->view->addForm = new AddForm($user_run, $id);


727

I'm sorry, I won't be of much help here I haven't used the Phalcon\Forms at all, but I'm guessing AddForm is something you defined in which you should somehow decide what data gets represented in the form and somehow call the getFormatedDate() value to the form. But that is speculation.



3.9k

Okay, thanks anyway.

The problem is, when I use a getter method inside my model the property is manipulated and the validation fails. The getter method displays the correct date but messes the form validation.