I use unique entity validation in username property of user entity,
when the user is going to be added that is ok,
but when the user is going to be edited, and username is not changed the unique entity validation impede this, because the same object already exist with that username,
how can I handle this?
PD:sorry for my poor english
Use validations groups. Link your UniqueEntity constraint to a validation group "new", then only link this group to your form when you are creating a User.
Related
I am working on a user registration form in laravel 5. I wish to know is it possible to use laravel's form request validation to check if the two passwords submitted by the user are thesame. Is it possible to do that with using requests?
Yes, it's possible.
There is an validator called confirmed.
confirmed
The field under validation must have a matching field of
foo_confirmation. For example, if the field under validation is
password, a matching password_confirmation field must be present in
the input.
https://laravel.com/docs/5.2/validation#rule-confirmed
In CRM Online I need to link the Account Name field in the Account entity to a lookup table of imported and approved company names. The goal is to require users to pick from an approved list instead of letting them make up a company account name. I know there is an Account Name lookup in Contacts which uses existing Account Name records, but I need the lookup in the Account entity. Thanks for any tips.
If you are referring to a different entity as LookupTable, then create a relationship between Account ans the Lookup Table entity. After that you can use that entity as Lookup attribute in Account entity
CRM renders a Lookup control for a 1:N relationship. What you are trying achieve would fall more into a data validation scenario where users can enter account names from a pre-set list of names.
You can leverage a new event introduced in CRM 2016 - addOnKeyPress which can be easily used to build an autocomplete feature which would be more inline with what you are trying to achieve. If a user doesn't "pick" a value instead types anything in disregarding the autocomplete use attribute control's setNotification to set an error message which would invalidate the save preventing the user from saving the record.
Assume, you are trying to create a new user, with a User model ( using soft deletes ) having a unique rule for it's email address, but there exists a trashed user within the database.
When trying to validate the new user's data, you will get a validation error, because of the existing email.
I made some kind of extra validation within my Controllers, but wouldn't it be nice to have it all within the Model?
Would you suggest creating a custom validation rule?
As I haven't found a clean solution now, I am interessted in how others solved this problem.
You can validate passing extra conditions:
'unique:users,deleted_at,NULL'
This sounds like an issue with your business logic rather than a technical problem.
The purpose of a soft delete is to allow for the possibility that the soft-deleted record may be restored in the future. However, if your application needs uniqueness of email (which is completely normal), you wouldn't want to both create a new user with that email address and be able to restore the old one as this would contravene the uniqueness requirement.
So if there is a soft deleted record with the email address for which you are adding as a new record, you should consider instead restoring the original record and applying the new information to it as an update, rather than trying to circumvent the uniqueness check to create a new record.
This is the best approach
'email' => 'required|email|unique:users,email,NULL,id,deleted_at,NULL',
It give you this query
select count(*) as aggregate from `users` where `email` = ? and `deleted_at` is null
Laravel offers "Additional Where Clauses".
My url validation rule (from the update model method) looks like this:
$rules['url'] = 'required|unique:pages,url,'.$page->id.',id,deleted_at,NULL';
This means that the url must be unique, must ignore the current page and ignore the pages where deleted_at id not NULL.
Hope this helps.
Your Eloquent model should have the $softDeletes property set. If so, then when you perform a WHERE check, like User::where('username', 'jimbob'), Eloquent will automatically add in the query WHERE deleted_at IS NULL... which excludes soft deleted items.
In laravel 6.2+ you can use
'email' => ['required', Rule::unique('users')->whereNull('deleted_at')]
I have a model name "User", their I added a validation for login. But I need to validate registration page also. Fields for both forms are different.
Can someone please tell me how to manage different form validation with 1 model.
You can validate as many fields as you want inside your User model, it does not matter in which View or in which form you input them.
So just add the fields from your registration page to the User's $validate inside your User model.
If all forms share similar fieldnames but require different validation rules you can use:
http://bakery.cakephp.org/articles/dardosordi/2008/07/29/multivalidatablebehavior-using-many-validation-rulesets-per-model
If the duplicate fields validate the same on all forms you can just add them all to the Model, it will only validate the ones present on the form.
Remember to NOT use 'required' => true, setting this key to true will make the field always required and it has to be present in the data array even if it's not on your form
I use Entity framework for creating model.
I have table hierarchy where User is my base table and I have tables Lecturer and Student which have some specific data in them. Entity framework made model that it isn't suitable so I made my middle layer called modelview where I have student table which wraps all data from both user and student tables. I have made CRUD functionality for students but I only want that admin can create student with some initial password, than admin should not have option to change student password at Edit action.
The problem is that password is required field at student Create action so I set [Required] attribute to it, but at Edit I must not have password field and my ModelState.IsValid is then always false since password field in this case isn't present and therefore is null.
I thought on adding hidden password field, but that would be very bad since someone could look at page source and see password value.
Can I somehow once this field required and another time not required?
If you have any other idea that might help please share with me.
When a user is being edited, you can put in a placeholder hidden field, with the value * (or something like that). This will satisfy the Required attribute - you just have to make sure you don't update the password when the user is edited :)
Maybe you could randomly generate a password and not have it be required at all.
Or you could remove the requred attribute and manually check if it's present at the serverside.
if (string.IsNullOrEmpty(user.Password))
ModelState.AddModelError("Password","A password is required");
To validate clientside, if you're using jquery validation: JQuery Docs
If you have separate views for Student addition and editing, an alternative solution would be:
Create a StudentViewModel class which contains all the properties required for both Student addition and editing, and
Create a StudentAdditionViewModel class (optionally derived from StudentViewModel) which includes a Password property.
You can then use StudentAdditionViewModel as the Add view's model, and StudentViewModel as the edit view's model :)