Update the database using Form Builder $builder->saveForm()

srinivas214 - 1 month ago

Hi, I have an issue cannot figure out. With out using Form builder render, I was calling the custom view to load the form and providing the edit mode to the users. When i save the form $builder->saveForm() method, it is always storing as new record in database when i submit the form. I want to update the database with the data instead of adding new record.

Please help me to fix this. Thanks!

cleancookie - 1 month ago

I've never tried using ->saveForm but I assume it will create a new entry every time if you don't also post the ID. If you don't already, you need a hidden field that contains the id of the thing you want to update

ryanthompson - 1 month ago

Can you show us some code? Be sure to set the entry being edited on the form. For example:

$builder->setEntry(auth()->user());

srinivas214 - 1 month ago

This is my custom form builder. I have to extend users module to add stores king of functionality. So i have extended User Form builder to add some more fields to generate in the form but the store relate d fields I am storing in separate table
public function edit(UserModel $users, StoreModel $stores, $id) { if (!$user = $users->find($id) && $store = $stores->where('user_id', $id)->first()) { abort(404); } $user = DB::table('users_users')->where('id', $id )->first(); $storedata = array('user' => $user, 'store' => $store); return $this->view->make('hcw.module.hcwusers::admin/store/edit', compact('storedata'));

namespace Hcw\HcwusersModule\Store\Form;

use Anomaly\UsersModule\User\Form\UserFormBuilder; use Anomaly\UsersModule\User\Validation\ValidatePassword;

/**

  • class StoreFormBuilder
  • Defines the Store Management features in Admin Panel */ class StoreFormBuilder extends UserFormBuilder { /*
    • @var type $model Assign the User model to use */ protected $model = 'Anomaly\UsersModule\User\UserModel'; /**
    • The form handler.
    • @var null|string */ protected $handler = \Hcw\HcwusersModule\Store\Form\StoreFormHandler::class; /**
    • @var type $fields Define the store Form fields */ protected $fields = [ 'store_name' => [ 'type' => 'anomaly.field_type.text', 'label' => 'Store Name' ], 'store_owner' => [ 'type' => 'anomaly.field_type.text', 'label' => 'Store Owner' ], 'secret_code' => [ 'type' => 'anomaly.field_type.text', 'label' => 'Secret Code', ], 'phone_number' => [ 'type' => 'anomaly.field_type.text', ], 'city' => [ 'type' => 'anomaly.field_type.select', 'label' => 'City', 'class' => 'store-city custom-select form-control', 'config' => [ "options" => [], "default_value" => null, "mode" => "dropdown", "handler" => \App\Store\CityOptions::class ] ], 'township' => [ 'type' => 'anomaly.field_type.select', 'label' => 'Township', 'class' => 'store-township custom-select form-control', 'config' => [ "options" => [], "default_value" => null, "mode" => "dropdown", "handler" => \App\Store\TownshipOptions::class ] ], 'address' => [ 'type' => 'anomaly.field_type.textarea', 'label' => 'Address', ], 'password' => [ 'type' => 'anomaly.field_type.text', 'instructions' => '1. Minumum length is 8.
      1. The password must contain at least one integer.
      2. The password must contain at least one lowercase letter.
      3. The password must contain at least one uppercase letter.
      4. The password must contain at least one special character.', 'value' => '', 'placeholder' => 'Password', 'config' => [ 'type' => 'password', ], 'rules' => [ 'valid_password', ], 'validators' => [ 'valid_password' => [ 'message' => false, 'handler' => ValidatePassword::class, ], ], ],
        ];

namespace Hcw\HcwusersModule\Store\Form;

use Anomaly\UsersModule\User\UserModel; use Anomaly\Streams\Platform\Message\MessageBag; use Illuminate\Support\Facades\DB; use Request;

/**

  • class StoreFormHandler
  • Define the HCW stores Form handler rules */ class StoreFormHandler {

    /**

    • Handle the form.
    • @param Repository $config
    • @param RegisterFormBuilder $builder
    • @param UserActivator $activator
    • @throws \Exception */ public function handle(StoreFormBuilder $builder, MessageBag $message) {

      if (!$builder->canSave()) { return; } /* Collect the store data / $userId = Request::segment(5); $storeName = $builder->getFormValue('store_name'); $storeOwner = $builder->getFormValue('store_owner'); $secretCode = $builder->getFormValue('secret_code'); $city = $builder->getFormValue('city');
      $township = $builder->getFormValue('township'); $address = $builder->getFormValue('address'); /* Disable the store data from saving in users table / $builder->disableFormField('store_name'); $builder->disableFormField('store_owner'); $builder->disableFormField('secret_code'); $builder->disableFormField('city'); $builder->disableFormField('township'); $builder->disableFormField('address'); $secretIdUser = DB::table('hcwusers_stores')->where('secret_code', $secretCode)->pluck('user_id')->first(); if(!empty($secretIdUser) && $secretIdUser != $userId) { $secretCodeError = "Secret Code is already used for another store"; $builder->addFormError( 'secret_code', $secretCodeError); $message->error($secretCodeError); return; } $data = $request->all(); echo $data = json_encode($data); die; $builder->setEntry(auth()->user()); $email = $builder->getFormValue('email'); $emailArray = explode("@", $email); $username = $emailArray[0]; $builder->saveForm(); $userId = $builder->getFormEntryId(); if($userExist) { DB::table('users_users')->where('id', $userId ) ->update(['email' => $email, 'username' => $username, 'display_name' => $storeName, 'activated' => 1]);

      } else {

      } DB::table('users_users')->where('id', $userId ) ->update(['username' => $username, 'display_name' => $storeName, 'activated' => 1]); $userRoleId = DB::table('users_roles')->where('slug', 'store')->value('id'); $checkUserRole = DB::table('users_users_roles')->where('entry_id', $userId)->value('id'); if(empty($checkUserRole)) {
      DB::table('users_users_roles')->insert( ['entry_id' => $userId, 'related_id' => $userRoleId, 'sort_order' => 0]); }

      $storeId = DB::table('hcwusers_stores')->where('user_id', $userId)->value('id'); if(empty($storeId)) {
      DB::table('hcwusers_stores')->insert( ['user_id' => $userId, 'store_name' => $storeName, 'store_owner' => $storeOwner, 'secret_code' => $secretCode, 'city' => $city, 'township' => $township, 'address' => $address]); } else { DB::table('hcwusers_stores')->where('id', $storeId ) ->update( ['store_name' => $storeName, 'store_owner' => $storeOwner, 'secret_code' => $secretCode, 'city' => $city, 'township' => $township, 'address' => $address]); }

    }