14

Ok worked out a solution to the attributes. You can set them like this: array(AttributeType::String, 'column' => ColumnType::Text, 'required' => false) By defining the column, so hopefully this will help someone else.


7

They're both the same for the most part. The only time I'd use the second way you listed is when the property is a variable. For instance: {% set foo = 'name' %} {{ variable[foo] }} This will give you {{ variable.name }}


4

If an attribute is set to true, it will get added without a value. {{ tag('input', { id: idVar, name: nameVar, required: true }) }} Also, if it is set to null or false, it will be omitted, as if it was never listed in the first place.


4

Generally speaking, no. They are just two ways of saying the same thing. Dot syntax is necessary when you are referring to a method within an object. However, if you are only trying to access the property of an object (or a value within an array), either technique will work.


4

Found an answer in the form of a customer validator class on the model: https://experiencehq.net/blog/the-definitive-guide-to-validation-in-craft-part-5-custom-validators#newsletter So something like this: public function validateMySetting($attribute) { $values = (is_array($this->$attribute)) ? $this->$attribute : [ $this->$...


3

When a User field is saved, it takes an array of user IDs only – not complete UserModel objects. Something like this should work: // get IDs for current members of group $members = $entry->members->ids(); // add new user ID to members $members[] = $addUser->id; // update field with new $members array $entry->getContent()->setAttribute('...


3

Give this a shot... {% set transform = (asset.thumbnailCrop.value ?: "portrait") ~ "ThumbSmall" %} {{ asset.url(transform) }} If asset.thumbnailCrop is a dropdown field (or similar), you'll need to specifically extract the value from it. If no value is found, it'll fall back to using the "portrait" string.


3

Another approach is to use the following event to attach a behavior Event::on(Asset::class, Asset::EVENT_DEFINE_BEHAVIORS, function(DefineBehaviorsEvent $event) { $event->behaviors[] = MyBehavior::class; }); The behavior could look like class MyBehavior extends Behavior { public $downloads; } This way you won't receive an exception and can ...


2

In your Micros_GuestRegistrationRequestModel's attribute definition, if you define person like so: 'person' => array(AttributeType::Mixed, 'model' => 'Micros_GuestPersonModel'); Craft should take care of ensuring person will be an instance of Micros_GuestPersonModel.


2

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 rules or you set the parameter to false.


2

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 since the majority of that has been stripped out of the upcoming Craft 3 making it much easier to follow Yii 2 conventions (which happens to be way more documented)....


1

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 side validation is to check certain parts of the date i.e. year, you can create an array out of the string. You'll want to do a check to make sure the date ...


1

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 on "/" and check each part is within a valid range 0-31, 1-12, 1900-2016. For serverside checking, you can use strtotime() and check that the return isn't ...


1

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 $dateTime->setTimezone(new \DateTimeZone('UTC')); }


1

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 validation, you can always write a custom URL validator.


1

I'm assuming that Alt Text (handle altText), Source Name (handle sourceName) and Source URL (handle sourceUrl are all plain text custom fields you've created on your Assets. If so, you'd be able to access sourceUrl the same as the others: {{ entry.assetHandle.first().sourceUrl }} They definitely aren't attributes on Craft's AssetSourceModel.


1

Use AttributeType::Name instead, this will undergo validation


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