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 ...
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 ...
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,...
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 ...
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 ...
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->...
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 ...
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[] = ...
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 ...
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
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 ...
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 ...
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 ...
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
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 ...
1
vote
Accepted
AttributeType::String is NULL in my database
Use AttributeType::Name instead, this will undergo validation
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
attribute × 22plugin-development × 11
models × 10
records × 4
craft3 × 2
plugins × 2
datetime × 2
events × 2
entrymodel × 2
templating × 1
assets × 1
users × 1
database × 1
entry × 1
form × 1
field × 1
error-message × 1
fieldtypes × 1
variable × 1
entries-field × 1
validation × 1
assetfilemodel × 1
plugin-guestentries × 1
element × 1
table × 1