"Not" operator in validation rule - laravel

I have a custom validation rule, is_admin, that checks if a user is an administrator.
Does Laravel have an "opposite" operator (like how ! works in PHP), such that I can do something like not:is_admin, which would check that the user isn't an admin:
$rules = array(
'user_id' => 'required|numeric|not:is_admin'
);
$validator = Validator::make(Input::all(), $rules);
if ($validator->fails())
{
// return error
}
else
{
// continue
}
Thanks.

Yes we can by using conditional statements on rules array.
$rules is an array that we pass to validation class or define in Request class.
Example #1:
public function rules{
//here we return an array of rules like shown below.
return [
'field_a' => 'required',
'field_b' => 'required',
];
//we can add any operator by a little change.
save validation rules array in variable like shown below.
$rules = [
'field_a' => 'required',
'field_b' => 'required',
];
//now we can add any rule in $rules array using common ways of writing conditional statements.
//For example field_c is required only when field_a is present and field_b is not
if(isset($this->field_a) && !isset($this->field_b)){
$rules['field_c' => 'required'];
}
//we can apply any kind of conditional statement and add or remove validation rules on the basis of our business logic.
}
Example#2
public function rules(){
$rules = [];
if ($this->attributes->has('some-key')) {
$rules['other-key'] = 'required|unique|etc';
}
if ($this->attributes->get('some-key') == 'some-value') {
$rules['my-key'] = 'in:a,b,c';
}
if ($this->attributes->get('some-key') == 'some-value') {
$this->attributes->set('key', 'value');
}
return $rules;
}

Yes, you can validate it through required_if:field,value.
You can check more details at http://laravel.com/docs/5.0/validation#rule-required-if
Or you can use not_in:foo,bar.
You can check more details at http://laravel.com/docs/5.0/validation#rule-not-in

Related

Laravel avoid duplicate entry from model

