How to change field changeNote from IVersionable behavior to be required - plone

I have use case where users should allways fill the changeNote field when saving changes on content. Is it possible (TTW) to change the changeNote field from IVersionable behavior to be required ? And if not, is it possible in code ? What is the best way to override standard fields properties from foreign/preinstalled behaviors ?

I don't know if there's another way to do this but you may override the value of the required attribute by implementing something like this in your code:
(Pdb) from plone.app.versioningbehavior.behaviors import IVersionable
(Pdb) IVersionable
<SchemaClass plone.app.versioningbehavior.behaviors.IVersionable>
(Pdb) IVersionable['changeNote']
<zope.schema._bootstrapfields.TextLine object at 0x7fe45a60f550>
(Pdb) IVersionable['changeNote'].required
False
(Pdb) IVersionable['changeNote'].required = True
(Pdb) IVersionable['changeNote'].required
True

Related

Is there a QML type allowing to execute certain actions once some condition became true?

I need to execute some QML slot once certain condition became true.
I know there is a Binding type which can assign a property with some value once "when" condition became true. I need something similar but instead of setting the "value" to "property" I want to execute some arbitrary actions.
Thanks.
In other words, something like this?
QtObject {
readonly property bool foobar: someExpression
onFoobarChanged: {
if (foobar) { ... }
}
}
There is a third party library that does that : benlau's QuickPromise
You could use it that way :
import QuickPromise 1.0
//...
Promise {
resolveWhen: someExpression
onFulfilled: arbitraryAction()
}
It would be better to use Connections type:
Connections {
target: conditionKeeper
onConditionChanged: console.log("Processing...")
}
You could keep your logic in any QObject, even in C++. Once your condition becomes true, your code will be executed. It can be used out of the box without any dependencies.

Java bean validation: Optional fields annotation

I would like to treat some fields as Optional, if the value is null or blank don't go with the checks of the other annotated constraints.
There is some way to achieve it!
Reading this tread Java bean validation: Enforce a #Pattern only when the property is not blank don't seems cover my needs.
Below an example to explain what I mean:
public class Test {
#Max(value=100) // <--mandatory
private int parA;
#Optional // <-Custom annotation telling "do not check other if null or blank"
#Range(min=10, max=200)
private int parB;
...
}
Now you can!
https://docs.jboss.org/hibernate/stable/validator/reference/en-US/html_single/#example-container-element-constraints-optional
public Optional<#Size(min=1, max=128) String> first_name = Optional.empty();
You cannot do what you want with Bean Validation. You cannot establish a "connection" between two constraints placed on a property. Also, if I understand correctly, your #Optional is not even a constraint annotation, but rather just a marker annotation. Note though, that all default constraints are implemented in a way that a constraint validates for null values. So in most cases you get what you want either way. In your example, you also have the problem that you are dealing with a primitive int. This will always have a value, so the range constraint would be evaluated either way.

symfony2 routing requirements combining 2 parameters

So I got this in my routing.yml:
requirements:
var1: \d+
var2: \d+
Both are checked on their own and valid.
I need to check the combination of the 2, since the combination is not always valid.
For this case I need to check the relation between 2 objects in the database, the first should be the parent of the second.
I can do this in the controller, but I don't really like that implementation. Also, I need this same check for more than 1 route.
How would I add another requirement that checks the combination? Can I define a method in the controller class that would be called?
Or would the best solution be something like:
public function indexAction($var1, $var2)
{
$result = $this->checkRelation($var1, $var2);
if ($result) {
// return errorpage
return $result;
}
// ...
}
So as I understand your question, you want the following:
/parent/child/ --> returns 200
/not_parent/not_child --> returns 404
The Symfony2 Routing component doesn't do this natively, but you could extend it.
http://symfony.com/doc/master/cmf/cookbook/using-a-custom-route-repository.html
The final solution I went with was the following:
add a method checkRelation that requires all parameters
run a query inside that method to check if everything is ok.
return false when there is a problem, return true when values are ok. (alternatively you can return an object or something)
in the action I check if the value is false, if so I return a generic "not found" page for the specific controller.
In all this is very similar to what I posted in my initial question.
When using the same checkRelation in multiple controllers it might be a good idea to move it (partially) to a repository-class or something similar to prevent duplication of code/logic.

