Previously, I was not using $model->save() function for inserting or updating any data. I was simply using createCommand() to execute query and it was working successfully. But, my team members asked me to avoid createCommand() and use $model->save();
Now, I started cleaning my code and problem is $model->save(); not working for me. I don't know where i did mistake.
UsersController.php (Controller)
<?php
namespace app\modules\users\controllers;
use Yii;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\swiftmailer\Mailer;
use yii\filters\AccessControl;
use yii\web\Response;
use yii\widgets\ActiveForm;
use app\modules\users\models\Users;
use app\controllers\CommonController;
class UsersController extends CommonController
{
.
.
public function actionRegister() {
$model = new Users();
// For Ajax Email Exist Validation
if(Yii::$app->request->isAjax && $model->load(Yii::$app->request->post())){
Yii::$app->response->format = Response::FORMAT_JSON;
return ActiveForm::validate($model);
}
else if ($model->load(Yii::$app->request->post())) {
$post = Yii::$app->request->post('Users');
$CheckExistingUser = $model->findOne(['email' => $post['email']]);
// Ok. Email Doesn't Exist
if(!$CheckExistingUser) {
$auth_key = $model->getConfirmationLink();
$password = md5($post['password']);
$registration_ip = Yii::$app->getRequest()->getUserIP();
$created_at = date('Y-m-d h:i:s');
$model->auth_key = $auth_key;
$model->password = $password;
$model->registration_ip = $registration_ip;
$model->created_at = $created_at;
if($model->save()) {
print_r("asd");
}
}
}
}
.
.
}
Everything OK in this except $model->save(); Not printing 'asd' as i echoed it.
And, if i write
else if ($model->load(Yii::$app->request->post() && $model->validate()) {
}
It's not entering to this if condition.
And, if i write
if($model->save(false)) {
print_r("asd");
}
It insert NULL to all columns and print 'asd'
Users.php (model)
<?php
namespace app\modules\users\models;
use Yii;
use yii\base\Model;
use yii\db\ActiveRecord;
use yii\helpers\Security;
use yii\web\IdentityInterface;
use app\modules\users\models\UserType;
class Users extends ActiveRecord implements IdentityInterface
{
public $id;
public $first_name;
public $last_name;
public $email;
public $password;
public $rememberMe;
public $confirm_password;
public $user_type;
public $company_name;
public $status;
public $auth_key;
public $confirmed_at;
public $registration_ip;
public $verify_code;
public $created_at;
public $updated_at;
public $_user = false;
public static function tableName() {
return 'users';
}
public function rules() {
return [
//First Name
'FirstNameLength' => ['first_name', 'string', 'min' => 3, 'max' => 255],
'FirstNameTrim' => ['first_name', 'filter', 'filter' => 'trim'],
'FirstNameRequired' => ['first_name', 'required'],
//Last Name
'LastNameLength' => ['last_name', 'string', 'min' => 3, 'max' => 255],
'LastNameTrim' => ['last_name', 'filter', 'filter' => 'trim'],
'LastNameRequired' => ['last_name', 'required'],
//Email ID
'emailTrim' => ['email', 'filter', 'filter' => 'trim'],
'emailRequired' => ['email', 'required'],
'emailPattern' => ['email', 'email'],
'emailUnique' => ['email', 'unique', 'message' => 'Email already exists!'],
//Password
'passwordRequired' => ['password', 'required'],
'passwordLength' => ['password', 'string', 'min' => 6],
//Confirm Password
'ConfirmPasswordRequired' => ['confirm_password', 'required'],
'ConfirmPasswordLength' => ['confirm_password', 'string', 'min' => 6],
['confirm_password', 'compare', 'compareAttribute' => 'password'],
//Admin Type
['user_type', 'required'],
//company_name
['company_name', 'required', 'when' => function($model) {
return ($model->user_type == 2 ? true : false);
}, 'whenClient' => "function (attribute, value) {
return $('input[type=\"radio\"][name=\"Users[user_type]\"]:checked').val() == 2;
}"], #'enableClientValidation' => false
//Captcha
['verify_code', 'captcha'],
[['auth_key','registration_ip','created_at'],'safe']
];
}
public function attributeLabels() {
return [
'id' => 'ID',
'first_name' => 'First Name',
'last_name' => 'Last Name',
'email' => 'Email',
'password' => 'Password',
'user_type' => 'User Type',
'company_name' => 'Company Name',
'status' => 'Status',
'auth_key' => 'Auth Key',
'confirmed_at' => 'Confirmed At',
'registration_ip' => 'Registration Ip',
'confirm_id' => 'Confirm ID',
'created_at' => 'Created At',
'updated_at' => 'Updated At',
'verify_code' => 'Verification Code',
];
}
//custom methods
public static function findIdentity($id) {
return static::findOne($id);
}
public static function instantiate($row) {
return new static($row);
}
public static function findIdentityByAccessToken($token, $type = null) {
throw new NotSupportedException('Method "' . __CLASS__ . '::' . __METHOD__ . '" is not implemented.');
}
public function getId() {
return $this->id;
}
public function getAuthKey() {
return $this->auth_key;
}
public function validateAuthKey($authKey) {
return $this->auth_key === $auth_key;
}
public function validatePassword($password) {
return $this->password === $password;
}
public function getFirstName() {
return $this->first_name;
}
public function getLastName() {
return $this->last_name;
}
public function getEmail() {
return $this->email;
}
public function getCompanyName() {
return $this->company_name;
}
public function getUserType() {
return $this->user_type;
}
public function getStatus() {
return $this->status;
}
public function getUserTypeValue() {
$UserType = $this->user_type;
$UserTypeValue = UserType::find()->select(['type'])->where(['id' => $UserType])->one();
return $UserTypeValue['type'];
}
public function getCreatedAtDate() {
$CreatedAtDate = $this->created_at;
$CreatedAtDate = date('d-m-Y h:i:s A', strtotime($CreatedAtDate));
return $CreatedAtDate;
}
public function getLastUpdatedDate() {
$UpdatedDate = $this->updated_at;
if ($UpdatedDate != 0) {
$UpdatedDate = date('d-m-Y h:i:s A', strtotime($UpdatedDate));
return $UpdatedDate;
} else {
return '';
}
}
public function register() {
if ($this->validate()) {
return true;
}
return false;
}
public static function findByEmailAndPassword($email, $password) {
$password = md5($password);
$model = Yii::$app->db->createCommand("SELECT * FROM users WHERE email ='{$email}' AND password='{$password}' AND status=1");
$users = $model->queryOne();
if (!empty($users)) {
return new Users($users);
} else {
return false;
}
}
public static function getConfirmationLink() {
$characters = 'abcedefghijklmnopqrstuvwxyzzyxwvutsrqponmlk';
$confirmLinkID = '';
for ($i = 0; $i < 10; $i++) {
$confirmLinkID .= $characters[rand(0, strlen($characters) - 1)];
}
return $confirmLinkID = md5($confirmLinkID);
}
}
Any help is appreciable. Please Help me.
It could be a problem related with your validation rules.
Try, as a test, to save the model without any validation in this way:
$model->save(false);
If the model is saved you have conflict with your validation rules. Try selectively removing your validation rule(s) to find the validation conflict.
If you have redefined the value present in active record you don't assign the value to the var for db but for this new var and then are not save.
Try removing the duplicated var.. (only the vars non mapped to db should be declared here.)
I guess $model->load() returns false, call $model->errors to see model's error.
$model->load();
$model->validate();
var_dump($model->errors);
Check model saving error like this :
if ($model->save()) {
} else {
echo "MODEL NOT SAVED";
print_r($model->getAttributes());
print_r($model->getErrors());
exit;
}
As #scaisEdge suggest, try removing all table related field in your Users class
class Users extends ActiveRecord implements IdentityInterface
{
/* removed because this properties is related in a table's field
public $first_name;
public $last_name;
public $email;
public $password;
public $user_type;
public $company_name;
public $status;
public $auth_key;
public $confirmed_at;
public $registration_ip;
public $verify_code;
public $created_at;
public $updated_at;
public $user_type;
public $company_name;
public $status;
public $auth_key;
public $confirmed_at;
public $registration_ip;
public $verify_code;
public $created_at;
public $updated_at;
*/
// this is properties that not related to users table
public $rememberMe;
public $confirm_password;
public $_user = false;
public static function tableName() {
return 'users';
}
/* ........... */
}
The other solution mentioned $model->save(false);. That is just a temporary workaround, and you should still find the actual reason why the save functionality is not working.
Here are additional steps to help diagnose the actual issue:
check that _form input field has the proper name, and
check that if you have added any dropdown functionality, then check whether it's working properly or not
And there maybe another reason of not saving model - you have property of your Users class and before saving from form its reset to NULL.
So, if you set $model->saveAttributes('favorite_book'=>$model->favorite_book), but at that time you declared in class Users public $favorite_book - you will get this field empty in DB.
You are doing all stuff correctly. I think you must add one line for confirm password validation
if(!$CheckExistingUser) {
$auth_key = $model->getConfirmationLink();
$password = md5($post['password']);
$registration_ip = Yii::$app->getRequest()->getUserIP();
$created_at = date('Y-m-d h:i:s');
$model->auth_key = $auth_key;
$model->password = $password;
$model->confirm_password= md5($post["confirm_password"]); /// add this line
$model->registration_ip = $registration_ip;
$model->created_at = $created_at;
And Also after this condition check model attributes and error like this :
if($model->save()) {
print_r("asd");
}else{
var_dump($model);exit;}
Try this:
$model->save(false);
and if thats working, check your model rules() and your form rules() if its
having the same rules. usually the cause is the required fields in your table.
if your column type in your table is "integer" and your data is "string" you may see tis error.You should check your data type and try again.
I suppose that your column type is integer, you should write the following code:
$model->created_at=time();//1499722038
$model->save();
but your column type is string, you should write the following code:
$model->created_at=date('d/m/Y');//11/07/2017
$model->save();
in your model i found First name , last name , email , password is required fields and in your controller you are updating or saving only
$model->auth_key = $auth_key;
$model->password = $password;
$model->confirm_password= md5($post["confirm_password"]); /// add this line
$model->registration_ip = $registration_ip;
$model->created_at = $created_at;
but first name and last name and email id are required so it will throw validation error , to check this error use
$model->load();
$model->validate();
var_dump($model->errors);
it will show you the error . correct that errors then model will get save.
you can solve that error using Scenario or
$model->saveAttributes('favorite_book'=>$model->favorite_book,'favorite_movie'=>$model->favorite_movie);
I hope it will help you.
Related
I am trying to setup my policy for users. However I keep on getting an error of:
Too few arguments to function App\Policies\UserPolicy::update(), 1 passed in /vendor/laravel/framework/src/Illuminate/Auth/Access/Gate.php on line 481 and exactly 2 expected (View: /resources/views/users/index.blade.php)
ErrorException /app/Policies/UserPolicy.php 20
which is on the UserPolicy#update function
When I am logged in as super_admin, it works fine but it throws this error whenever I am logged in as a user of different role.
Below is my current implementation:
UserPolicy
class UserPolicy
{
use HandlesAuthorization;
public function update(User $user, User $userEdit) {
if ($user->id == $userEdit->id) {
return true;
}
return $user->can('update_user');
}
public function before($user, $ability) {
if ($user->hasRole('super_admin')) {
return true;
}
}
}
UsersController
class UsersController extends Controller {
public function __construct() {
$this->middleware('auth');
}
public function edit(User $user) {
$this->authorize('update', $user);
return view('users.edit', [
'user' => User::with('roles', 'level')->find($user->id),
'surveys' => \App\Survey::all(),
]);
}
public function update(UserRequest $request, User $user) {
$this->authorize('update', $user);
$request->save();
session()->flash('success', 'User successfully updated');
// means user is editing his own profile
if (auth()->id() == $user->id) {
return redirect('/dashboard');
} else {
return redirect('/users');
}
}
}
UserRequest
class UserRequest extends FormRequest {
public function authorize() {
return true;
}
public function rules() {
switch ($this->method()) {
case 'POST':
return [
'name' => 'required|string',
'email' => 'required|string|email|max:255|unique:users',
'role' => 'required|exists:roles,id',
'level' => 'required|string',
];
break;
case 'PATCH':
return [
'name' => 'required|string|max:255',
'email' => 'required|string|email|max:255|unique:users,email,'.$this->user->id,
'role' => 'sometimes|exists:roles,id',
'level' => 'sometimes|string',
'password' => 'nullable|sometimes|string|min:6|confirmed'
];
break;
default:
break;
}
}
public function save() {
switch (request()->method()) {
case 'POST':
$this->createUser();
break;
case 'PATCH':
$this->updateUser();
break;
default:
break;
}
}
protected function createUser() {
// random generate password
$password = str_random(8);
$user = User::create([
'name' => request('name'),
'email' => request('email'),
'level_id' => request('level'),
'password' => Hash::make($password),
]);
$user->assignRoleById(request('role'));
Mail::to($user)->send(new WelcomeMail($user, $password));
}
protected function updateUser() {
$user = User::findOrFail($this->user->id);
$user->name = request('name');
$user->email = request('email');
if (request('password') != '') {
$user->password = Hash::make(request('password'));
}
if (request('level') != '') {
$user->level_id = request('level');
}
$user->update();
if (request('role') != '') {
$user->roles()->sync([request('role')]);
}
}
}
AuthServiceProvider
class AuthServiceProvider extends ServiceProvider
{
/**
* The policy mappings for the application.
*
* #var array
*/
protected $policies = [
\App\User::class => \App\Policies\UserPolicy::class,
];
/**
* Register any authentication / authorization services.
*
* #return void
*/
public function boot()
{
$this->registerPolicies();
foreach ($this->getPermissions() as $permission) {
Gate::define($permission->name, function($user) use ($permission) {
return $user->hasRole($permission->roles);
});
}
}
protected function getPermissions() {
return Permission::with('roles')->get();
}
}
In my views file I'm calling
#can('update', App\User::class)
<!-- html code --!>
#endcan
instead of
#can('update', $user)
<!-- html code --!>
#endcan
I was not passing the user instance into the function which was causing the error.
In UserRequest you haven't given any parameters when you call $user->update();. The update() function requires for a UserRequest instance, as well as a User.
Give this a try: $user->update(request()->all(), $user)
Edit:
I would just move the following...
$this->authorize('update', $user);
$request->save();
session()->flash('success', 'User successfully updated');
// means user is editing his own profile
if (auth()->id() == $user->id) {
return redirect('/dashboard');
} else {
return redirect('/users');
}
...to the updateUser() function.
I am new to YII. I know this question has been asked a lot of times before nut i can really find an answer. So, please take a look.
I am saving some data in the database from my PaypalController.php
Here is the code of PaypalController.
$transid = $paymentResult['TRANSACTIONID'];
$profileid = $recurringResult['PROFILEID'];
/*var_dump($transid);
var_dump($profileid);
exit;*/
$payment = new Payment;
$payment->user_id = 2;
$payment->feature = 'import sending servers';
$payment->transaction_id = $transid;
$payment->amount = 10;
$payment->profile_id = $profileid;
if($payment->save()){
//var_dump($recurringResult);exit;
//echo "<script>alert('Your payment is succesful');</script>";
$this->redirect('../email-hustler/index');
}else{
print_r($payment->getErrors());
exit;
}
And here is the code of my Payment model
public $user_id;
public $feature;
public $transaction_id;
public $amount;
public $profile_id;
public $is_active;
public function tableName()
{
return '{{payment}}';
}
public function rules()
{
$rules = array(
array('user_id, feature, transaction_id, amount, profile_id, is_active', 'safe'),
);
return CMap::mergeArray($rules, parent::rules());
}
public function attributeLabels(){
return array(
'user_id' => 'user_id',
'feature' => 'feature',
'transaction_id' => 'transaction_id',
'amount' => 'amount',
'profile_id' => 'profile_id',
'is_active' => 'is_active',
);
}
public static function model($className=__CLASS__)
{
return parent::model($className);
}
Now the problem is the value of the variables is not saving but the value i am passing static are saved.
var_dump of variables is giving me correct output.
Please help.
I have 3 classes Persona, Alumno and factura. i need to show the person fullname on factura view using the gridview and the search function. The fullname is in Persona. Factura is only related to alumno through id_alumno and alumno is related to persona through id_persona.
i try to solved this by creating a new variable fullname and a function to get persona on class factura.
class Factura extends \yii\db\ActiveRecord{
public $full_name;
}
public static function tableName()
{
return 'factura';
}
public function rules()
{
return [
[['id_factura_reemplazo', 'id_obra_social', 'id_alumno',], 'integer'],
[['id_obra_social', 'id_alumno'], 'required'],
[['fecha_factura','fullname','id_persona'], 'safe'],
];
}
public function attributeLabels()
{
return [
'id_factura' => 'Id Factura',
'id_factura_reemplazo' => 'Id Factura Reemplazo',
'id_obra_social' => 'Id Obra Social',
'id_alumno' => 'Id Alumno',
'fullName'=>Yii::t('app', 'Nombre y Apellido'),
];
}
public function getIdAlumno()
{
return $this->hasOne(Alumno::className(), ['id_alumno' => 'id_alumno']);
}
public function getIdPersona() {
return $this->hasOne(Persona::className(), ['id_persona' => 'id_persona'])
->via('idAlumno');
}
public function getFullname(){
if($this->idPersona)
return $this->idPersona->nombre." ".$this->idPersona->apellido;
return null;
}
I get:
Invalid Call – yii\base\InvalidCallException
Setting read-only property: app\models\Facturasearch::fullName
if (method_exists($this, 'get' . $name)) {
throw new InvalidCallException('Setting read-only property: ' . get_class($this) . '::' . $name);
} else {
throw new UnknownPropertyException('Setting unknown property: ' . get_class($this) . '::' . $name);
}
it's like it doesnt have the getter... the problem jump from the line when it tries to do $this->load($params);
class Facturasearch extends Factura{
public function rules()
{
return [
[['id_factura', 'id_factura_reemplazo', 'id_alumno', 'numero'], 'integer'],
[['id_obra_social','fecha_factura', 'observacion','id_alumno','fullName','id_persona'], 'safe'],
];
}
public function scenarios()
{
// bypass scenarios() implementation in the parent class
return Model::scenarios();
}
public function search($params)
{
$query = Factura::find();
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$this->load($params);
...
You should add a pubblic var in FacturaSearch for fullname
class Facturasearch extends Factura{
public $fullname;
public function rules()
{
return [
[['id_factura', 'id_factura_reemplazo', 'id_alumno', 'numero'], 'integer'],
[['id_obra_social','fecha_factura', 'observacion','id_alumno','fullName','id_persona'], 'safe'],
];
}
public function scenarios()
{
// bypass scenarios() implementation in the parent class
return Model::scenarios();
}
public function search($params)
{
$query = Factura::find();
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$this->load($params);
...
Hi I take user data to two models If the user clicks the checkbox (company) it show him the additional data that needs to complete. I needs to work on scenario if checbox = 1 the data fields of the form must be passed. It is my action from the controller:
public function actionCreate() {
$model = new UrUserForm();
$userDate = new UserDataForm();
$model->scenario = 'create';
if (($userDate->load(Yii::$app->request->post()) && $userDate->validate() && $model->load(Yii::$app->request->post()) && $model->validate()) || $model->load(Yii::$app->request->post()) && $model->validate()) {
if ($userDate->IsCompany()) {
$userDate->scenario = 'setFirm';
} else {
$userDate->scenario = 'notFirm';
$userDate->clearData();
}
var_dump($userDate->scenario);
exit();
$userDate->saveOptionalData();
$model->RoyalUserData=$userDate->data['Id'];
$model->saveUser();
Yii::$app->session->setFlash('success', 'Użytkownik został dodany');
return $this->redirect(['index']);
} else {
return $this->render('create', [
'model' => $model,
'userDate' => $userDate
]);
}
}
An my model:
<?php
namespace backend\modules\users\models;
use common\models\UserData;
use frontend\modules\settings\models\Profile;
use yii\base\Model;
use Yii;
class UserDataForm extends Model
{
public $Address;
public $NIP;
public $CompanyName;
public $Website;
public $Phone;
public $IsCompany;
public $IsPhoneConfirmed;
public $CreatedAt;
public $UpdateAt;
public $Rel_State;
public $Rel_Currency;
public $IsDeleted;
public $data;
public function rules()
{
return [
[['Address', 'Phone', 'Rel_State', 'Rel_Currency','IsCompany'], 'safe', 'on' => 'notFirm'],
[['Address', 'Phone', 'Rel_State', 'Rel_Currency','IsCompany'], 'required', 'on' => 'setFirm'],
[['NIP','IsCompany', 'Phone', 'IsPhoneConfirmed', 'CreatedAt', 'UpdateAt', 'Rel_State', 'Rel_Currency', 'IsDeleted'], 'integer'],
[['Address', 'CompanyName', 'Website'], 'string', 'max' => 45],
[['Phone'], 'common\components\validators\PhoneValidator'],
[['NIP'], 'common\components\validators\NipValidator'],
['IsCompany', 'safe']
];
}
public function scenarios()
{
$scenarios = parent::scenarios();
$scenarios['setFirm'] = ['Address', 'Phone', 'Rel_State', 'Rel_Currency','IsCompany'];
$scenarios['notFirm'] = ['Address', 'Phone', 'Rel_State', 'Rel_Currency','IsCompany'];
return $scenarios;
}
public function saveOptionalData() {
$model = new UserData();
$model->Address=$this->Address;
$model->Phone=$this->Phone;
$model->Rel_State=$this->Rel_State;
$model->Rel_Currency= $this->Rel_Currency;
$model->NIP=$this->NIP;
$model->IsCompany = $this->IsCompany;
$model->IsPhoneConfirmed = $this->IsPhoneConfirmed;
$model->CompanyName = $this->CompanyName;
$model->Website = $this->Website;
$this->data=$model;
if ($model->validate() && $model->save()) {
return $model;
}
return false;
}
public function clearData() {
$this->Address = NULL;
$this->Phone = NULL;
$this->Rel_State = NULL;
$this->Rel_Currency = NULL;
$this->NIP = NULL;
$this->IsCompany = NULL;
$this->IsPhoneConfirmed = NULL;
$this->CompanyName = NULL;
$this->Website = NULL;
}
public function IsCompany() {
if ($this->IsCompany == 1) {
return true;
}
return false;
}
}
I read the documentation but it does not help me. In the create action I created
var_dump($userDate->scenario);
exit();
which indicates that there is everything okay because when checkobox is off vardump spits: string (7) "notFirm" and when he's on spits: string (7) "setFirm." I do not know where the fault but each time validation is safe, that should work that if checkbox is on data from rules(addres, phone) should be required. Anyone see my bad and can help me?
I hope you have found an answer, but in case you haven't here's one. You're setting the scenario after you validate the data. Scenarios must be set before you have run the validation in order to use different validation rules.
In your code you have
if ($userDate->IsCompany()) {
$userDate->scenario = 'setFirm';
} else {
$userDate->scenario = 'notFirm';
$userDate->clearData();
}
But in the first if in your code you have already validated
if (($userDate->load(Yii::$app->request->post()) && $userDate->validate() ...
In order to use a scenario I suggest the following:
$userDate->load(Yii::$app->request->post();//load data into model
if ($userDate->IsCompany()) {//check if company was set and is equal to 1
$userDate->scenario = 'setFirm';
} else {
$userDate->scenario = 'notFirm';
}
if($userDate->validate()...)//Validation code according to the scenario
need help updating a unique rule in my validation rules. I have a abstract validator that will validate a rules before storing into my database and in the rules array I set the email to be unique when creating or registering a user but when updating the user the enique email should not validate if the email is owned by the user.
abstract class Validator
abstract class Validator {
protected $errors;
protected $attributes;
public function __construct($attributes = null)
{
$this->attributes = $attributes ?: \Input::all();
}
public function passes()
{
$validation = \Validator::make($this->attributes, $this->rules());
if ($validation->passes()) return true;
$this->errors = $validation->messages();
return false;
}
public function getErrors()
{
return $this->errors;
}
}
Validation Rules(UserRule.php)
use MyCustomValidatorNamespaceHere....
class UserRules extends Validator
public function rules()
{
return [
'email' => 'required|email|unique:users,email,id',
...
];
}
and in my UserController I injected the UserRule in the constractor. (UserRule $userRule). Here is the code in the update method.
public function update($id)
{
$if ($this->userRule->passes())
{
$this->user->find($id)->update(Input::all());
return .........
}
}
But the validation always fail and displaying the error that the email is already taken. Please help guys.
The problem is your rule. When you update, you need to use unique that doesn't check record you update. So you should have:
unique:users,email,id
but for example:
unique:users,email,10
if you edit record with id 10.
What you could do is to define this rule:
'email' => 'required|email|unique:users,email,{id}',
and your passes method:
public function passes($id = null)
{
$rules = $this->rules();
$rules['email'] = str_replace('{id}', $id, $rules['email']);
$validation = \Validator::make($this->attributes, $rules);
if ($validation->passes()) return true;
$this->errors = $validation->messages();
return false;
}
and now in update rule use:
if ($this->userRule->passes($id))
By the way you have error in $if ($this->userRule->passes()) - it should be if and not $if
You can use the route method inside your request class to except an id from the validation
public function rules()
{
return [
'email' => 'required|email|unique:users,email,'.$this->route('user'),
...
];
}
I had a problem like that before and it was difficult to find an answer. Here is what I did.
class UserRules extends Validator {
public function __construct($input = NULL) {
$this->input = $input ?: \Input::all();
if(isset($this->input['id'])):
self::$rules['username'] = 'required|unique:users,username,'.$this->input['id'];
self::$rules['email'] = 'required|email|unique:users,email,'.$this->input['id'];
else:
self::$rules['username'] = 'required|unique:users';
self::$rules['email'] = 'required|email|unique:users';
endif;
}
public static $rules = array(
'company_id' => 'required',
'role' => 'required',
'password' => 'sometimes|required|confirmed'
);
}
You need to use self:: because $rules is static.
I have moved this function:
public function passes($id)
{
$rules = static::$rules;
$rules['username'] = str_replace('{id}', $id, $rules['username']);
$rules['email'] = str_replace('{id}', $id, $rules['email']);
$validation = \Validator::make($this->attributes, $rules);
if($validation->passes()) return true;
$this->errors = $validation->messages();
return false;
}
into UserRule.php and commented the same function in abstract class Validator
Now updating is working.
I solved this problem here on stackoverflow in a generic way. It will automatically adapt your rules if you do an update and add exceptions to your :unique if necessary.