Search code examples
laravelvalidationlaravel-5.3

validate : on of these fields must be a different value from the current one


I have a form which contains multiple text inputs , i want to check if at least one of these fields are changed from the current value before processing to the controller.

my form :

{!! Form::open(['url' => 'url']) !!}
   echo Form::text('street_name',$stree_name);
   echo Form::text('state',$state);
   echo Form::text('building_number',$building_number);
   echo Form::text('postal_code',$postal_code);
{!! Form::close() !!}

my vaidationRequest :

 public function rules()
    {

        return [
            FLD_PROFILES_CITY                       => 'max:50',
            FLD_PROFILES_STREET_NAME                => 'max:150',
            FLD_PROFILES_BUILDING_NUMBER            => 'max:50',
            FLD_PROFILES_POSTAL_CODE                => 'max:10',
        ];
    }

Solution

  • At your update controller method:

    $current_stored_item = Address::find($address_id); //you have to send $address_id using a hidden field
    
    //fill the item with the "new" data
    
    $current_stored_item->street_name = $request->street_name;
    
    //do it with all fields
    
    if (empty(array_diff_assoc($current_stored_item ->getOriginal()->forgetKey('updated_at'),$current_stored_item ->getAttributes()->forgetKey('updated_at'))){
       //there are no changes!
    }else{
      //there are changes!
    }