Does Grails have a built-in way to bind a string to a date for a single bean property?

I know that it's possible to register a custom property editor, as demonstrated here. It is my understanding that this will cause all properties for the registered type to be bound using that custom editor.
(Perhaps that's a misunderstanding on my part? If so, enlighten me!)
However, what if I only want to register a custom editor for a single property on a given domain?
Example:
class MyCommand {
Date foo // bind this using a custom format, e.g. 'yyyy-MM-dd'
Date bar // bind this using the normal 'struct' date picker fields
}
class MyController {
def myAction = { MyCommand command ->
// params has [foo: '2011-01-01', bar: 'struct', ...]
// which get bound to command as above
}
}
Does Grails have a built-in way to do this?
I know that it's possible to register a custom property editor, as demonstrated here. It is my understanding that this will cause all properties for the registered type to be bound using that custom editor.
AFAIK, this is correct
However, what if I only want to register a custom editor for a single property on a given domain?
If you're using Grails 2.3.0 or later, you can do this like so:
class MyCommand {
#BindingFormat('yyyy-MM-dd')
Date foo // bind this using a custom format, e.g. 'yyyy-MM-dd'
Date bar // bind this using the normal 'struct' date picker fields
}
You could write getter and setters that take the format you want and return the format you want. The DateFormatter or SimpleDateFormatter classes would be useful in your get/set methods.
You can still use the neat trick for dates in grails:
<g:form>
<g:hiddenField name="myDomainInstance.bar_year" value="2011"/>
<g:hiddenField name="myDomainInstance.bar_month" value="07"/>
<g:hiddenField name="myDomainInstance.bar_day" value="01"/>
<g:textField name="myDomainInstance.bar_hour" value=""/>
<g:textField name="myDomainInstance.bar_minute" value=""/>
</g:form>
In controller:
myDomainInstance.properties = params.myDomainInstance
Will result in the desired date for bar.

Showing default widget value only when object value is not set (in NEW mode)

THIS QUESTION IS NOT ABOUT HOW TO SET DEFAULT VALUE OF A WIDGET
Hello Symfonians!
I had a fundamental doubt about forms, Im putting 2 scenarios below.
I have a customModelForm that extends a modelForm.
1> If I do not specify a default value for a form field
new: field is empty
edit: field shows the value in the object
2> If I specify a default value for a field,
new: field shows default value
edit: field shows default value
I am trying to avoid the EDIT mode behaviour in scenario 2.
The default value should only be displayed when the value in the object is not set.
I am calling parent::configure after setting the default value. Do we have any control on the 'bind' event?
Thanks
This shouldn't be happening, at least in Doctrine. The part of the code where this is happening is in updateDefaultsFromObject in sfFormDoctrine. The relevant lines are:
if ($this->isNew())
{
$defaults = $defaults + $this->getObject()->toArray(false);
}
else
{
$defaults = $this->getObject()->toArray(false) + $defaults;
}
updateDefaultsFromObject does net get called until the entire configure chain is done, so something else must be going on here.
Are you using Doctrine? Are you using the most current version of Symfony (there was a bug here a while ago)? Are you sure the default is getting set in the configure method of your form?
The isNew check richsage is recommending should be avoided. There is a larger issue here as the proper behavior is for default value to get overwritten by an existing object's values.
First of all, call parent::configure() first in your configure() method. That way you don't run the risk of your configuration being overwritten by the parent configuration.
You can set defaults based on the model's status by doing something like the following in your configure() method:
if ($this->getObject()->isNew())
{
// do something here but only if the object is new
}
else
{
// the object is being edited
}

Resources