Sure, that would work.
It be better to override the set() function, since Model::create() and
Model::save() call it internally:
function set($one, $two = null) {
parent::set($one, $two);
if (empty($this->data[$this->alias]['title'])) {
$this->data[$this->alias]['title'] = [...]
}
}
This way, it doesn't matter if you set the data using set(), create(),
or save(), it will get processed in any case
On Feb 5, 12:27 pm, "Dardo Sordi Bogado" <[EMAIL PROTECTED]> wrote:
> I'm using it myself this way:
>
> function create($data = array())
> {
>
> if ($this->i_dont_like_the_value_here($data['Model']['field'])
> {
> $data['Model']['field'] = $this->put_something_better();
> }
>
> parent::create($new);
> }
>
> And in controller:
>
> $this->Model->create($this->data)
>
> On Feb 5, 2008 9:00 AM, grigri <[EMAIL PROTECTED]> wrote:
>
>
>
> > > Also Model::create()
>
> > That won't work in this situation. Novice is referring to a case where
> > a user leaves an input field blank, but it's still present, so any
> > value you put in Model::create would be overwritten with the blank
> > string sent in the form.
>
> > > beforeValidate() seems to be a nice place then because validation might
> > > reject the values that i want to modify later on. Am i correct?
>
> > If you only want to generate an automatic title when the user leaves
> > it blank, and you're sure that any automatic title you generate will
> > be valid, then it would be better to update the information in
> > beforeSave() and set 'allowEmpty' to true in the validation rule for
> > the title. It "feels wrong" to change the data in beforeValidate(),
> > but that's just a personal opinion.
>
> > On Feb 5, 10:46 am, "Dardo Sordi Bogado" <[EMAIL PROTECTED]> wrote:
> > > Also Model::create()
>
> > > On Feb 5, 2008 7:19 AM, Novice Programmer <[EMAIL PROTECTED]> wrote:
>
> > > > beforeValidate() seems to be a nice place then because validation might
> > > > reject the values that i want to modify later on. Am i correct?
>
> > > > Thanks.
>
> > > > On 2/5/08, nate <[EMAIL PROTECTED]> wrote:
>
> > > > > Not true actually. Generally speaking, any time you *can* put logic
> > > > > in the model, you should. Don't move it out into the controller
> > > > > unless it is necessary for some reason.
>
> > > > > On Feb 4, 11:14 pm, manish <[EMAIL PROTECTED]> wrote:
> > > > > > I think, The best way to do this stuff is in the controller itself.
>
> > > > > > First you need to check whether the data is posted from the form.
> > > > > > You can do so by doing something like:
>
> > > > > > if(!empty($this->data['ModelName']['id']))
>
> > > > > > if it is so, you can check:
>
> > > > > > if(empty($this->data['ModelName']['title']))
> > > > > > $this->data['ModelName']['title'] = "Your_Custom_Title";
>
> > > > > > Hope this helps.
>
> > > > > > On Feb 5, 7:48 am, "Novice Programmer" <[EMAIL PROTECTED]>
> > > > > > wrote:
>
> > > > > > > Hello guys,
>
> > > > > > > I was wondering that what is the best place to change the data to
> > > > > > > be
> > > > fed to
> > > > > > > the model. Consider the following situation:
>
> > > > > > > I have a form which asks the user about the title of a content.
> > > > > > > if the
> > > > user
> > > > > > > has not given the title, i want to give some default value to the
> > > > title(note
> > > > > > > that this default value is dynamic and called be defined using
> > > > > > > default
> > > > > > > directive in MySql).
>
> > > > > > > Now what is the best place to do this stuff? is it beforeValidate
> > > > > > > or
> > > > > > > beforeSave or should i modify the data in the controller itself?
>
> > > > > > > --
> > > > > > > Thanks & Regards,
> > > > > > > Novice (http://ishuonweb.wordpress.com/).
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Cake
PHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---