1
votes

I'm beginner in PHP and phalcon, I want to use custom validation and creating default value.

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;

How to change array value of getPost()?

(can I do this) Should i change the code with $isEmailConfirmed = $_POST['isEmailConfirmed'];

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

Thank you.

1
How are you sending isEmailConfirmed ? via ajax / form submit? - Timothy
it's later via token from user email @Timothy - Faisal

1 Answers

2
votes

First of all, you can just store POST data in a variable. 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.