thinkphp5.0 edit interface to re-assign data to the editing interface when redirected to the editing interface after validation is not valid

Source: Internet
Author: User

First, take a look at the error scenario:

For example, we have such an editing interface:

And we have some validation methods (note: thinkphp5.0 validation rules are used)

namespaceApp\admin\validate;use think\validate;classLink extends validate{protected$rule = [        "title"="require|max:30",        " Priority"="require|between:1,100",        "URL"="Url|require",        "des"="length:1,255",    ]; protected$message = [        "Title.require"="The title is a must",        "Title.max"="the maximum length of the title is",        "Priority.require"="priority is required",        "Priority.between"="priority must be between 1~100",        "Url.url"="link address is not legal",        "Url.require"="the link address is required",        "Des.between"="a description must be between 1~255 characters"    ];}

For example, if we verify the priority, we will be asked to enter the priority level between 1~100, but if we enter the number is greater than 100? What's going to happen?

As follows:

Verification does not pass! Well, there's nothing wrong with it now, but what happens when the waiting time passes?

Why did such a thing happen? Let's take a look at the code inside the edit controller.

 Publicfunction edit () {if(Request (),IsPost ()) {//If the form is submitted $data= Input ("Post.");//Get data to the editing interface $validate= Loader::validate ("Link");//Load Validator classif(! $validateCheck ($data)) {//Verify data is consistent with our defined validation rules$ ThisError ($validate->geterror (),"Edit","","2");            Die }            $linkModel =NewLinkmodel ();
$updateRes= $linkModel-UpdateLink (Input ("Post.") ;//Call the method inside the Linkmodel to update the dataif($updateRes) {$ This->success ("Modification succeeded","Index","","1"); }Else{ $ This->error ("Modification Failed","Edit","","1"); } return; } $res=DB ("Link"),where("ID", Input ("ID")),Find ()///by first clicking on the Edit button and then going to the database to locate the dataif($res) {$ This->assign ("Link", $res);//Assign data to the edit interface}Else{ $ This->assign ("Link","")///If no data is found, assign to the edit interface an empty (otherwise it might be an error)} returnview ();//Load Interface}

Look at the above code, in the end where the problem arises?

I guess it's a matter of assigning $res data to the interface. If the $res data is empty, then the data we assign to the edit interface is an empty string.

Let's see how our Edit interface gets the data:

Note that at this moment our link is an empty string, then $link.id must not get the value, so will report the above-mentioned error?

But why would $res be empty? As follows:

Do not know if you have noticed such a code:

$res =db ("link"),where("ID", input (" ID ")) ->find (); // start by clicking the Edit button and then go to the database to find the data

When we failed to verify, redirect to the edit interface, the moment we click the Edit button passed through the ID has been refreshed, the ID is not worth it, so nature will not find. How to solve it?

We failed to verify that the jump code is as follows:

if (! $validate->check ($data)) {// Verify data is consistent with our defined validation rules                $ This -error ($validate->geterror (),"  Edit"," ","2");                Die;            }

When we fail to verify:

if (! $validateCheck ($data)) {                session ("redirectid", $data [' ID ' ]);                $ This, error ($validate->geterror (),"edit"," 2 " );                Die;            }

We use the session to save the ID, and then write to edit the data when assigning it:

  $redirectId = Session ("redirectid");//Take the ID out         of the session if($ Redirectid) {//determine if the session is empty
$res=DB ("Link"),where("ID", $redirectId)find ();//Use the ID of the session to search}Else{$res=DB ("Link"),where("ID", Input ("ID")),Find ();//If the session is empty, that is, our verification has not failed, or the first click on the Edit button to enter the editing interface of the scene, with our click on the Edit button to pass in the ID to search}if($res) {$ This->assign ("Link", $res);//Assign data to template}Else{            $ This->assign ("Link",""); }

In this way, we can guarantee the value of the query, no matter how it is directed. But there is another problem, when you fail the validation, after redirecting to the editing interface, you are no longer working on the editing interface, but instead return to the list page.

At this point because you have failed to verify once, so there is a value in the session, when you click on another data editing, remember the above code? We first determine whether there is a value in your session, and at this point there is an ID in the session, so even if we click on different data, the editing interface will show you the last piece of data.

Egg hurts, how to solve?

In fact it is very simple, the method on your list page will empty the session. As follows:

  Publicfunction Index () {session ("Redirectid",NULL);//Delete the remaining ID when editing$res = db ("Link")->order (" Priority","desc"), Paginate (2); if($res) {$ This->assign ("linklist", $res); }Else{            $ This->assign ("linklist", Array ()); }        returnview (); }

Well, it's all settled by now.

thinkphp5.0 edit interface to re-assign data to the editing interface when redirected to the editing interface after validation is not valid

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.