CodeIgniter Forums
I want to return the value entered by the user at the time of validation error - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=31)
+--- Thread: I want to return the value entered by the user at the time of validation error (/showthread.php?tid=81685)



I want to return the value entered by the user at the time of validation error - yoshi - 04-09-2022

## overview
where is 'set_fields'.

## reference
https://codeigniter4.github.io/userguide/libraries/validation.html

## detail
The Codeigniter 3 Tutorial had a special variable called 'set_fields'.
The effect was to display the 'post' data entered by the user.

Now, when I have a simple "create lesson" controller like this, is there a way to keep the values entered by the user?
or should I assign it to '$ _POST' and use 'post' in the template?

PHP Code:
    /**
    * @throws ReflectionException
    */
    public function create(): RedirectResponse|string
    
{
        $data null;
        if ($this->request->getMethod() === 'post') {
            $this->validation->reset();
            if ($this->validate(
                [
                    'lesson_title' => ['label' => 'title''rules' => 'required'],
                ]
            )) {
                // valid
                $isContinuous $this->request->getPost('is_continuous');
                if (isset($isContinuous)) {
                    return redirect('lesson_create');
                } else {
                    return redirect('lesson_list');
                }
            } else {
                // invalid
                $data['validation'] = $this->validator;
                view(route_to('lesson_create'), $data);
            }
        } else {
            $data = [
                'title' => null,
                'body' => null,
            ];
        }
        
        $b 
= new Breadcrumb();
        $b->add('Home'route_to('school_home'));
        $b->add('lesson manage'route_to('lesson_list'));
        $b->add('lesson create'null);
        $data['breadcrumb'] = $b->render();
    
        
return view(route_to('lesson_create'), $data);
    



RE: I want to return the value entered by the user at the time of validation error - ignitedcms - 04-09-2022

Do you mean set_value in CI3? If you validate via ajax you wouldn't have to repopulate the form.


RE: I want to return the value entered by the user at the time of validation error - yoshi - 04-09-2022

(04-09-2022, 10:44 AM)ignitedcms Wrote: Do you mean set_value in CI3? If you validate via ajax you wouldn't have to repopulate the form.

thankyou for reply ignitedcms.

the following address is for reference. However, looking inside the `set_value`, it seems that using `$ _POST` after all. Smile
http://www.codeigniter.com/userguide3/helpers/form_helper.html#set_value

The tutorial doesn't seem to use `ajax`, but I'd like to take a look at the `ajax` page and create a validation mechanism.
https://codeigniter.com/user_guide/libraries/validation.html?highlight=validation#the-form
https://codeigniter.com/user_guide/general/ajax.html?highlight=ajax


RE: I want to return the value entered by the user at the time of validation error - kenjis - 04-09-2022

See https://codeigniter.com/user_guide/helpers/form_helper.html#set_value


RE: I want to return the value entered by the user at the time of validation error - yoshi - 04-09-2022

thanks kenjis
Oh? It seems that I misunderstood that 'set_value' was gone.
Thank you


RE: I want to return the value entered by the user at the time of validation error - ignitedcms - 04-09-2022

I believe you were looking for 'set_fields' which is IIRC what it was historically named. Additionally in the old documentation there was a section under libraries > validation which discusses repopulating the form which appears to have been omitted, although as Kenjis has kindly pointed out is in the form helper section.

Off topic, I do prefer validating forms with ajax calling server side validation as it skips a step of having to manually repopulate input fields, though this is predicated on js being enabled in browser.


RE: I want to return the value entered by the user at the time of validation error - yoshi - 04-09-2022

thanks, ignitedcms.

Yes. It was exactly the old `set_values` on the Validation page of the old document.
I would like to challenge later even if I use Ajax