I’m using Datamapper (1.2.0) and Sinatra (1.3.2). I have a model and a property of the model is Employee ID. This is a mandatory field so whenever this is not been entered by the user I need to throw validation error.
Datamapper intelligently identifies the name of the property as 'Employee' (cuts down id part) and displays the error as 'Employee can't be blank' and 'Employee should be an integer'.
So I tried to override these error messages. I could able to override the 'Employee can't be blank' but cannot able to override the other.
property :employee_id, Integer, :required => true, :unique => true,
:messages => {
:presence => "Employee ID cannot be blank.",
:is_unique => "Employee ID should be unique."
}
What should be the hash key I need to use to override the 'not_an_integer' error?
I think the message key you’re looking for is :is_number. Where this is documented is a bit hidden away. (I actually looked for it in the source).
Also, it seems to be if you have any :messages hash in the property options then the default messages are replaced with nil if you don’t specify a custom message for that validation.
Related
I have two fields in my module called: start_date_c & end_date_c with date datatype
These fields are not mandatory fields however when i enter data into the end_date_c field I would like to make it is not less than start_date_c
I have tried the following:
https://suitecrm.com/suitecrm/forum/suitecrm-7-0-discussion/12522-how-to-validate-start-and-end-date-in-suitecrm
http://sugarmods.co.uk/how-to-add-custom-validation-to-form-fields-in-sugarcrm/
but as i am new to suiteCRM, i am not able to find positive response
You will need 2 things
Edit the file editviewdefs.php in the module you want to add the logic. This field will be autogenerated when you add the first custom field to the edit view.
Create your custom JS logic to define when the field is valid.
This logic here will add a validation callback for your
addToValidateCallback(
'EditView', // Form Name
'end_date_c', // field name
'datetime', // Field type
false, // Is required
"End date cannot be earlier than start date", // Message
function() {
//WRITE YOUR JS VALIDATION HERE, return true when is valid
});
In the editviewdefs.php find the field definition and use the displayParams to make suite/sugar add the JS for you.
array (
'name' => 'end_date_c',
'displayParams' =>
array (
'updateCallback' => 'FUNCTIONNAME',
),
),
The last step ain't needed if you already have a global custom JS (like style.js file for a custom theme).
EDIT: javascript DisplaParams will not work, so added the updateCallback option.
Now this validation works in 2 ways.
The updateCallback will be fired onChange
The addtoValidateCallback will be fired on Save.
This will give you enough flexibility to validate the form.
Simple and one linear, try following in any JS file (added in module edit view):
addToValidateDateBefore('EditView', 'start_date_c', 'date', false,'Date Start', 'end_date_c' );
worked for me.I added the following code to the fields in modules/custom_module/vardefs.php
'audited' => true,
'enable_range_search' => true,
and added the following to the start field
'validation' =>
array (
'type' => 'isbefore',
'compareto' => 'enddate',
'blank' => true,
),
I'm not quite sure about the way of doing.
The challenge:
I call an addAction which shows a form. The point of calling the addAction gives two routing parameters, say value1 and value 2 separated by an "-".
I need value 1 and value 2 to search a pk in a table which will be saved as a foreignkey value by the addAction. I take both values give it to a method and get the key I need, that is tested and ok so far.
My problem.
In the first call of addAction I get the routing parameters and find the key. But afterwards of course it is forgotten. How can I remember the found value, so that I can use it for my saveModel method?
What would be the best way?
Idea 1:
Can I give it to the form and set it as value to the hidden keyfield?
For example:
class PadForm extends Form
{
public function __construct($name = null, $unitpartid)
{
parent::__construct('pad');
$this->add([
'name' => 'UnitPartPadID',
'type' => 'hidden',
'value' => $unitpartid,
]);
Would this be working? And would this be an accepted, proper way?
Idea 2:
Or would I try to use an instance variable in my controllerclass, like $this->smtg; ?
In both cases I get an understandable error
Trying to get property of non-object
Questions:
what would be the best way?
and
how to do it, perhaps somebody could give a short example.
EDIT:
I really would appreciate to learn about the best way. I now tried to give a variable to my form and then fill in some field, but that doesn't work.
Part of Controlleraction
(the action works if I set a constant value for the related field)
$parameter = $this->params()->fromRoute('id');
// echo $parameter;
$partnumber =substr($parameter,0,strpos($parameter,"-"));
// echo $partnumber;
$unitid=substr($parameter, strpos($parameter,"-")+1, strlen($parameter));
// echo $unitid;
$test=$this->unitparttable->getUnitPartID($unitid, $partnumber);
echo $test->UnitPartID;
$form = new PadForm(NULL, $test->UnitPartID);
Then in the Formclass:
public function __construct($name = null, $unitpartid)
{
// We will ignore the name provided to the constructor
parent::__construct('pad');
// $this->db=$db;
$this->add([
'name' => 'UnitPartPadID',
'type' => 'hidden', //hidden
]);
$this->add([
'name' => 'UnitPartID',
'type' => 'text', //hidden
'value' => $unitpartid,
]);
The question is now, how to fill the formfield UnitPartID with the value of $unitpartid given within the constructor.
I also tried $form->populate but it is unknown, I used it in ZEND1 before, but probably it doesn't exist anymore.
any help appreciated!
I have a dropdown menu in my form and the form structure depends on its value. I have managed to solve the "form-update-issue" with event subscriber/listener class, where i am trying to update the main form according to dropdown's value.
The main problem is that i have to modify the form from values that persisted in the database.
My DB schema:
I have 4 table: Model, ModelCategory, ModelCategoryKey, ModelParameter.
ModelCategory 1--n Model 1--m ModelParameter
ModelCategory 1--n ModelCategoryKey
ModelCategoryKey 1--n ModelParameter
After the user choose a ModelCategory from the form's (form based on Model entity) dropdown i have to update the form with ModelParamater rows, but it's number and default values depends on ModelCategory 1--n ModelCategoryKey assocaiton.
I've tried to attach NEW ModelParameter entities to the main Model entity during the PRE_BIND event (also set their default values) and it seems working fine, but when i add the 'parameters' with a 'collection' typed element to the form i get the next error:
Entities passed to the choice field must be managed. Maybe persist them in the entity manager?
Clearly my entities can't be (and shouldn't be) persisted at this time.
All ideas are welcome!
UPDATE:
Modifying the form after preSubmit/preBind:
$form->add('parameters','collection',array(
'type' => new ModelParameterType(),
));
OR
$form->add(
$this->factory->createNamed('parameters','collection',null,
array(
'type' => new ModelParameterType()
))
);
where the 'factory' attribute is a FormFactoryInterface. The error message is the same.
UPDATE2:
Further investigation proved, that if i don't add "default" entities to the assocation. Then it works without error.
Here is the source of my form modifying method:
public function preSubmit(FormEvent $event) {
$form = $event->getForm();
$id = $event->getData()['modelCategory'];
$entity = $form->getData();
$categoryKeys = $this->em->getRepository('MyBundle:ModelCategoryKey')->findByModelCategory(
$this->em->getReference('MyBundle:modelCategory',$id)
);
foreach ($categoryKeys as $key) {
$param = new ModelParameter();
$param->setModel($entity);
$param->setKey($key);
$entity->addParameter($param);
}
$form->add(
$this->factory->createNamed('parameters','collection',null,
array(
'type' => new ModelParameterType(),
'allow_add' => true,
'cascade_validation' => true
))
);
}
SEEMS TO BE SOLVED BY
I have just commented out the $param->setModel($entity); line and it seems to be working fine. I will work this out more and will share the experience, if it realy works.
choice field accepts only managed entities, as the value is set to the entity after submit, and form posts only entities ID, so it must be saved beforehand.
You don't need choice field - you need collection of parameter subforms.
$formBuilder
->add('category', 'category_select')
->add('parameters', 'collection', array('type' => 'parameter'))
;
I'm assuming here that category_select is choice field with categories and parameter is subform with it's own values, depending on your parameter structure.
When you have category in your controller, you can bind the newly created entity with added Parameter entities with their key set, depending on ModelCategoryKey.
I've managed to solve my problem, so here it is what i've found out:
It is enough to add the newly created object by the adder function of the inverse side. I don't have to call the owning side's setter.
Inverse side adder function must be modified, that it calls the owning side's setter.
Inverse side adder function must check if the object is not in the collection already.
PRE_SET_DATA event happens, when the form is created. (so in new entities it is empty, and in old ones it is filled)
I'm trying to do this with mongodbauthmanager. I'm follow step by step in Usage section but finally i'm getting PHP warning: Illegal offset type. I had posted this question at Yii Extension before clone to SO:
Please tell me what is wrong?
1// Config
'authManager'=>array(
'class' =>'CMongoDbAuthManager',
'showErrors' => true,
),
2// Create auth items in db
$auth = new CMongoDbAuthManager();
$bizRule = 'return Yii::app()->user->id==$params["User"]->_id;';
$auth->createTask('updateSelf', 'update own information', $bizRule);
//I had tried with $auth->createOperation() but they has the same error
$role = $auth->createRole('user');
$role->addChild('updateSelf');
$auth->save();
and here is result in db
result in db http://i.minus.com/iIpXoBlDxaEfo.png
**3// Checking access in controller ** - UPDATE CODE AND ERROR
public function actionUpdate($id)
{
$model=$this->loadModel($id);
$params = array('User'=>$model);
if (!Yii::app()->user->checkAccess('updateSelf', Yii::app()->user->id,$params) )
{
throw new CHttpException(403, 'You are not authorized to perform this action');
}
//another statement ...
}
4// Getting error:
Fatal error : Cannot use object of type MongoId as array in F:\Data\03. Lab\www\yii\framework\web\auth\CAuthManager.php(150) : eval()'d code on line 1
RESOLVED PROBLEM
Base-on the answer of #Willem Renzema, I resolve my problem. Now, I update here and hope it useful for someone have this error.
0// First, config authManager with defaultRoles
'authManager'=>array(
'class'=>'CMongoDbAuthManager',
'showErrors' => true,
'defaultRoles'=> array('user'),//important, this line help we don't need assign role for every user manually
),
1// Fix save id in UserIdentity class
class UserIdentity extends CUserIdentity
{
private $_id;
//...
public function authenticate()
{
//...
$this->_id = (string)$user->_id;//force $this save _id by string, not MongoId object
//...
}
//...
}
2// Fix $bizrule in authe items
($bizrule will run by eval() in checkAccess)
//use _id as string, not MongoId object
$bizRule = 'return Yii::app()->user->id==(string)$params["User"]->_id;';
3// And user checkAccess to authorization
public function actionUpdate($id){
/**
* #var User $model
*/
$model=$this->loadModel($id);
$params = array('User'=>$model);
if (!Yii::app()->user->checkAccess('updateSelf', $params) )
{
throw new CHttpException(403, 'You are not authorized to perform this action');
}
//...
}
4// Done, now we can use checkAccess :D
First off, your original use of checkAccess was correct. Using Yii::app()->user->checkAccess() you are using the following definition:
http://www.yiiframework.com/doc/api/1.1/CWebUser#checkAccess-detail
Now, CWebUser's implementation of checkAccess calls CPHPAuthManager's implementation, which is where you encountered your problem with an illegal offset type.
http://www.yiiframework.com/doc/api/1.1/CPhpAuthManager#checkAccess-detail
An Illegal offset type means you are attempting to access an array element by specifying its key (also known as: offset) with a value that doesn't work as a key. This could be another array, an object, null, or possibly something else.
Your stack trace posted on the extensions page reveals that the following line gives the problem:
if(isset($this->_assignments[$userId][$itemName]))
So we have two possibilities for the illegal offset: $userId and $itemName.
Since $itemName is clearly a string, the problem must be with $userId.
(As a side note, the fact that your stack trace revealed surrounding code of this error also revealed that, at least for CPHPAuthManager, you are using a version of Yii that is prior to 1.1.11. Observe that lines 73 and 74 of https://github.com/yiisoft/yii/blob/1.1.11/framework/web/auth/CPhpAuthManager.php do not exist in your file's code.)
At this point I would have guessed that the problem is that the specified user is not logged in, and so Yii::app()->user->id is returning null. However, the new error you encountered when placing Yii::app()->user->id as the 2nd parameter of checkAccess reveals something else.
Since the 2nd parameter is in fact what should be the $params array that appears in your bizRule. Based on the error message, this means that Yii::app()->user->id is returning a mondoId type object.
I was unfamiliar with this type of object, so looked it up:
http://php.net/manual/en/class.mongoid.php
Long story short, you need to force Yii::app()->user->id to return the string value equivalent of this mondoId object. This likely set in your UserIdentity class in the components folder. To force it to be a string, simply place (string) to force a type conversion.
Example:
$this->_id = (string)$User->_id;
Your exact code will vary, based on what is in your UserIdentity class.
Then, restore your checkAccess to the signature you had before, and it should eliminate the Illegal offset error you encountered originally.
Note however that I have not used this extension, and while performing the following actions should fix this issue, it may cause new issues if the extension relies on the fact that Yii::app()->user->id is a mondoId object, and not a string.
ScalaForms
In the example linked here there is this example about form validation:
// You can also define ad-hoc constraints on the fields:
val loginForm = Form(
tuple(
"email" -> nonEmptyText,
"password" -> text
) verifying("Invalid user name or password", fields => fields match {
case (e, p) => User.authenticate(e,p).isDefined
})
)
Via binding errors some constraints are displayed in my form. (Like the nonEmptyText, that gets a extra line behind the field stating This field is required. See:
loginForm.bindFromRequest.fold(
formWithErrors => // binding failure, you retrieve the form containing errors,
value => // binding success, you get the actual value
)
If I do a .toString to the formWithErrors i get this for the nonEmptyText constraint:
Form(ObjectMapping2(<function2>,<function1>,(Email adress,FieldMapping(,List(Constraint(Some(constraint.required),WrappedArray())))),(Password,FieldMapping(,List(Constraint(Some(constraint.required),WrappedArray())))),,List(Constraint(None,List()))),Map(Password -> test, Email adress -> ),List(FormError(Email adress,error.required,WrappedArray())),None)
The latter part is a FormError List: List(FormError(Email adress,error.required,WrappedArray())),None) which is a case class: case class FormError (key: String, message: String, args: Seq[Any]) where key is defined as: The error key (should be associated with a field using the same key)..
The FieldConstructor picks this up and makes the 'Email adress' input box go red and add the error message ('This field is required').
Displaying the ad hoc constraints?
So when the form fields are all filled the username and password are checked:
verifying("Invalid user name or password", fields => fields match {
case (e, p) => User.authenticate(e,p).isDefined
})
But i dont know how to display the 'Invalid user name or password' to the user. The .toString of the FormError List of formWithErrors is:
List(FormError(,Invalid user name or password,WrappedArray())),None)
The Key part is empty.
Question
How do i display the ad hoc error?
I even tried:
BadRequest( views.html.test( formWithErrors ) ).flashing( "error" -> "Invalid user name or password." ) },
but for some reason its not working via the Flash either :(. This #flash.get("error").getOrElse("no 'error'") gives me 'no error' each time.
Actually, a form can have errors attached to it, so then when a bind from request fails validating constraint, a new form is created based on the given one, but filled in with errors.
Such errors list is composed of FormError which refers a Form Field and overrides toString to show its embed message.
This way if you wish to show all messages at once, you can simply formWithErrors.errors.mkString("<br>") for instance (in you template or flashing it).
There are a lot of ways and some common ones are described here http://www.playframework.org/documentation/2.0.2/ScalaFormHelpers. You might define your own field constructor as well.
To conclude, my advice would be that you should use helpers amap (for instance #inputText and co) because they already contain the logic to show helpers, tips and errors when available.
Edit
I missed the hint about the flash problem... you've probably forgotten to add (implicit flash: Flash) to you template param list. (don't forget to flag the request as implicit as well in your action definition)