简体   繁体   English

不带库的自定义验证Phalcon

[英]custom validation phalcon without library

I'm beginner in PHP and phalcon, I want to use custom validation and creating default value. 我是PHP和Phalcon的初学者,我想使用自定义验证并创建默认值。

My controller is: 我的控制器是:

use Phalcon\Mvc\Controller;

class OspoController extends Controller
{

    public function indexAction()
    {

    }

    public function createAction()
    {

        $ospo = new Ospos();

        // Store and check for errors
        $success = $ospo->save(
            $this->request->getPost(),
            array('isEmailConfirmed', 'email', 'password', 'salt' ,'phoneNum', 'verifiedPhoneStatus', 'languageId', 'firstName', 'lastName', 'address', 'cityId', 'provId', 'countryId', 'postCode')
        );
        $data = array();
        if ($success) {
            $data[] = array(
                'status' => 'success'
            );
            echo json_encode($data);
        } else {
            foreach ($ospo->getMessages() as $message) {
                $msg = $message->getMessage();
                $data[] = array(
                    'message' => $msg
                );

            }
            echo json_encode($data);
        }

        $this->view->disable();
    }

I want if isEmailConfirmed is null - I want to create value that isEmailConfirmed = 0; 我想,如果isEmailConfirmednull -我想创建值isEmailConfirmed = 0;

How to change array value of getPost() ? 如何更改getPost()数组值?

(can I do this) Should i change the code with $isEmailConfirmed = $_POST['isEmailConfirmed'] ; (我可以这样做)是否应该使用$isEmailConfirmed = $_POST['isEmailConfirmed']更改代码;

and $ospo->save($isEmailConfirmed, $etc, $etc) ? $ospo->save($isEmailConfirmed, $etc, $etc)

Thank you. 谢谢。

First of all, you can just store POST data in a variable. 首先,您可以将POST数据存储在一个变量中。 Then just check for null and assign default value if needed before saving. 然后只需检查是否为空,然后在保存之前根据需要分配默认值。

$data = $this->request->getPost();
if (!isset($data['isEmailConfirmed']) {
    $data['isEmailConfirmed'] = 0;
} 

Another way is to save null value, but in that case you should set up DEFAULT for that column in your database table. 另一种方法是保存空值,但是在这种情况下,您应该为数据库表中的该列设置DEFAULT。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM