i would like to do a custom Range Validator but i'm not sure how to proceed in this.
I need the Range validator to be active only if a boolean stay false, if the bool is true, the validation should not apply. Something like the RequiredIfFalse of foolproof validation.
[RequiredIfFalse("UnEquip", ErrorMessage = "Le champ 'Nombre de salariés' doit être renseigné")]
[Range(1, 1000000, ErrorMessage = "Le nombre de salariés doit être compris entre 1 et 1000000")]
public int Salaries { get; set; }
something like this, but combining those 2 type of validation.
Thanks in advance
You need to perform this validation at model level, because it implies several properties at once. Check this link, for a similar issue : Make the email field required based on the dropdown list value selection
In your case, Validate will test your boolean and Salaries value, if false and Salaries > 1000000 or Salaries < 1, you'll return a new ValidationResult containing your error message.
Related
I have fields in activity form for email. It contains "to, cc and bcc" fields that are all fields of the type PartyList
The question is: Can I only store entity values like contact or account or can I also just store a email address which is not associated to any contact or account in the system?
Here is a picture explaining what I'm trying to achieve
As per this article it appears that the answer is yes via the addressUsed field.
Entity email = _sdk.Retrieve("email", emailId, new ColumnSet("to"));
EntityCollection to = email.GetAttributeValue<EntityCollection>("to");
if (to != null)
{
to.Entities.ToList().ForEach(party =>
{
EntityReference partyId = party.GetAttributeValue<EntityReference>("partyid");
bool isDeleted = party.GetAttributeValue<bool>("ispartydeleted");
string addressUsed = party.GetAttributeValue<string>("addressused");
// Do something...
});
}
I need to show last saved record in crystal report.
I know I can do it with recordSelectionFormula , I tried too,
on report Preview form load event
if (ReportName == "Bill Receipt")
{
Report.RecordSelectionFormula = String.Format("{0} == (BillNo) AND {0} == (BillNO) ","{BILLH.Bh_no}" , BillNo);
}
BillNo is the variable gets value from property set on the form,its value is passed from another Billform
public int BillNo { get; set; }
but it showing exception
A number currency amount boolean date time or string is expected here
Please tell me if anything wrong with my code.
you are going wrong in code ,try this
if (ReportName == "Bill Receipt")
{
Report.RecordSelectionFormula = String.Format("{0} = {1} AND {2}={3} ", "{BILLH.Bh_no}", BillNo, "{BILL.B_no}", BillNo);
}
I have the next code that is now working fine but Im trying to validate the oldPassword, but ever appear the error message that it dont match.
Aspersoft\DirectorioBundle\Entity\User:
properties:
password:
- NotBlank: { groups: [change_password] }
- Length: { min: 3, max: 20, minMessage: "La cotraseña debe de contener minimo {{ limit }} caracteres de longitud.", maxMessage: "La cotraseña debe de contener maximo {{ limit }} caracteres de longitud.", groups: [change_password]}
oldPassword:
- Symfony\Component\Security\Core\Validator\Constraints\UserPassword:
groups: [change_password]
and in my entity class:
/**
* User
*
* #ORM\Table(name="user", indexes={#ORM\Index(name="user_index_1", columns={"id_municipality"})})
* #ORM\Entity(repositoryClass="Aspersoft\DirectorioBundle\Entity\UserRepository")
*/
class User implements AdvancedUserInterface, \Serializable {
public $oldPassword;
// more properties, getters and setters...
somebody knows what can be the problem? and how I would create the property OldPassword or maybe if it would be serialized in entity, In the symfony page there is poor information about Constraint UserPassword.
I found the solution.
I needed create the form from a Type to work properly with validation class.
In our application we have such a case:
Constraints should be evaluated in particular order. (cheap to expensive)
Constraints should not be evaluated after a violation per field.
All fields should be validated.
For first two, groupsequence is fitting very good. However for my 3rd requirement I could not find a way to solve.
public class AccountBean {
#CheepValidation
#ExpensiveValidation
#VeryExpensiveValidation
private String name;
#CheepValidation
#ExpensiveValidation
#VeryExpensiveValidation
private String surname
}
For example,
Let's say that, for name field VeryExpensiveValidationconstraint is violated and for surname field ExpensiveValidation constraint is violated.
For this case I should display:
For field name: Only VeryExpensiveValidation error message
For field surname: Only ExpensiveValidation error message
Note that for field surname we did not evaluate VeryExpensiveValidation constraint.
Is there a way to implement it with JSR 303?
Thanks
You can use groups and #GroupSequence, but it's a bit unwieldy.
public class AccountBean {
#CheapValidation(groups=Name1.class)
#ExpensiveValidation(groups=Name2.class)
#VeryExpensiveValidation(groups=Name3.class)
String name;
#CheapValidation(groups=Surname1.class)
#ExpensiveValidation(groups=Surname2.class)
#VeryExpensiveValidation(groups=Surname3.class)
String surname;
public interface Name1 {}
public interface Name2 {}
public interface Name3 {}
#GroupSequence({Name1.class, Name2.class, Name3.class})
public interface Name {}
public interface Surname1 {}
public interface Surname2 {}
public interface Surname3 {}
#GroupSequence({Surname1.class, Surname2.class, Surname3.class})
public interface Surname {}
}
Then validate with:
validator.validate(myAccountBean,
AccountBean.Name.class, AccountBean.Surname.class)
The key is to have two entirely independent group sequences.
Unfortunately, it seems you must explicitly list the groups for all the fields you want to validate. I wasn't able to get it working with a 'default' #GroupSequence. Can anyone improve on this?
I've implemented ordered validation with GroupSequence but, generally speaking, GroupSequence beans validation implementation is not transparent.
Meaning, untill first group is fully validated, you can not trigger the validation of the second group.
E.g.
I have 3 validated fields with custom validators. The idea is pretty straightforward: every field should be validated with the set of validators from top to bottom independently (descending cardinality).
#StringPropertyNotNullOrEmptyConstraint(message = "Group name is required", groups = {ValidationStep1.class})
private final StringProperty groupName;
#StringPropertyNotNullOrEmptyConstraint(message = "Group password is required", groups = {ValidationStep1.class})
#StringPropertyMatchConstraint(message = "The given password phrases do not match", dependentProperties = {"groupPasswordMatch"}, groups = {ValidationStep2.class})
private final StringProperty groupPassword;
#StringPropertyNotNullOrEmptyConstraint(message = "Group password match is required", groups = {ValidationStep1.class})
#StringPropertyMatchConstraint(message = "The given passwords phrases do not match", dependentProperties = {"groupPassword"}, groups = {ValidationStep2.class})
private final StringProperty groupPasswordMatch;
public interface ValidationStep1 {
}
public interface ValidationStep2 {
}
#GroupSequence({GroupDialogModel.class, ValidationStep1.class, ValidationStep2.class})
public interface GroupDialogModelValidationSequence {
}
ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory();
Validator validator = validatorFactory.getValidator();
Set<ConstraintViolation<GroupDialogModel>> constraintViolations = validator.validate(this, GroupDialogModelValidationSequence.class);
The caveat of this approach is that each field should go through ValidationStep1 first and only after each validation of step 1 succeeds it goes to step 2. For example, even if password fields are not empty, but contain different values, validation for them succeeds if group name field does not contain any value. And only after I enter some value to the group name, ValidationStep1 group succeeds and then it displays validation result of ValidationStep2 (passwords do not match).
Making each group for each field in every sequence is bad practice IMHO, but it seems like there is no other choice.
Any other solution is much appreciated.
I have an object Customer with a 1-n relationship to Addresses.
I want to be able to take the first address. So i create a method:
public Address firstAddress
{
get
{
var f=from d in this.Addresses
select d;
return f;
}
}
I get the following error :
Error 5 Impossible to find an implementation ofsource 'System.Data.Objects.DataClasses.EntityCollection'. 'Select' introuvable. Une référence à 'System.Core.dll' ou une directive using pour 'System.Linq' est-elle manquante ?
I do not undertand why i can't query the collection of addresses...
Thanks
John
Well, the error message tells you where to start looking, presuming you can read French. :) Make sure your app has a reference to the System.Core assembly and your code file has using System.Linq; at the top.
Also, I think the query is wrong. I'm presuming that this.Addresses is an enumeration of the type Address. In this case you'd need:
public Address firstAddress
{
get
{
var f=(from d in this.Addresses
select d).FirstOrDefault();
return f;
}
}