14 votes

What attribute type should I set in the record or model to create a TEXT type in the database?

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 ...
user avatar
  • 2,023
7 votes
Accepted

Difference between using subscript syntax and "dot" syntax when accessing attributes of a variable or advantages of usage one of them

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 ...
user avatar
4 votes

Setting valueless boolean attributes with the tag function

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,...
user avatar
4 votes

Difference between using subscript syntax and "dot" syntax when accessing attributes of a variable or advantages of usage one of them

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 ...
user avatar
  • 23.6k
4 votes

How do I validate an array model attribute against a set of possible values?

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 ...
user avatar
  • 1,503
3 votes
Accepted

Update User "Field Type" for Entry in Plugin

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->...
user avatar
3 votes
Accepted

Accessing and Manipulating a Variable

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 ...
user avatar
  • 23.6k
3 votes
Accepted

Add custom attribute to standard element from plugin

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[] = ...
user avatar
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 ...
user avatar
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 ...
user avatar
  • 66.4k
2 votes
Accepted

Is there a good way of setting a model attribute to be an instance of another model?

In your Micros_GuestRegistrationRequestModel's attribute definition, if you define person like so: 'person' => array(AttributeType::Mixed, 'model' => 'Micros_GuestPersonModel'); Craft should ...
user avatar
  • 66.4k
1 vote
Accepted

What's the attribute of a Matrix field to add error message

Matrix blocks are elements themselves, so the individual fields of a matrix block aren't available as properties on the entry. In the entry save hook, the only thing you can add errors to (that will ...
user avatar
  • 5,985
1 vote

What's the best practice for creating computed (dynamic/virtual) attributes on entries?

You can use behaviors to add new methods to system components. See the link above for documentation. A behavior is a class that extends yii\base\Behavior and is attached to a component using the ...
user avatar
  • 5,985
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 ...
user avatar
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 ...
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 $...
user avatar
  • 66.4k
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 ...
user avatar
  • 66.4k
1 vote
Accepted

Accessing the attributes of an Asset field

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 ...
user avatar
  • 66.4k
1 vote
Accepted

AttributeType::String is NULL in my database

Use AttributeType::Name instead, this will undergo validation
user avatar

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