7 votes
Accepted

How can I add custom validation rules to my plugin settings?

The gist is, you will need to give your plugin a dedicated Settings model class, where you will have complete control over its validation rules and errors. Here’s how to set that up: Create a new ...
6 votes

Programmatically update entry field

I got around this issue by looping through all fields that were of BaseElementFieldType re-setting them to their current value. $fields = $entry->getFieldLayout()->getFields(); $...
5 votes
Accepted

How to return errors from a model back to the template?

If you use $model->getErrors() instead, it should return a multidimensional array of errors indexed by attribute name like so: array( 'email' => array( 'First email error.', ...
  • 66.5k
5 votes
Accepted

Entry validation not working when Publish live changes permission is not checked

As carlcs said, this is already working as expected. Craft only enforces content validation rules for enabled entries (not including drafts), and most people prefer that behavior. If you want to ...
4 votes
Accepted

Merge element type errors? (title + hard-coded field)

You can pass the element off to ContentService::validateContent() in the event that it doesn't pass the record’s validation, just to get those errors mixed into the element as well. if (!$record->...
4 votes

Programmatically update entry field

You're very close. Try this: $entry->getContent()->setAttribute('metaStatus', 'Declined'); craft()->entries->saveEntry($entry);
  • 7,248
4 votes

Entry validation not working when Publish live changes permission is not checked

As other answers have touched on, the behaviour you're seeing is by design. I've written a plugin called Always Validate which will make Craft validate all disabled entries and categories – including ...
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/...
4 votes
Accepted

Validation on username?

I don't know if there is anything natively in Craft to do this, but you could achieve it quite easily with a single file plugin: public function init() { craft()->on('users.onBeforeSaveUser', ...
  • 4,519
4 votes
Accepted

Title field character length

You can create a custom validation at the EVENT_BEFORE_SAVE event Event::on(Entry::class, Entry::EVENT_BEFORE_SAVE, function(ModelEvent $event){ /** @var Entry $entry */ $...
3 votes
Accepted

Contact Form Plugin: Validate if a checkbox is checked?

There's no clean way doing this without making changes to the plugin itself. The contactForm.beforeSend event makes it possible to prevent the email from being sent from another plugin, but you're ...
  • 35.9k
3 votes
Accepted

A plugin to validate a field depending on another field's value

In your FieldType you have $this->element available, representing the Element the field is in. If you use $this->element->lightSwitchHandle you can access its value.
3 votes
Accepted

How do I get the last uri segment from within a plugin (php)

Have a look at the segments property (shortcut for getSegments() method) of the HttpRequestServices class, which returns an array of uri segments. craft()->request->segments; To get the last ...
3 votes

Can a model or record have conditional requirements?

I can think of a couple of options and I'll just throw them all out there. Have a separate model per partial form and each model performs its own validation. Have a single model, but have it override ...
  • 66.5k
3 votes

"Custom" validation for plugin settings page

It sounds like you're on the right track. You could create a custom validator, as detailed here. <?php namespace Craft; use Carbon\Carbon; class MyPlugin_ExampleModel extends BaseModel { ...
3 votes
Accepted

Repopulate a static select input

No, I don't think there's a better way, you need to add that conditional to each select option. But what you could do is to refactor a bit and move that logic to a Twig macro. {% macro stateOption(id,...
  • 35.9k
3 votes

You don’t have the proper credentials to access this page

Be sure that you're testing as a logged-out user on the front-end of the site. If you have an active Craft session, you could be getting a false positive error message.
  • 66.5k
3 votes

Programmatically update entry field

I'm not completely sure of the technical reasons (I guess something to do with relation fields not have a column in the craft_content table), but you also need to re-save the existing ids for any ...
  • 4,361
3 votes

Programmatically update entry field

Craft 3 $entry->setFieldValue('my_field_handle', 'new_value'); $app->elements->saveElement($entry);
3 votes
Accepted

Logged in after email validation

I believe you're looking for the aptly named autoLoginAfterAccountActivation config setting.
  • 66.5k
3 votes

Entry validation not working when Publish live changes permission is not checked

That's the idea behind Drafts, to store entries not yet ready to publish, unfinished ones so to speak. I think it does make sense to not validate the fields. Good feature!
  • 35.9k
3 votes
Accepted

How do I include record relationships in validation?

Yii/Active Record won't do this by default. You'd need to manually add an exists validation rule to the typeId attribute to get it to behave like you're looking for.
  • 66.5k
3 votes
Accepted

Front-end registration validation

You could use Regex (Regular Expression), /^[a-zA-Z]{3,}$/ would for instance allow a minimum of 3 characters behind the comma would be the max value. As for numbers it would be something like this /^...
  • 353
3 votes

Craft Commerce — Billing / Shipping Address validation / required Fields

It will be possible in the next release (commerce 2-beta.5): https://github.com/craftcms/commerce/issues/342
  • 6,702
3 votes

Craft Commerce — Billing / Shipping Address validation / required Fields

Address validation is, in Commerce v1 at least, generally handled via a plugin. (I'll assume you already have a business logic plugin on the go, but if not head over to https://pluginfactory.io and ...
3 votes

How to validate a unique field in Craft 3 element?

Here's my findings after some experimentation: Elements and Records are two entirely separate entities. The Element is basically the core Craft database entry, while the Record is the additional ...
  • 175
2 votes

You don’t have the proper credentials to access this page

I ran into this issue too, but there is another cause to this error. Go into Settings > Users > Settings in the Control Panel and ensure "Allow public registration?" is checked, it's unchecked by ...
  • 403
2 votes
Accepted

Using the required field validation in custom element types/plugins?

Custom field validation happens automatically for you when you pass your element into craft()->elements->saveElement(). That function will return true or false, largely depending on whether ...
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

Customise errors when validating model data?

In addition to Brad’s answer (which is what I was looking for), you could also use template-specific messages using conditionals if for some reason you only needed to override the global error ...
  • 1,128

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