I'm building a Laravel API. I have a models called Reservations. I want to avoid that a user creates two reservations for the same product and time period.
I have the following:
$reservation = Reservation::firstOrCreate([
'listing_id' => $request->listing_id,
'user_id_from' => $request->user_id_from,
'start_date' => $request->start_date,
'end_date' => $request->end_date,
]);
Edit after comments:
I'm also using validation
$validator = Validator::make($request->all(), [
'listing_id' => 'required|exists:listings,id',
'user_id_from' => 'required|exists:users,id',
'start_date' => 'required|date_format:"Y-m-d"|after:today',
'end_date' => 'required|date_format:"Y-m-d"|after:start_date'
]);
if ($validator->fails()) {
return response()->json(['error' => 'Validation failed'], 403);
}
Validation is working properly.
End of Edit
In my model I have casted the start_date and end_date as dates.
class Reservation extends Model
{
protected $fillable = ['listing_id', 'start_date', 'end_date'];
protected $dates = [
'start_date',
'end_date'
];
....
....
Documentation says:
The firstOrCreate method will attempt to locate a database record
using the given column / value pairs
However I notice that I'm still able to insert entries with the same attributes.
Any idea what I'm doing wrong or suggestions to fix it?
Probably there's a better way than this, but you can create an static method on Reservation to do this, like:
public static function createWithRules($data) {
$exists = $this->where('product_id', $data['product_id'])->whereBetween(*date logic that i don't remember right now*)->first();
if(!$exists) {
* insert logic *
} else {
* product with date exists *
}
}
So you can call Reservation::createWithRules($data)
You can achieve this using Laravel's built in ValidateRequest class. The most simple use-case for this validation, is to call it directly in your store() method like this:
public function store(){
$this->validate($request, [
'listing_id' => 'required|unique,
'start_date' => 'required|unique,
//... and so on
], $this->messages);
$reservation = Reservation::firstOrCreate([
'listing_id' => $request->listing_id,
'user_id_from' => $request->user_id_from,
'start_date' => $request->start_date,
'end_date' => $request->end_date,
]);
}
With this, you're validating users $request with by saying that specified columns are required and that they need to be unique, in order for validation to pass.
In your controller, you can also create messages function to display error messages, if the condition isn't met.
private $messages = [
'listing_id.required' => 'Listing_id is required',
'title.unique' => 'Listing_id already exists',
//... and so on
];
You can also achieve this by creating a new custom validation class:
php artisan make:request StoreReservation
The generated class will be placed in the app/Http/Requests directory. Now, you can add a few validation rules to the rules method:
public function rules()
{
return [
'listing_id' => 'required|unique,
'start_date' => 'required|unique,
//... and so on
];
}
All you need to do now is type-hint the request on your controller method. The incoming form request is validated before the controller method is called, meaning you do not need to clutter your controller with any validation logic:
public function store(StoreReservation $request)
{
// The incoming request is valid...
// Retrieve the validated input data...
$validated = $request->validated();
}
If you have any additional question about this, feel free to ask. Source: Laravel official documentation.

How to validate only part of a value I'm getting through a get request?

I want to get two values from a get request, and then I'll use explode to retrieve each of them separately. However, this means that the value I'm getting before using explode isn't unique to the table (since it's a compound, and therefore it's not an existing value in the table). How can use validate to avoid duplicate of it ?
I tried making the column unique using an SQL constraint, however this means I get an exception page when a user enters a duplicate. I would like to use validation instead so I get an error message.
public function store(Request $request)
{
$this->validate($request, [
'idProduit' => 'unique:stocks',
'produit' => 'required|unique:stocks',
'quantite' => 'required'
]);
$produit = explode("|",$request->get('produit'));
$stock = new Stock;
$stock->idProduit = $produit[0];
$stock->produit = $produit[1];
$stock->quantite = $request->input('quantite');
$stock->save();
return redirect('/stocks/')->with('success', 'Entrée de stock ajoutée à la base de données');
}
I don't actually get any error message, but it is allowing me to add duplicates in the database, which I know why : the value is a compound that doesn't exist in the database, and so the unique validation I wrote cannot catch duplicate on just a part of it.
You could create custom validation rule class or use Validator https://laravel.com/docs/5.8/validation#custom-validation-rules.
Something like that:
Validator::make($request->all(), [
'quantite' => 'required',
'produit' => [
'required',
function ($attribute, $value, $fail) {
$parts = explode('|', $value);
if (!Stock::where('idProduit', $parts[0])->exists()) {
$fail('idProduit already exists.');
}
if (!Stock::where('produit', $parts[1])->exists()) {
$fail('produit already exists.');
}
}
],
])->validate();
Easy way : Explode before validation and make new $request with 2 more fields . Thats Over !!
public function store(Request $request)
{
$produit = explode("|",$request->get('produit'));
$request->request->add(['idProduit' => $produit[0]]);
$request->request->add(['produit' => $produit[1]]);
$this->validate($request, [
'idProduit' => 'unique:stocks',
'produit' => 'required|unique:stocks',
'quantite' => 'required'
]);
$stock = new Stock;
$stock->idProduit = $produit[0];
$stock->produit = $produit[1];
$stock->quantite = $request->input('quantite');
$stock->save();
return redirect('/stocks/')->with('success', 'Entrée de stock ajoutée à la base de données');
}

How can i skip unique field in Yii2?

I have a unique field that I check when editing or adding a new training course. But, for some reason, when I enter a value in a field, it does not show me a hint that the field is already taken.
In addition, I need to do this: when I change the values and did not change this unique field, but left it as it is, then the validor should not swear that the field is already taken.
Thank.
InfCourses Model:
public function rules()
{
return [
[['name', 'short_description', 'price', 'favorite', 'active', 'course_order', 'link'], 'required'],
[['price', 'active'], 'integer'],
[['favorite'], 'string'],
[['name', 'short_description', 'link'], 'string', 'max' => 255],
[['active'], 'exist', 'skipOnError' => true, 'targetClass' => InfStatuses::className(), 'targetAttribute' => ['active' => 'id']],
[['course_order'], 'integer', 'min' => 1],
[
['course_order'], 'unique',
'targetAttribute' => ['course_order'],
'filter' => ['!=', 'id', Yii::$app->request->get('id')],
],
];
}
Validator in InfCoursesController:
public function actionValidate()
{
$model = new InfCourses();
if (Yii::$app->request->isAjax && $model->load(Yii::$app->request->post())) {
Yii::$app->response->format = Response::FORMAT_JSON;
return ActiveForm::validate($model);
}
}
Part of form code:
<?php $form = ActiveForm::begin([
'enableAjaxValidation' => true,
'validationUrl' => 'validate',
'options' => [
'data-pjax' => true,
]
]); ?>
Your validation is simply incorrect. You're using Yii::$app->request->get('id') in your rules, which is probably main source of your problems. Model should not access request or web user component directly - it breaks MVC pattern. Also putting values directly in rules in this way may give you unexpected results. You should check what query is generated by this validator, because it is hard to guess what is happening with such twisted rule.
But it may be easier to fix actionValidate() and distinguish between validating of new record and validating existing record:
public function actionValidate($id = null) {
if (empty($id)) {
$model = new InfCourses();
} else {
$model = $this->findModel($id);
}
if (Yii::$app->request->isAjax && $model->load(Yii::$app->request->post())) {
Yii::$app->response->format = Response::FORMAT_JSON;
return ActiveForm::validate($model);
}
}
Then you can limit your unique rule to:
[['course_order'], 'unique'],
Validator will be smart enough to detect that it is validating existing record and will not report unchanged fields values as duplicates. You just need to provide record ID in this action URL.
Well... I cut code below to each action: create/update.
if (Yii::$app->request->isAjax && $model->load(Yii::$app->request->post())) {
Yii::$app->response->format = Response::FORMAT_JSON;
return ActiveForm::validate($model);
}
Then remove validationUrl from form component. Inside model i make this rule [['course_order'],'unique']... Working fine...

unique value with custom request laravel 5.3

i have custom request in laravel ..
this is the code
public function rules()
{
if($this->ajax())
{
return [];
}
else
{
return
[
'username'=> 'required|min:3|max:30|unique:users',
'password'=> 'required|min:6',
'email'=>'required|min:3|max:35|unique:users',
'permission'=>'required',
'phone'=>'required',
'division'=>'required',
];
}
}
and i need to to ignore the current id from validation
i tried this
public function rules()
{
if($this->ajax())
{
return [];
}
else
{
return
[
'username'=> 'required|min:3|max:30|unique:users,id'.$this->id,
'password'=> 'required|min:6',
'email'=>'required|min:3|max:35|unique:users',
'permission'=>'required',
'phone'=>'required',
'division'=>'required',
];
}
}
but its ignoring the whole user name from validation not just the current id ..
Use auth()->user()->id instead of $this->id to get current user's ID.
Also, I'm not sure about the syntax you're using when trying to add ignoring ID. From unique() rule docs:
To instruct the validator to ignore the user's ID, we'll use the Rule class to fluently define the rule. In this example, we'll also specify the validation rules as an array instead of using the | character to delimit the rules:
use Illuminate\Validation\Rule;
Validator::make($data, [
'email' => [
'required',
Rule::unique('users')->ignore($user->id),
],
]);

How to exclude a perticular field from unique validation in edit mode in cakephp3.0 validation

I want to validate a field called survey_id which is an input from user for uniqueness. It is working properly and giving the correct response when adding the new record, but when I tried to edit this record it is giving an error [unique] => Provided value already exist. So what I want is to exclude the survey_id of the current record from uniqueness check and if user input some other value for survey_id it should check for uniqueness search.
Currently I am using the CakePHP 3.0 validation with on create validation. Here is the validation rule that I am using:
validator
->requirePresence('survey_id', __('msg_required'))
->notEmpty('survey_id', __('msg_required'))
->maxlength('survey_id', 32, __('msg_maxlength'))
->add('survey_id', 'unique', ['rule' => ['validateUnique',['id']], 'provider' => 'table', 'message' => 'Provided value already exist', 'on'=>'create']);
return $validator;
Is there anything wrong with this code?
Thanks in advance.
`
It will work with this validation rule
$validator
->requirePresence('survey_id', __('msg_required'))
->notEmpty('survey_id', __('msg_required'))
->maxlength('survey_id', 32, __('msg_maxlength'))
->alphaNumeric('survey_id', __('msg_surveyid_format'))
->add('survey_id', 'custom', [
'rule' => function ($value, $context) {
if (!empty($context['data']['projectId'])) { $values = array($context['data']['projectId']); } else { $values = array(); }
$data = $this->getSurveyId($value, $values);
return (!empty($data)) ? false : true;
},
'message' => __('msg_surveyid_exsist')]);
return $validator;
}
public function getSurveyId($surveyId = null, $exclude = null) {
$where = array('p.survey_id' => $surveyId);
if (!empty($exclude) && is_array($exclude)) {
$where[] = array('p.id NOT IN' => $exclude);
}
return $this->db->newQuery()
->select('*')
->from(['p' => 'projects'])
->where($where)
->execute()
->fetch('assoc');
}

Resources