Back to Custom Fields Support

hellopaint
hellopaint

When I make the register and I introduce all fields included Custom Fields, It not save this Custom fields. Only save if I Save this backend plugin configuration or create in backend user plugin users.

Vladimir
Vladimir

Are you using Custom fields with the RainLab Users plugin?
Do you want to keep the value of the custom fields in the front end?

You need to store the form data in the handler.

example

function onSave()
    {

        $data = post();

        $rules = [
            'name' => 'required',
            'email' => 'required|email',
            'password' => 'confirmed|min:8',
        ];

        $validation = Validator::make($data, $rules);

        if ($validation->fails()) {
            throw new ValidationException($validation);
        }

        $id = input('id');

        $user = UserModel::where('id', $id)->first();

        $user->fill($data);

        $user->setField('usercustomfields', [[
                "first" => input('first'),
                "second" => input('second'),
                "photo" => input('notchangephoto'),

         ]]);

        $user->save();

        Flash::success('Successfully saved!');

        if (array_key_exists('password', $data) && strlen($data['password'])) {

            Auth::login($user->reload(), true);

        }

    }
hellopaint
hellopaint

Yes Vladimir I use user plugin. My code is more complex, the problem is that not create de Custom fields when save data.

function onSave() {

    $data = Input::all();

    $rules = [
        'name' => 'required',
        'email' => 'required|email',
        'password' => 'confirmed|min:8',
        'nickname' => 'required',
        'types' => 'required',
    ];

    $validation = Validator::make($data, $rules, Lang::get('ledel.profile::validation'));

    if ( $validation->fails() ) {
        throw new ValidationException($validation);
    }

    $user = Auth::getUser();
    $id = $user->id;

    $user = UserModel::where('id', $id)->first();

    $user->fill( $data );

    if ( post('types') ) {
        $types = Type::whereIn('id', post('types'))->get();
        $user->types = $types;
    }

    if ( isset( $data['origins'] ) ) {
        $origins = Origin::whereIn('id', post('origins'))->get();
    } else {
        $origins = Origin::where('id', 1)->get();
    }
    $user->origins = $origins;

    if ( isset( $data['orientations'] ) ) {
        $orientations = Orientation::whereIn('id', post('orientations'))->get();
    } else {
        $orientations = Orientation::where('id', 1)->get();
    }
    $user->orientations = $orientations;

    if ( isset( $data['genders'] ) ) {
        $genders = Gender::whereIn('id', post('genders'))->get();
    } else {
        $genders = Gender::where('id', 1)->get();
    }
    $user->genders = $genders;

    if ( isset( $data['languages'] ) ) {
        $languages = Language::whereIn('id', post('languages'))->get();
    } else {
        $languages = Language::where('id', 1)->get();
    }
    $user->languages = $languages;

    if ( isset( $data['sports'] ) ) {
        $sports = Sport::whereIn('id', post('sports'))->get();
    } else {
        $sports = Sport::where('id', 1)->get();
    }
    $user->sports = $sports;

    if ( isset( $data['hobbies'] ) ) {
        $hobbies = Hobby::whereIn('id', post('hobbies'))->get();
    } else {
        $hobbies = Hobby::where('id', 1)->get();
    }
    $user->hobbies = $hobbies;

    $user->birthday = input('birthday');

    $user->smoke = input('smoke');

    if ( input('nickname') && input('birthday') && input('name') && input('surname') && input('description') && input('country_id') ) {
        $user->show = 1;
    }

    $filename1 = input('notchangephoto');
    $filename2 = input('notchangephotoone');
    $filename3 = input('notchangephototwo');

    if (Request::hasFile('photo')) {
        $mediafilename = '/media/avatars/' . $user->id . '/' . 'avatar_' . Request::file('photo')->getClientOriginalName();
        $filename1 = 'avatars/' . $user->id . '/' . 'avatar_' . Request::file('photo')->getClientOriginalName();

        Storage::put($mediafilename, file_get_contents(Request::file('photo')->getRealPath()));
    } else {
        $filename1 = "";
    }

    if (Request::hasFile('photoone')) {
        $mediafilename = '/media/avatars/' . $user->id . '/' . 'photoone_' . Request::file('photoone')->getClientOriginalName();
        $filename2 = 'avatars/' . $user->id . '/' . 'photoone_' . Request::file('photoone')->getClientOriginalName();

        Storage::put($mediafilename, file_get_contents(Request::file('photoone')->getRealPath()));
    } else {
        $filename2 = "";
    }

    if (Request::hasFile('phototwo')) {

        $mediafilename = '/media/avatars/' . $user->id . '/' . 'phototwo_' . Request::file('phototwo')->getClientOriginalName();
        $filename3 = 'avatars/' . $user->id . '/' . 'phototwo_' . Request::file('phototwo')->getClientOriginalName();

        Storage::put($mediafilename, file_get_contents(Request::file('phototwo')->getRealPath()));
    } else {
        $filename3 = "";
    }

    $user->setField('images', [[
        "avatar" => $filename1,
        "photoone" => $filename2,
        "phototwo" => $filename3,
    ]]);

    $user->setField('personal', [[
        "nickname" => input('nickname'),
        "description" => input('description'),
        "location" => input('location'),
    ]]);

    $user->save();

    Flash::success('Successfully saved!');

    $this['countries'] = Country::get();
    $this['situations'] = Situation::get();
    $this['complexions'] = Complexion::get();
    $this['eyes'] = Eye::get();
    $this['hairs'] = Hair::get();
    $this['sons'] = Son::get();
    $this['professionals'] = Professional::get();
    $this['rents'] = Rent::get();
    $this['studies'] = Study::get();
    $this['beliefs'] = Belief::get();
    $this['zodiacs'] = Zodiac::get();
    $this['heights'] = Height::get();

    $this['auxCountry'] = $data['country_id'];
    $this['auxTypes'] = $data['types'];
    $this['auxOrigins'] = $origins->toArray(); 
    $this['auxSituation'] = $data['situation_id']; 
    $this['auxGenders'] = $genders->toArray(); 
    $this['auxOrientations'] = $orientations->toArray(); 
    $this['auxComplexion'] = $data['complexion_id']; 
    $this['auxEye'] = $data['eye_id']; 
    $this['auxHair'] = $data['hair_id']; 
    $this['auxSon'] = $data['son_id']; 
    $this['auxProfessional'] = $data['professional_id']; 
    $this['auxStudy'] = $data['study_id']; 
    $this['auxBelief'] = $data['belief_id']; 
    $this['auxLanguages'] = $languages->toArray(); 
    $this['auxSports'] = $sports->toArray(); 
    $this['auxHobbies'] = $hobbies->toArray();
    $this['auxZodiac'] = $data['zodiac_id'];
    $this['auxEmail'] = $data['email']; 
    $this['auxBirthday'] = input('birthday');
    $this['auxHeight'] = input('height_id');
    $this['auxSmoke'] = input('smoke');

    return [
        '#myTypes' => $this->renderPartial('templates/types'),
        '#myOrigins' => $this->renderPartial('templates/origins'),
        '#myOrientations' => $this->renderPartial('templates/orientations'),
        '#myLanguages' => $this->renderPartial('templates/languages'),
        '#mySports' => $this->renderPartial('templates/sports'),
        '#myHobbies' => $this->renderPartial('templates/hobbies'),
        '#myGenders' => $this->renderPartial('templates/genders'),
        '#myBirthday' => $this->renderPartial('templates/birthday'),
    ];    

}
Vladimir
Vladimir

If possible send me backend login details so I can find the reason.
https://octobercms.com/author/Pkurg
or email: pkurg@pkurg.ru

Vladimir
Vladimir

I have updated the plugin to version 2.2.2

1-5 of 5