Hot answers tagged

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 ...
Robin Schambach's user avatar
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/...
Jeremy Daalder's user avatar
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 ...
Brad Bell's user avatar
  • 67.3k
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 ...
Robin Schambach's user avatar
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->...
Mats Mikkel Rummelhoff's user avatar
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()->...
Brad Bell's user avatar
  • 67.3k
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 ...
Brad Bell's user avatar
  • 67.3k
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 &...
danieltalsky's user avatar
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 ...
Ben Parizek's user avatar
  • 13.4k
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.
Fyrebase's user avatar
  • 735
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' =>...
Brad Bell's user avatar
  • 67.3k
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, ...
Tim Everts's user avatar
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 ...
James White's user avatar
  • 1,141
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, ...
Martin Spain's user avatar
  • 1,484
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 ...
Josh Coady's user avatar
1 vote
Accepted

How to get instance of DiscountModel in Commerce 2?

$discount = new \craft\commerce\models\Discount();
Luke Holder's user avatar
  • 6,767
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' =&...
Brad Bell's user avatar
  • 67.3k
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 ...
Brad Bell's user avatar
  • 67.3k
1 vote

Plugin development - Querying related data

I think you're looking for $criteria->with = array('fieldHandle');
nullvariable's user avatar
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( '...
Alan A's user avatar
  • 131
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 ...
Brad Bell's user avatar
  • 67.3k
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 ...
Brimby's user avatar
  • 333
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 ...
James White's user avatar
  • 1,141
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 ...
Alexander Holsgrove's user avatar
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 $...
Brad Bell's user avatar
  • 67.3k
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 ...
Brad Bell's user avatar
  • 67.3k
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.
carlcs's user avatar
  • 36.2k

Only top scored, non community-wiki answers of a minimum length are eligible