5
votes
Accepted
What's the difference between a Component and a Model in Craft 3?
Models are classes that contains abstractions of the reality, for example you could have the model car that contains the technical information about it, how many wheels it has and it's brand. Models ...
4
votes
Accepted
Craft Commerce: Custom address model validations
You can do either or both of:
Use javascript validation on the front end (with e.g. parsley)
Create a plugin with a method that is registered as a listener on this event:
https://craftcommerce.com/...
3
votes
Accepted
Override a model method via plugin
Kind of, but probably not what you're looking for.
Your plugin can certainly do a MyAssetFileModel extends AssetFileModel and (in the scope of your plugin) take advantage of existing AssetFileModel ...
3
votes
Accepted
Model values assignment issue
There is a second parameter in your setAttributes() function. This parameter is true per default and checks if the attribute is safe so you either have to declare all safe attributes in your records ...
3
votes
Accepted
How to duplicate an element type model with content?
I'm going to look pretty dumb if somebody drops a one-line answer down the line, but here goes: AFAIK, unfortunately, "cloning" an element (e.g. an EntryModel) isn't that simple.
For one, $model->...
2
votes
Accepted
Getting related models in templates?
I would move that logic out of your model class and into a service method.
Your model could still have a thin wrapper for it:
public function getVideos()
{
return craft()->...
2
votes
Accepted
Model/Record AttributeType documentation?
Problem is: so far as I can tell, nobody's written the book.
That's partly our fault... we've never really documented the Attribute parts of Models and Records. And it probably won't happen now ...
2
votes
Accepted
How do I write custom model validators for my plugin that refer to other fields?
Looks like I can just refer to the other field name:
public function validateUsername($attribute)
{
$value = $this->$attribute;
$otherFieldsValue = $this->otherField;
if ($value &...
2
votes
Accepted
Is it possible to return data/models from controller to template via setRouteVariables?
I believe you are overwriting the log variable you are sending back to the template when you set log equal to the response of getPost.
From what I can see of your code, when you return variables to ...
2
votes
Model public method throws CMapIterator warning
User error. It's a naming collision with the getAttributes method within my model and the CModel class.
2
votes
Accepted
How to add to arrays in models?
I'm pretty sure 'default'=>[] isn't doing anything so it's probably not necessary.
If you wanted to force a single attribute to be of a specific model, you could use:
'courierShippingItems' =>...
2
votes
Customise errors when validating model data?
This might be similar to Brad Bell's recommendation, but if you are using Yii's built-in validation rules, you should just override the rules() method in your model or record, specify the rule there, ...
2
votes
::model(), ::populateModel() and ::populateModels() in Craft 3
I can answer my own question with some information I've found to help anyone else for Craft 2 related porting.
The populateModel() and populateModels() methods used to exist on the BaseModel class in ...
1
vote
How can I best add additional fields to the Address form during the checkout
I would suggest that date and place of birth would be more appropriate as custom fields on the User profile rather than attached to the address. They don't appear to have any relevance to an address, ...
1
vote
Override entry.url / entry.getUrl() Method
I was able to get it to use my class via the following in config/app.php
'container' => [
'definitions' => [
\craft\elements\Entry::class => \modules\Entry::class
...
1
vote
Accepted
How to get instance of DiscountModel in Commerce 2?
$discount = new \craft\commerce\models\Discount();
1
vote
Accepted
AttributeType::Number giving validation error "Must be an integer"
Not really documented anywhere, but AttributeType::Number can accept an array of options, including min, max and decimal. You're looking for the last one.
AttributeType::Number => array('min' =&...
1
vote
no element type exists with getCriteria and custom element type
As Stephen pointed out, you're getting that error because your Commerce_GroupModel class is a Model that extends BaseModel, not an Element Type, which is what ElementsService->getCriteria() is ...
1
vote
Plugin development - Querying related data
I think you're looking for
$criteria->with = array('fieldHandle');
1
vote
Plugin development - Querying related data
I think this works but is it an acceptable solution:
$criteria = craft()->elements->getCriteria(ElementType::Entry);
$criteria->section = 'checkin';
$criteria->relatedTo = array(
'...
1
vote
Model public method throws CMapIterator warning
Difficult to know for sure without seeing what's going on in craft()->core_code->joinDataAttributes and what type of data is in $this->dataAttributes, but my guess is its because you're ...
1
vote
How to duplicate an element type model with content?
$clonedEntry = Craft::$app->getElements()->duplicateElement($entry);
Not sure if you have to save it afterward or not because in my code I'm altering one field on it after duplication and ...
1
vote
Validating Date of Birth format from a form field value
Here's what I ended up doing. Despite having the format of DD/MM/YYYY on the front-end the Craft model actually stores the date in YYYY-MM-DD. Given the main the reason for wanting to perform server ...
1
vote
Accepted
Validating Date of Birth format from a form field value
Would it be worth doing some client-side validation first using javascript to check the date of birth field is entered correctly? You can use an onChange event on your field and then split the string ...
1
vote
Getting UTC DateTime from Model attributes
I can't think of a way to automatically tell Craft to do this, but you could write a simple helper method to do this for you:
public static function convertDateTimeToUTC($dateTime)
{
return $...
1
vote
Accepted
Using $model->validate() with AttributeType::Url
Craft extends Yii's CUrlValidator to add support for protocol-less URLs as well as URLs without a TLD (such as your example of http://sdfsdfsfsdfsdf).
If you need other behaviors for your URL ...
1
vote
More extensive documentation for defining models for plugins
Have a look at Stephen Lewis' archived blog article about validation. Even better, buy the book.
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
models × 53plugin-development × 33
records × 17
attribute × 10
validation × 8
database × 5
plugin-craftcommerce × 4
relations × 3
controller × 3
services × 3
craft3 × 2
templating × 2
plugins × 2
php × 2
datetime × 2
elementcriteriamodel × 2
custom-field × 2
elementtype × 2
control-panel × 1
search × 1
entry × 1
form × 1
config × 1
plugin-craftcommerce2 × 1
tags × 1