Welcome Guest, Not a member yet? Register   Sign In
I want to return the value entered by the user at the time of validation error
#1
Smile 

## overview
where is 'set_fields'.

## reference
https://codeigniter4.github.io/userguide...ation.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);
    
Reply
#2

(This post was last modified: 04-09-2022, 11:06 AM by ignitedcms.)

Do you mean set_value in CI3? If you validate via ajax you wouldn't have to repopulate the form.
Practical guide to IgnitedCMS - Book coming soon, www.ignitedcms.com
Reply
#3

(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/he...#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/libra...n#the-form
https://codeigniter.com/user_guide/gener...light=ajax
Reply
#4

See https://codeigniter.com/user_guide/helpe...#set_value
Reply
#5

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

(This post was last modified: 04-09-2022, 10:32 PM by ignitedcms.)

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.
Practical guide to IgnitedCMS - Book coming soon, www.ignitedcms.com
Reply
#7

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
Reply




Theme © iAndrew 2016 - Forum software by © MyBB