package com.jfinal.club._admin.ques;

import com.jfinal.club.common.model.Questions;
import com.jfinal.core.Controller;
import com.jfinal.kit.Ret;
import com.jfinal.validate.Validator;

/**
 * @ProjectName: jfinal-club
 * @Package: com.jfinal.club._admin.ques
 * @ClassName: QuestAdminValidator
 * @Author: Jzdsh
 * @Description:
 * @Date: 2020/3/31 15:15
 * @Version: 1.0
 */
public class QuestAdminValidator extends Validator {

    @Override
    protected void validate(Controller c) {
        setShortCircuit(true);
        setRet(Ret.fail( ));
        c.getFile("filepath", "/temp");
        validateRequired("ques.quesname", "msg", "请填写问题描述！");
        validateRequired("ques.questext", "msg", "请填写问题补充说明！");

        String ms = c.get("ques.quesname");
        String sm = c.get("ques.questext");
        if (ms.length( ) > 25) {
            addError("msg", "问题描述应该小于25个字符!");
        }
        if (sm.length( ) > 1000) {
            addError("msg", "问题补充超越1000个字符");
        }

        String sort = c.get("ques.sort");
        String reg = "^[0-9]{1,5}$";
        if (sort == null || !sort.matches(reg)) {
            addError("msg", "排序号输出错误!");
        }

    }

    @Override
    protected void handleError(Controller c) {
        c.keepModel(Questions.class);
        c.renderJson(getRet( ));
    }
}
